Multiple selection on listview with simpleAdapter in fragment

27 Views Asked by At

I have listview that has onItemClickListener. I have created arraylist that adds the value of selected item. I have 2 function, one is for selectall where it selects and change background color to all item in listview(works fine). When i deselect/select the first item, it causes another item in the list to deselect/select, which i haven't touched. It doesn't remove the item from the arraylist that was deselected by itself. However the user would see that it is deselected. I understand from researching that it is because we are setting the background color to the view, then when you scroll, we end up reusing that view due to using convertView. Need all the help as it i have tried the given methods.

I have tried adding getting view and setting background color by having this but issue still persist :

  View vi=getViewByPosition(position,LvTags);
  vi.setBackgroundColor(getResources().getColor(R.color.white));

and

view.setBackgroundColor(getResources().getColor(R.color.white));

and

LvTags.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.white))

and

  LvTags.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  LvTags.setItemChecked(position, false); 

Below is the code

 checkedValue = new ArrayList<>();
LvTags = (ListView) rootView.findViewById(R.id.LvTags);
adapter = new SimpleAdapter(getContext(), tagList, R.layout.listtag_items,
            new String[]{"tagUii", "tagacsii","tagLen", "tagCount", "tagRssi"},
            new int[]{R.id.TvTagUii,  R.id.TvTagAcsii, R.id.TvTagLen, R.id.TvTagCount,
                    R.id.TvTagRssi});

LvTags.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String completepath = tagList.get(position).toString();
           
            mSelectedItem=position;
            TextView textView = (TextView) view.findViewById(R.id.TvTagUii);



            String RFID = textView.getText().toString();
            porcelainReceivingMessage.setText("");
            
            insqldata = checkInSql("RFID", RFID);
            if (!insqldata) {
              
                boolean insqlite=checkInSqlite("RFID",RFID);

                if(insqlite){
                    Toast.makeText(getContext(), "Tag in already scanned",
                            Toast.LENGTH_SHORT).show();
                   //view.setBackgroundColor(getResources().getColor(R.color.white));--> Tried 
                    //View vi=getViewByPosition(position,LvTags); -->Tried 
                  //  vi.setBackgroundColor(getResources().getColor(R.color.white));-->
                    LvTags.setItemChecked(position, false);
                    //LvTags.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.white));
                }else {
                    if (checkedValue!=null && checkedValue.contains(RFID)) {
                checkedValue.remove(RFID);
                        Log.d("deselect"+position, RFID);
                        selectedCount.setText(String.valueOf(checkedValue.size()));
               // view.setBackgroundColor(getResources().getColor(R.color.white));
                        
                        Log.d("deselect", String.valueOf(view));
                    } else {
                        checkedValue.add(RFID);
                        selectedCount.setText(String.valueOf(checkedValue.size()));
                        //view.setBackgroundColor(getResources().getColor(R.color.gray1));
                     
                        Toast.makeText(getContext(), "Long click to add this tag to scanned items",
                                Toast.LENGTH_SHORT).show();
                        Toast.makeText(getContext(), "Item selected",
                                Toast.LENGTH_SHORT).show();
                    }
                }
               
            }else if (insqldata){

                Toast.makeText(getContext(), "Tag in already in SQL DB",
                        Toast.LENGTH_SHORT).show();
                //view.setBackgroundColor(getResources().getColor(R.color.white));

            }
            Log.d("checkedValue",String.valueOf(checkedValue));


        }
    });
1

There are 1 best solutions below

0
Ahsan On

I was able to solve it by changing the background in getView method. First i added another hmap item called background. If background is true then change the background else have it as white. Even though there is a getView method in Simple Adapter. I created Override getView when initializing the adapter as below.

 adapter = new SimpleAdapter(getContext(), tagList, R.layout.listtag_items,
                new String[]{"tagUii", "tagacsii","tagLen", "tagCount", "tagRssi","background"},
                new int[]{R.id.TvTagUii,  R.id.TvTagAcsii, R.id.TvTagLen, R.id.TvTagCount,
                        R.id.TvTagRssi,R.id.TvTagBg}) {
          @Override
          public View getView (int position, View convertView, ViewGroup parent) {
              View view = super.getView(position, convertView, parent);
              Boolean 
           bg=Boolean.valueOf(tagList.get(position).get("background"));
                              
              if (bg) {
                  view.setBackgroundColor(Color.GRAY);
              } else {
                  view.setBackgroundColor(Color.WHITE);
              }
              return view;
          }
      };

onClick i set the background for the hmap entry as below.

  LvTags.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String completepath = tagList.get(position).toString();
                              
                mSelectedItem=position;
                TextView _taguii = (TextView) view.findViewById(R.id.TvTagUii);
                TextView _tagacsii = (TextView) view.findViewById(R.id.TvTagAcsii);
                TextView _tagcount = (TextView) view.findViewById(R.id.TvTagCount);
                TextView _tagrssi = (TextView) view.findViewById(R.id.TvTagRssi);

                String RFID = _taguii.getText().toString();
                String _acsii = _tagacsii.getText().toString();
                String _count = _tagcount.getText().toString();
                String _rssi = _tagrssi.getText().toString();
                porcelainReceivingMessage.setText("");
                
                insqldata = checkInSql("RFID", RFID);
                if (!insqldata) {

                    boolean insqlite=checkInSqlite("RFID",RFID);

                    if(insqlite){
                        Toast.makeText(getContext(), "Tag in already scanned",
                                Toast.LENGTH_SHORT).show();
                        hmap = new HashMap<String, String>();
                        hmap.put("tagUii",RFID);
                        hmap.put("tagRssi",_rssi);
                        hmap.put("tagCount",_count);
                        hmap.put("tagacsii",_acsii);
                        hmap.put("background", "false");
                        tagList.set(position,hmap);
                        

                    }else {
                        if (checkedValue!=null && checkedValue.contains(RFID)) {
                    checkedValue.remove(RFID);
                                                   

                            hmap = new HashMap<String, String>();
                            hmap.put("tagUii",RFID);
                            hmap.put("tagRssi",_rssi);
                            hmap.put("tagCount",_count);
                            hmap.put("tagacsii",_acsii);
                            hmap.put("background", "false");
                            tagList.set(position,hmap);
                            Log.d("deselect", String.valueOf(view));
                        } else {
                            checkedValue.add(RFID);
                            selectedCount.setText(String.valueOf(checkedValue.size()));
                            hmap = new HashMap<String, String>();
                            hmap.put("tagUii",RFID);
                            hmap.put("tagRssi",_rssi);
                            hmap.put("tagCount",_count);
                            hmap.put("tagacsii",_acsii);
                            hmap.put("background", "true");
                            tagList.set(position,hmap);

                        
                            Toast.makeText(getContext(), "Item selected",
                                    Toast.LENGTH_SHORT).show();

                        }
                    }
                    
                }else if (insqldata){

                    Toast.makeText(getContext(), "Tag is already in SQL DB",
                            Toast.LENGTH_SHORT).show();
                        
                }
               

                adapter.notifyDataSetChanged();
             
            }


        });