列表视图行为不正常

时间:2017-11-04 12:38:10

标签: android listview

我知道这类问题已被问及herehere,但提供的解决方案并未解决我的问题。

问题: 我正在尝试将项目设置为列表视图(总共19个从查询返回),但列表视图只显示10。它也在重复这些项目。我不知道代码有什么问题。我现在已经摸不着头脑了。

这是我的baseAdapter类:

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

}

这是我设置我的适配器的地方:

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

1 个答案:

答案 0 :(得分:1)

在getCount()中调试列表项的大小。它是19(在你的情况下)然后你的getView()必须有一个缺陷。关于我需要解决的问题:

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

所以你只需将逻辑从If / else语句中删除,因为只有这个块才会为每个位置执行。如果你还面临同样的问题,请告诉我。

对于复选框问题我以下步骤将有所帮助。

1.删除checkedChangeListener。

  1. 为xml

  2. 中的复选框设置clickable false
  3. 在列表的根项上设置OnClick。单击“切换”复选框,将布尔值保存在数据列表中。