Android如何在listview中添加可点击按钮?

时间:2013-05-02 12:56:26

标签: android listview adapter

我正在显示Listview的一些电影和右侧显示按钮供下载但是当我每次滚动Listview然后按钮事件交换(重新绘制)时,我已经设置按钮文本取消当用户按下载按钮并下载开始但滚动时向下列表然后按钮文本更改初始名称与下载和事件获得交换。

 static class listitemcontainers
{
     //ImageView imgIcon;
     //TextView txtTitle;
     ProgressBar pb;
     TextView progressval;
     Button downloadbutton;
     TextView title;
     ImageView imageicon;
     LinearLayout layout; 
}

       @Override
public View getView(int position, View convertview,final ViewGroup parent) {
    // TODO Auto-generated method stub
    final listitemcontainers holder;
    Log.d("Game Name in adapter",":"+GameListname.get(position));

     View vi=convertview;

     final listitemcontainers holdz;
    RelativeLayout RL;

    if(convertview==null)
    {

        convertview = inflater.inflate(R.layout.list_row, null);    

         holder = new listitemcontainers();

          holder.title = (TextView)convertview.findViewById(R.id.game_name);
          holder.imageicon=(ImageView)convertview.findViewById(R.id.listicon);

             RL=(RelativeLayout)convertview.findViewById(R.id.game_row);
             holder.layout = (LinearLayout )convertview.findViewById(R.id.dynamic);

               holder.progressval=(TextView)convertview.findViewById(R.id.progressval);
               holder.pb=(ProgressBar)convertview.findViewById(R.id.progressBar1);
               holder.pb.setVisibility(View.INVISIBLE);

               holder.downloadbutton=(Button)convertview.findViewById(R.id.button1);

         convertview.setTag(holder);

    final listitemcontainers hold = holder;

       //setButtonClick(hold );


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

      }


          holder.title.setText(GameListname.get(position));

          holder.imageicon.setImageDrawable(GameIcon.get(position));

          holder.downloadbutton.setText("Dwnload"+position);

          holder.downloadbutton.setId(position);

          holder.pb.setId(position+1000);


          setButton(holder,convertview,position);


       return convertview;
}
 int globalid;

   public void setButton(final listitemcontainers holder,final View v,final int id){
            holder.downloadbutton=(Button)v.findViewById(id);
            holder.pb=(ProgressBar)v.findViewById(id+1000);
          holder.downloadbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(activity, "Click By:"+id, Toast.LENGTH_SHORT).show();
            if(checkflag==true){
                checkflag=false;
                globalid=id;
             UpdateProgresBar.listcntr=holder;


          up=new UpdateProgresBar(holder.pb,activity,v);
          up.execute("");

          CancleButtonClick(holder,v,id);
            }

         else{
             Toast.makeText(activity, "Please wait", Toast.LENGTH_SHORT).show();
         }




        }
    });

}

1 个答案:

答案 0 :(得分:0)

你应该在Listview中使用视图持有者,否则你必须在扩充UI之前正确检查convertview null。