0

我正在创建一个按类别划分的列表,我正在使用 View Holder 在列表视图中传递一个视图(使用简单光标适配器)。因此,在我的自定义简单光标适配器中,我可以在列表的每个项目上设置一个条的可见性。但是在滚动时,该栏的可见性对于列表项不是固定的,它会随机上下变化。检查我在这里发布的代码。

当我从正常活动中调用我的自定义光标列表适配器的构造函数时,只有我传递所有参数(视图、光标),并且我只是骑着绑定视图而不是 newview。

请帮助我。

活动是:

 catCount = cursorCat.getCount();
int i=0;
cursorCat.moveToFirst();
do {
categ  =  cursorCat.getString(cursorCat.getColumnIndex("category_Name")); 
cursorCatItems = db.rawQuery("SELECT _id, product_code, product_name, product_category, in_stock, price FROM ProductDetails WHERE _id || ' ' || product_name || product_code LIKE ? ",
new String[] { "%" +searchValue+ "%"});

adapter = new MforceAdapterCat(this, R.layout.item_details,
cursorCatItems, new String[] { "product_code", "product_name","product_category","in_stock","price" }, new int[] {
R.id.tvCode, R.id.tvItemName,R.id.tvItemType,R.id.tvQuantity,R.id.tvPrice });

listView.setAdapter(adapter);

i++;
} while (cursorCat.moveToNext() && i<catCount-1);              

cursorCat.close();

// down here is the code for list view items

      @SuppressLint("NewApi")
      public class MforceAdapterCat extends SimpleCursorAdapter {
        String prevCat="";
    int count=0;
    private final String TAG = this.getClass().getSimpleName();

    protected ListAdapter adapter;

    public MforceAdapterCat(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        // TODO Auto-generated constructor stub
    }


@SuppressWarnings("deprecation")
public MforceAdapterCat(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO Auto-generated constructor stub
}


@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return super.getItemViewType(position);
}


   @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        ViewHolder holder = (ViewHolder) view.getTag();

        holder = new ViewHolder();

            holder.tvCode = (TextView) view.findViewById(R.id.tvCode);
            holder.tvItemName = (TextView) view.findViewById(R.id.tvItemName);
            holder.tvItemType = (TextView) view.findViewById(R.id.tvItemType);
            holder.tvPrice = (TextView) view.findViewById(R.id.tvPrice);
            holder.tvQuantity = (TextView) view.findViewById(R.id.tvQuantity);
            holder.imgBtnLv=(ImageButton) view.findViewById(R.id.imgBtnLv);
            holder.categBar=(LinearLayout) view.findViewById(R.id.categBar);
            holder.tvTitleCateg=(TextView) view.findViewById(R.id.tvTitleCateg);

            holder.item_detail_layout=(RelativeLayout) view.findViewById(R.id.item_detail_layout);


            String currCategory =   cursor.getString(cursor.getColumnIndex("product_category"));

         holder.tvTitleCateg.setText(currCategory);

         if(prevCat.equalsIgnoreCase(currCategory)){


             holder.categBar.setVisibility(View.GONE);


         }else{

                    holder.categBar.setVisibility(View.VISIBLE);

         }

         prevCat= currCategory;

         OnClickListener mOnImagebtnClickListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int position = ProductsActivity.listView.getPositionForView((View) v.getParent());
                    Log.v(TAG, "Image button in list clicked, row ="+position);

                    ListAdapter la=     ProductsActivity.listView.getAdapter();
                       System.out.println("list adapter from get is ="+la);

                    int cnt =     getPositionFromRowId(la, position, 0, la.getCount());

                     System.out.println("info from new mthod ="+cnt);

                }
            };

            holder.imgBtnLv.setOnClickListener(mOnImagebtnClickListener);

            view.setTag(holder);


    }

    static class ViewHolder {
        TextView tvCode;
        TextView tvItemName,tvItemType,tvPrice,tvQuantity;
         RelativeLayout item_detail_layout;
         ImageButton imgBtnLv;
         LinearLayout categBar;
         TextView tvTitleCateg;
    }
4

1 回答 1

0

您使用 ViewHolder 的方式毫无意义。恕我直言,这是没用的。见这里: http ://www.jmanzano.es/blog/?p=166

您没有在这里夸大任何视图,因此您不需要它。ViewHolder 应该用于删除多余的视图膨胀和重用视图,但在光标适配器中重用视图已经完成。所以你可以删除它。

于 2013-02-07T06:49:06.647 回答