1

这是我的自定义 ListViewAdapter

public class ListViewAdapter extends BaseAdapter{


    Viewholder holder;
    public ArrayList<HashMap<?, ?>>list;
    public ArrayList<Integer> add;
    Activity activity;
    boolean [] checkedItems;

    String []ckboxTextAdd;
    DbAdapter db;
    /*
     * Constructor Calling
     */

    public ListViewAdapter(Activity activity,ArrayList<HashMap<? , ?>>list) {
        // TODO Auto-generated constructor stub
         super();
         this.activity=activity;
         this.list=list;
         add=new ArrayList<Integer>(list.size());
         db=new DbAdapter(activity);
         db.open();
         checkedItems= new boolean[list.size()];

         ckboxTextAdd=new String[list.size()];
         for(int i=0;i<list.size();i++)
         {
             checkedItems[i]=false;
             ckboxTextAdd[i]="Add";

         }

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertview, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater layoutInflater=activity.getLayoutInflater();
        final HashMap<?, ?> map=list.get(position);
        if(convertview==null)
        {
        convertview=layoutInflater.inflate(R.layout.l_view,parent,false);
        holder = new Viewholder();
        holder.title=(TextView) convertview.findViewById(R.id.serving_size_grams);
        holder.servingsize=(TextView) convertview.findViewById(R.id.tvprotiendesclabel);
        holder.Fiber=(TextView)convertview.findViewById(R.id.Fiberview);

        holder.carbs=(TextView)convertview.findViewById(R.id.carbsview);
        holder.sodium=(TextView)convertview.findViewById(R.id.sodiumview);
        holder.proteins=(TextView)convertview.findViewById(R.id.Protienview);
        holder.calories=(TextView)convertview.findViewById(R.id.caloriesview);
        holder.img_serve_size=(ImageView)convertview.findViewById(R.id.image_serve_eqivalent);

        holder.calories_val=(TextView)convertview.findViewById(R.id.cal_desc_val);
        holder.Fiber_val=(TextView)convertview.findViewById(R.id.Fiber_desc_val);
        holder.sodium_val=(TextView)convertview.findViewById(R.id.sodium_desc_val);
        holder.proteins_val=(TextView)convertview.findViewById(R.id.prot_desc_val);
        holder.carbs_val=(TextView)convertview.findViewById(R.id.carbs_desc_val);
        holder.tvbrand=(TextView)convertview.findViewById(R.id.item_brand_name);
        holder.ckbox=(CheckBox)convertview.findViewById(R.id.save_item_box);
        holder.tvckboxText=(TextView)convertview.findViewById(R.id.ckboxAdd);



        convertview.setTag(holder);
        }   
         else
         {
              holder=(Viewholder)convertview.getTag();
         }


         holder.ckbox.setOnCheckedChangeListener(null);
         holder.ckbox.setChecked(checkedItems[position]);
         holder.tvckboxText.setText(ckboxTextAdd[position]);
         holder.ckbox.setTag(R.id.save_item_box, position);
        //On Check Box Check Events



         holder.ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



             @Override
                public void onCheckedChanged(final CompoundButton buttonview, boolean isChecked) {
                    // TODO Auto-generated method stub

                     final int position = (Integer) buttonview
                                .getTag(R.id.save_item_box);

                       if (isChecked) 
                       {
                           Log.i(String.valueOf(position), String.valueOf(isChecked));


                          checkedItems[position] = true;
                          ckboxTextAdd[position]="Added";

                         //holder.ckbox.setText(ckboxTextAdd[position]);
                         Log.i("value of Somple List", list.get(position).toString());
                         Log.i("value of listitr", list.get(position).toString());
                         Log.i("value of text box",ckboxTextAdd[position]);
                         final HashMap<?, ?> map=list.get(position);
                         map.get(Constants.NF_CALORIES);

                         String item_name =map.get(Constants.ITEM_NAME).toString().replace('\'', ' ');
                         String brand_name =map.get(Constants.BRAND_NAME).toString().replace('\'',' ');
                         Cursor cursor= db.fetch_data(item_name,brand_name);
                         if(cursor.getCount()>0)
                        {
                             AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                             builder.setMessage("Already Saved")
                                    .setPositiveButton("Replace", new DialogInterface.OnClickListener() 
                                    {
                                        public void onClick(DialogInterface dialog, int id)
                                        {

                                                        checkedItems[position] = true;
                                                        ckboxTextAdd[position]="Added";

                                                        Log.i("value of text box trey",ckboxTextAdd[position]);
                                                        //   holder.ckbox.setText(ckboxTextAdd[position]);
                                         }
                                    })
                                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                                    {
                                        public void onClick(DialogInterface dialog, int id) 
                                        {


                                            checkedItems[position] = false;
                                            ckboxTextAdd[position]="Add";

                                               Log.i("value of text box false",ckboxTextAdd[position]);
                                            //holder.ckbox.setText(ckboxTextAdd[position]);

                                         }
                                    });

                             AlertDialog alertDialog = builder.create();
                             alertDialog.show();
                        }
                        else
                            {


                            Log.i("value of Calories",map.get(Constants.NF_CALORIES).toString());
                            db.createSave(
                                  position
                                  ,brand_name
                                  ,item_name
                                  ,map.get("path").toString()
                                  ,map.get(Constants.NF_SERVING_SIZE_QTY).toString()
                                  ,map.get(Constants.NF_SERVING_SIZE_UNIT).toString()
                                  ,map.get(Constants.NF_CALORIES).toString()
                                  ,map.get(Constants.NF_PROTEINS).toString()
                                  ,map.get(Constants.NF_SODIUM).toString()
                                  ,map.get(Constants.NF_TOTAL_FAT).toString()
                                  ,map.get(Constants.NF_TOTAL_CARBOHYDRATES).toString()
                                  ,map.get(Constants.NF_SATURATED_FAT).toString()
                                  ,map.get(Constants.NF_CHOLESTEROL).toString()
                                  ,map.get(Constants.NF_VITAMIN_A).toString()
                                  ,map.get(Constants.NF_VITAMIN_C).toString()
                                  ,map.get(Constants.NF_IRON).toString()
                                  ,map.get(Constants.NF_CALCIUM).toString()
                                  ,map.get(Constants.NF_SERVING_WEIGHT_GRAMS).toString()
                                  );
                            }
                        }
                       else 
                        {
                            Log.i(String.valueOf(position), String.valueOf(isChecked));
                            //buttonview.setChecked(false);
                            //buttonview.setText("Save");
                             ckboxTextAdd[position]="Add";

                               Log.i("value of text box false",ckboxTextAdd[position]);
                        //   holder.ckbox.setText(ckboxTextAdd[position]);
//                          holder.ckbox.setText("Save");
//                          holder.ckbox.setBackgroundColor(Color.rgb(198, 228, 225));
                            checkedItems[position] = false;
                            //holder.ckbox.setText(ckboxTextAdd[position]);
                            db.deleteUnChecked(position);
                        }
                }
            });
}
        return convertview;
    }
