Figen Güngör
Figen Güngör

Reputation: 12559

How can I access textview of other items when I click on another item in ListView?

I have a hidden textView in listView item layout and when the item is clicked, I am displaying that hidden textView. However, when I click another item, I want the other visible textview in item to be hidden again. So how can I access the textView of this other item?

theListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

                        TextView itemView2 = (TextView)view.findViewById(R.id.textView2);
                        itemView2.setVisibility(View.VISIBLE);
                        itemView2.setBackgroundColor(color.white);

                        Double lat = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLat());
                        Double lng = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLng());
                        LatLng itemLocation = new LatLng(lat,lng);

                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(itemLocation, 18 ));

                    }
                        });

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="7dp"
        android:textSize="15sp"
        android:textColor="@color/loginblue"></TextView>
      <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="7dp"
        android:textSize="12sp"
        android:visibility="gone"
        android:textColor="@color/loginblue"></TextView>

</LinearLayout>

Upvotes: 0

Views: 272

Answers (2)

Lars Blumberg
Lars Blumberg

Reputation: 21421

You need to remember the index/position of the selected item in your ListView. Then, if another item is selected, you test if there already is another selected item. If so you can get that item's view like so:

// Hide previously selected text view
if (selectedIndex > -1) {
    int viewIndex = selectedIndex - listView.getFirstVisiblePosition();
    // if previously selected list item is still visible, hide its text view
    if (viewIndex > -1 && viewIndex < listView.getChildCount()) {
        View itemView = listView.getChildAt(viewIndex);
        TextView textView = itemView.findViewById(R.id.textView2);
        textView.setVisibility(View.GONE);
    }
}

// Now you make the newly selected text view visible and remember the selected item
selectedIndex = position;

selectedIndex is an instance variable of either your click listener or any other surrounding class. Initialize it with -1 as no item is selected in the beginning.

Obviously you can optimize the code. It just to clarify to basic steps to accomplish your task.

Upvotes: 1

Techfist
Techfist

Reputation: 4344

define this as member variable in your activity,

int clickedPosition = -1;

then use this code,

            heListView.setOnItemClickListener(
                    new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

                            if(clickedPosition!= position && clickedPosition != -1){
                                clickedPosition = position;
                                View previousView = heListView.getChildAt(clickedPosition);
                                TextView itemView2 = (TextView)view.findViewById(R.id.textView2);
                                itemView2.setVisibility(View.INVISIBLE);
                            }
                            TextView itemView2 = (TextView)view.findViewById(R.id.textView2);
                            itemView2.setVisibility(View.VISIBLE);
                            itemView2.setBackgroundColor(color.white);

                            Double lat = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLat());
                            Double lng = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLng());
                            LatLng itemLocation = new LatLng(lat,lng);

                            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(itemLocation, 18 ));

                        }
                            });

Upvotes: 1

Related Questions