Saloom
Saloom

Reputation: 163

ListView item's OnClickListener behaviour replicating on more items

When I have a simple ListView and I want to change the color of the TextBox showing the string of the item by clicking on it I use the following code:

public class ListViewActivity extends Activity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    ArrayList<String> list=new ArrayList<String>();
    for (int i = 0; i < 100; i++) {
        list.add(String.valueOf(i));
    }

    listView=(ListView) findViewById(R.id.listView1);
    CustomArrayAdapter adapter=new CustomArrayAdapter(this,R.layout.listview_row,R.id.listviewRow,list);
    listView.setAdapter(adapter);

}

class CustomArrayAdapter extends ArrayAdapter<String>{


    public CustomArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=super.getView(position,convertView,parent);

        ViewGroup vg=(ViewGroup) view;
        TextView txtView=(TextView) vg.getChildAt(0);

        if (!txtView.hasOnClickListeners()){
            txtView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView t=(TextView) v;
                    t.setTextColor(0xffbababa);
                    t.invalidate();
                }
            });
        }

        return view;
    }
}

}

and the layout files:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="gr.spunitrade.development.spda.ListViewActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView1">

</ListView>

and

<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/listviewRow"
        />

</LinearLayout>

When clicking on the items one can see that by scrolling down the ListView more items have been grayed out that where not clicked. The same behavior happens even if the graying out is performed on listView.setOnItemClickListener alternative implementation.

I cannot understand why this happens. The breakpoints on the OnClick event are hit only once.

Upvotes: 1

Views: 24

Answers (1)

Joel Libby
Joel Libby

Reputation: 116

I would approach this way.

Assuming you want to toggle color based on click.

Keep an array of booleans with size of your data source.

Initialize to false.

When clicked set to true and set color. Then in on create view set the color based on the boolean.

Upvotes: 1

Related Questions