2013-05-02 96 views
0

我顯示Listview與一些電影和右側顯示按鈕下載,但當我滾動列表查看然後按鈕事件交換(重繪)每次,我有設置按鈕文本取消時,用戶按下載按鈕和下載開始,但向下滾動時列表然後按鈕文本更改與下載和事件獲取交換的初始名稱。Android如何在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(); 
     } 




     } 
    }); 

} 
+0

如何更改名稱?郵政編碼。 – 2013-05-02 12:57:20

+0

你應該看看我的新庫,這使得寫入適配器變得更容易..當然包括你需要的 - 在你的項目中添加可點擊的按鈕。 http://amigold.github.io/FunDapter/ – Ami 2013-05-20 19:57:35

回答

0

你應該在ListView中使用視圖持有者,或者你必須在充氣UI之前檢查convertview null。

相關問題