1
votes

I've a problem with ListView. I have a ListView with some items(for example, two have green background color, and rest have white. When I click on item, the rest of items changes colors to white.

Example1:

Item1(white)
Item2(green)
Item3(white)
Item4(green)

When I click on Item3 this look like:

Item1(green)
Item2(white)
Item3(green)
Item4(white)

Example2:

Item1(white)
Item2(green)
Item3(white)
Item4(white)

When I click on Item3 this look like:

Item1(white)
Item2(white)
Item3(green)
Item4(white)

What can I do to fix it?

EDIT: This is my code onCreate() method:

    protected void onCreate(Bundle savedInstanceState) {
    System.out.println("onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paired_devices);
    devicesList = (ListView) findViewById(R.id.pairedDevicesListView);
    devices = DatabaseHandler.getInstance().getCounters(false);
    adapter = new ArrayAdapter(this, R.layout.text_view_layout, devices);
    devicesList.setAdapter(adapter);

    devicesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            System.out.println("Click " + devicesList.getItemAtPosition(i));
        }
    });
}

Adding items to ListView:

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Broadcast Receiver");
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println(device.getName());

            for(int i = 0; i < devicesList.getCount(); i++) {
                if(device.getName().equals(devices.get(i))) {
                    View v = devicesList.getChildAt(getItemPosition(device.getName()));
                    v.setBackgroundColor(Color.GREEN);
                }
            }

        }
    }
};

EDIT2 ListView xml

    <ListView
    android:layout_width="match_parent"
    android:layout_height="167dp"
    android:id="@+id/pairedDevicesListView"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.66"
    android:choiceMode="singleChoice" />

text_view_layout

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textviewlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"></TextView>
1
Could you please post the relevant part of your code? Thanks.fasteque
could you give us some code ? I suspect a problem in your adapter.Anton Roy
Kindly post your layout filehumblerookie
NEVER change child views of AdapterViews directly! AdapterViews manipulates with fixed set of views, where only content changed when it scrolled. Change your data collection instead, then call yourAdapter.notifyDataSetChanged()Ganster41

1 Answers

0
votes

You can do something like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(dbManager.toggleCheck(list,parent.getItemAtPosition(position).toString()) == 1){
                parent.getChildAt(position).setBackgroundColor(Color.parseColor("#2196f3"));
            }
            else {
                parent.getChildAt(position).setBackgroundColor(Color.parseColor("#e3f2fd"));
            }
        }
    });