select multiple items in a listview and change background color of selected item

杀马特。学长 韩版系。学妹 提交于 2019-12-25 08:30:11

问题


Hey I am stuck with 1 thing that I want a view like this :

where in a listview I can select multiple items and whichever item I can select I can change the backgrond color and also add that item into the arraylist so that I can use it further..

Anyone please help me in this??

EDIT:

Am using it like this:

     ArrayAdapter<String> part_list_adapter=new ArrayAdapter<String>(AssetSearch.this,  R.layout.part_list,R.id.label,part_array_list);

    PartNumber_List.setAdapter(part_list_adapter);


       PartNumber_List.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {
        String item = (String)PartNumber_List.getItemAtPosition(position);

        Log.i("Item", item);
         v.setBackgroundColor(R.color.result_image_border);

    }
});

Here in a log it is displaying me the item clicked only..but changes color of 2 items...


回答1:


Try like this..

  @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
   String item = (String) getListAdapter().getItem(position);
    boolean blnFound = yourarrayList.contains(item);
 if(blnFound=true){
     yourarraylist.remove(item);
       v.setBackgroundColor(R.color.somecolourtoindicate notselected);

    }
   else{
          yourarraylist.add(item);
          v.setBackgroundColor(R.color.somecolourtoindicate itemselected);
    }
    yourarraylist.add((String) getListAdapter().getItem(position);)
}



回答2:


Your going to set Background for View v of list view cell, actually list view adapter reuse the cell view when you scroll the view, because of that the color reflect in some other rows cell.

Only option you need to add one image view to the View holder and try to change the background of image view in view holder , don`t change adapter view color.



来源:https://stackoverflow.com/questions/10085666/select-multiple-items-in-a-listview-and-change-background-color-of-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!