private class Viewholder extends Activity
    {
        TextView title;
        TextView servingsize;
        //TextView status;
        TextView carbs;
        TextView proteins;
        TextView calories;
        ImageView img_serve_size;
        TextView sodium;
        TextView Fiber;

        TextView carbs_val;
        TextView proteins_val;
        TextView calories_val;
        TextView sodium_val;
        TextView Fiber_val;
        CheckBox ckbox;
        TextView tvbrand;


        TextView tvckboxText;
    }

现在我想在列表视图中单击复选框的那一刻更新我的文本视图,但直到现在它没有按预期执行,但是当我向下滚动并查看选中的项目时,文本视图已更新

请帮助我提前谢谢

4

2 回答 2

1

问题:

当您更新数组的值以映射已选中或未选中的状态时TextView,您不会更新被更新。CheckBoxTextView

解决方案:

您需要手动更新TextView' 的文本,或者notifyDataSetChanged()在选中或取消选中该项目时简单地调用,这将getView()调用 Adapter 并根据更新的值强制刷新行。

建议/改进:

  1. 您可以只维护单个布尔数组,无需维护Added或Add的String数组,在显示文本时,检查布尔值是否为真,将文本设置为“Added”,否则“Add”如下:

    holder.ckbox.setChecked(checkedItems[position]);
    holder.tvckboxText.setText(checkedItems[position]?"Added":"Add");
    
  2. 与其实现 anonymous CompoundButton.OnCheckedChangeListener,不如将其实现到类级别并设置为this,到目前为止,CompoundButton.OnCheckedChangeListener每次滚动列表时都会创建多个对象。

  3. 你在哪里关闭数据库?虽然没关系,但有一个结束点很好。

于 2014-10-05T11:01:35.957 回答
1

测试使用

holder.ckbox.setText(checkedItems[position]?"Added","Add");

并删除

holder.tvckboxText.setText(ckboxTextAdd[position]?"Added","Add"););

这是因为它可能不支持横向视图

于 2014-10-11T05:34:32.647 回答