一行中的按钮单击事件会影响android

时间:2015-06-24 10:43:58

标签: android android-listview

我在listview的每一行都有发送按钮。当我们点击发送按钮时,应该更改该特定行项目的背景图像(图像更改以传送完成的图像)​​。这里我可以通过写入更改背景图像代码为setBackgroundResource.but问题是它会影响listview中的其他行项目,这意味着当我滚动时,其特定行项目的其他按钮图像也会更改。我已经尝试过去三天这个问题但没有用请给我这样做的建议。

public View getView(final int position, View convertView, ViewGroup parent) {

          ![enter image description here][1]
            final ViewHolder holder;
           holder = new ViewHolder();


           if (convertView == null) {
               LayoutInflater vi = getLayoutInflater();
           convertView = vi.inflate(R.layout.ambassdor_orders_list_fields, null);
           }

           Routes rou=RouteList.get(position);

           holder.route = (Button) convertView.findViewById(R.id.deliverBtn);
           holder.memberName = (TextView) convertView.findViewById(R.id.memberNameTxt);
           holder.amount = (TextView) convertView.findViewById(R.id.amountTxt);

           holder.memberName.setText(rou.getMemberName());
           holder.amount.setText(rou.getAmount());

           holder.route.setTag(position);
           holder.memberName.setTag(position);
           holder.amount.setTag(position);

           convertView.setTag(holder);

           holder.route.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                holder.route.setBackgroundResource(R.drawable.done);                          

            }
            });
           convertView.setTag(R.id.routeButtton, holder.route);
           convertView.setTag(R.id.memberNameTxt, holder.memberName);
           convertView.setTag(R.id.amountTxt, holder.amount);
           return convertView;
     }  

3 个答案:

答案 0 :(得分:1)

    public View getView(final int position, View convertView, ViewGroup parent) {

                final ViewHolder holder;
                holder = new ViewHolder();


               if (convertView == null) {
                   LayoutInflater vi = getLayoutInflater();
                   convertView = vi.inflate(R.layout.ambassdor_orders_list_fields, null);

                   convertView.setTag(holder);

               } else {
                   holder = (ViewHolder) convertView.getTag();
               }

               Routes rou=RouteList.get(position);

               holder.route = (Button) convertView.findViewById(R.id.deliverBtn);
               holder.memberName = (TextView) convertView.findViewById(R.id.memberNameTxt);
               holder.amount = (TextView) convertView.findViewById(R.id.amountTxt);

               holder.memberName.setText(rou.getMemberName());
               holder.amount.setText(rou.getAmount());

               holder.route.setOnClickListener(new OnClickListener() {                  public void onClick(View v) {                                  
                          holder.route.setBackgroundResource(R.drawable.done);                          

                }
                });
               return convertView;
         }  

根据您的代码,您应该保留一个字段来表示模型数据本身(路由)中路由按钮背景的状态。以便添加代码以切换具有此状态的背景。

当您点击按钮时,只需更新状态(例如,rou.setDone(true))

getView 中,您可以添加以下代码来更新此

if(rou.getDone()) {
//setBackground
} else {
//revert background
}
notifyDatasetChanged()

答案 1 :(得分:0)

这是我的代码

 if(rou.isDone()) {
         //setBackground
            holder.route.setBackgroundResource(R.drawable.deliver_done);

         } else {
         //revert background
             holder.route.setBackgroundResource(R.drawable.deliver_not_done);
         }
         notifyDataSetChanged();



       holder.route.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            rou.setDone(true);

        }
        });

答案 2 :(得分:0)

通过更改此行

解决了我的问题
 if(rou.isDone()) {
     //setBackground
        holder.route.setBackgroundResource(R.drawable.deliver_done);

     } else {
     //revert background
         holder.route.setBackgroundResource(R.drawable.deliver_not_done);
     }




   holder.route.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        rou.setDone(true);
        notifyDataSetChanged();

    }
    });