0

I know this kind of question have been asked here and here but the solutions provided there didn't solved my problem.

PROBLEM: I am trying to set items to a list view (19 in total returning from a query) but the list view is just displaying 10 from it. Also it's repeating the items. I don't know what's wrong with the code. I've been scratching my head for a while now.

This is my baseAdapter class:

public class StrengthOfDemandAdapter extends BaseAdapter {
    ArrayList<Products> list;
    private LayoutInflater layoutInflater;
    private static HashMap<Integer, String> selectedSODBrandsMap = new HashMap<Integer, String>();

    //private static ArrayList<String> selectSODBRandNames = new ArrayList<String>();
    private Context con;
    private int imageNo;

    public StrengthOfDemandAdapter(Context context, ArrayList<Products> list, int imageNumber) {
        this.list = list;
        this.con = context;
        this.imageNo = imageNumber;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder mHolder;
        if(convertView == null){
            Log.i("SIZE", "getView: "+list.size());
            convertView = layoutInflater.inflate(R.layout.strengthofdemandlistview, null);
            mHolder = new ViewHolder();
            mHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.SODCheckBox);
            mHolder.mCheckBox.setText(((Products)list.get(i)).getProductName());
            mHolder.mCheckBox.setTag(((Products)list.get(i)).getProductID());

            mHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        //Log.i("YES", "onCheckedChanged: YES");
                        selectedSODBrandsMap.put(((Products)list.get(i)).getProductID(), ((Products)list.get(i)).getProductName());
                    }else {
                        //Log.i("NO", "onCheckedChanged: NO");
                        selectedSODBrandsMap.remove(((Products)list.get(i)).getProductID());
                    }
                }
            });

            convertView.setTag(mHolder);
        }else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    private class ViewHolder {
        private CheckBox mCheckBox;

    }

    public static HashMap<Integer, String> sendSelectedSODBrandsMap(){
        return selectedSODBrandsMap;
    }

}

This is where I set my adapter:

Cursor crsBrand = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_PRODUCT +" WHERE "
            + ItemsTable.COLUMN_PRODUCT_CATEGORY_ID +"="+ categoryID +"", null);

    while (crsBrand.moveToNext()){
        int brandID = crsBrand.getInt(crsBrand.getColumnIndex("id"));
        String brandTitle = crsBrand.getString(crsBrand.getColumnIndex("title"));
        //Log.i("INFO", "openDialog: "+brandID+", "+brandTitle);
        brandList.add(new Products(brandID, brandTitle, 0));
    }
    crsBrand.close();
    SODListViews.setAdapter(new StrengthOfDemandAdapter(StrengthOfDemandsView.this, brandList, imageNo));
3iL
  • 2,146
  • 2
  • 23
  • 47
  • The `setText` and `setTag` must be outside the `if-else` block but before `return converView`. What you did is you are assigning the value after inflating the view only. – Enzokie Nov 04 '17 at 12:42

1 Answers1

1

Well debug the size in getCount() for list items . It it 19(in your case) then there must be a flaw in your getView(). On thing i need to address you :

 if(convertView == null){
  //This will call only whenever new view is created probably only once 
        convertView.setTag(mHolder);
    }else {
        mHolder = (ViewHolder) convertView.getTag();
    }

So you just put the logic out of If/else statement, cause only this block will execute for each position. Let me know if you still face same issue.

For check Box Problem I thing following steps will help.

1.Remove the checkedChangeListener.

  1. Set clickable false for check box in xml

  2. Set OnClick on root item of list . Toggle Check box on Its click and save the boolean in your data list .

ADM
  • 20,406
  • 11
  • 52
  • 83
  • I moved the code outside if else statement and it works fine on the first time (displays all 19 items) but when i come back to the view the listview repeats – 3iL Nov 04 '17 at 13:13
  • The check where you are setting the adapter to listview . Check the size of arrayList Your are passing to adapter . If still got issue post code of where you setting adapter . – ADM Nov 04 '17 at 13:18
  • I have edited my question and added the part where I set my adapter. Please have a look @ADM – 3iL Nov 04 '17 at 13:28
  • Ok i see . Check the brandList Size before setting adapter . for first time and second time too .As i am seeing you are adding the element to list . If its not not reinitialize each time it will contain previously added items . – ADM Nov 04 '17 at 13:33
  • My bad I wasn't initialing brandList properly. It works fine now. Although I have another issue I want your help with. My listview is a list of checkboxes. The issue is when I check the first checkbox 11th one automatically gets checked and if I check the second one 12th one get automatically checked and so on. Its a bit weird. hope you can hellp – 3iL Nov 04 '17 at 13:48