如何根据listview中复选框的状态更新textview文本

时间:2014-10-04 06:51:08

标签: android listview listview-adapter

这是我的自定义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;
    }

现在我想在列表视图中点击复选框的那一刻更新我的textview 但是直到现在它还没有按照预期的那样做,但是当我向下滚动并对已检查的项目进行检查时,textView就会更新

请提前帮助我

2 个答案:

答案 0 :(得分:1)

<强>问题:

当您更新数组的值以映射已选中或未选中的状态时,您不会将TextView更新为CheckBox&#39; s选中或取消选中已实现api的开箱即用,但TextView的文字需要更新。

<强>解决方案:

您需要手动更新TextView的文字,或者只要在选中或取消选中该项时调用notifyDataSetChanged(),就会调用适配器的getView()并将强制根据更新的值刷新行。

<强>建议/改进:

  1. 你只能维护单个数组的布尔值,不需要维护String数组的添加或添加,同时显示文本,检查布尔值是否为真,将文本设置为&#34;添加&#34;,别的&#34;添加&#34;像这样:

    holder.ckbox.setChecked(checkedItems[position]);
    holder.tvckboxText.setText(checkedItems[position]?"Added":"Add");
    
  2. 不是实施匿名CompoundButton.OnCheckedChangeListener,而是将其实施到班级并设置为this,截至目前,您每次创建多个CompoundButton.OnCheckedChangeListener对象时滚动列表。

  3. 你在哪里关闭数据库?它并不是matter though,但它有一个关闭点的好处。

答案 1 :(得分:1)

istesd use

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

并删除

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

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