从自定义适配器中的ListView获取文本

时间:2014-10-08 07:21:12

标签: android listview android-listview custom-adapter

我在获取特定的textview值时遇到问题,我得到了最后一个的值这里是我的customAdapter的getview()代码 基本上我想要的是更新加号按钮点击数量文本,反之亦然

@Override
    public View getView(int position, View convertview, ViewGroup parent) {
LayoutInflater layoutInflater=((Activity) activity).getLayoutInflater();
        final HashMap<?, ?> map=list.get(position);
        if(convertview==null)
        {
        convertview=layoutInflater.inflate(R.layout.listviewdetails,parent,false);
        holder = new Viewholder();
        holder.title=(TextView) convertview.findViewById(R.id.lserving_size_grams);
        holder.servingsize=(TextView) convertview.findViewById(R.id.ltvprotiendesclabel);
        holder.Fiber=(TextView)convertview.findViewById(R.id.lFiberview);
        holder.lserving_qty=(TextView)convertview.findViewById(R.id.lvalueserving);
        holder.ckbox=(CheckBox)convertview.findViewById(R.id.lsave_item_box);
        holder.sminus=(ImageView)convertview.findViewById(R.id.limage_minus);
        holder.splus=(ImageView)convertview.findViewById(R.id.limage_plus);

        convertview.setTag(holder);

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

         //On Check Box Check Events


         holder.ckbox.setOnCheckedChangeListener(null);
         holder.splus.setOnClickListener(null);
         holder.ckbox.setChecked(checkedItems[position]);
         holder.ckbox.setTag(R.id.lsave_item_box, position);
         //holder.splus.setOnClickListener(null);
         holder.lserving_qty.setText(checkedAddSub[position] ? qtysavesearch[position] :                                                  map.get(Constants.NF_SERVING_SIZE_QTY).toString());

         holder.splus.setTag(R.id.limage_plus,position);


         /*
          * Plus Btn click Listener
          */
         holder.splus.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     final int position = (Integer) v.getTag(R.id.limage_plus);

                     // holder.lserving_qty.getText(checkedAddSub[position] ? qtysavesearch[position] : map.get(Constants.NF_SERVING_SIZE_QTY).toString());

                     //counter=Math.round(Float.valueOf(serving_size_qty[position]));
                     counter=Math.round(Float.valueOf(holder.lserving_qty.getText().toString()));
                    if(counter>=1)
                    {   
                        counter++;
                        qtysavesearch[position]=String.valueOf(counter);
                        checkedAddSub[position]=true;
                        notifyDataSetChanged();

                    }
                    else
                    {
                        checkedAddSub[position]=false;
                        notifyDataSetChanged();

                    }
                }
            });

请提前帮助我

2 个答案:

答案 0 :(得分:2)

public void onShowButtonClickHandler(View v) {
        RelativeLayout rl = (RelativeLayout) v.getParent();
        LinearLayout ll = (LinearLayout) rl.getChildAt(0);
        TextView textView = (TextView) ll.getChildAt(0);
        if (!isTablet) {
            getSupportFragmentManager().beginTransaction()
                    .show(mapFragment)
                    .setCustomAnimations(R.anim.slide_out, R.anim.slide_out)
                    .hide(vehiclesFragment)
                    .commit();
            isVehiclesOpen = false;
        }
        vehicleToShowHide = textView.getText().toString();
        mapFragment.hideVehicle(vehicleToShowHide);
    }

获取parentlayout位置,然后尝试获取该子布局相对于该布局的位置,然后您将获得您点击该按钮的特定行

答案 1 :(得分:0)

您的&#34;持有人&#34;是适配器的一个字段,因此每次调用getView()方法时都会重新分配它,因此它的最终值是&#34;最后一个&#34;,这就是你获得值的原因最后一个。

如何解决您的问题?

关键是要使用加号按钮找到convertview,这样你就可以从中获得合适的持有者。 假设您的加号按钮是转换视图的直接子视图,您可以使用以下代码在onClick(View v)方法中获取正确的ViewHolder:

// Get the View Parent of the plus button, and its parent surely is the convertview.
ViewParent vp = v.getParent();
ViewHolder holder = (ViewHolder)((View)vp).getTag();
// Now you have the right holder and you can get the proper text with it.