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));