2013-12-17 125 views
0

我想將點擊事件添加到自定義列表視圖中的兩個按鈕中。我的自定義列表視圖擴展了Base Adapter。我不知道該怎麼寫。請幫幫我 。 這裏是我的適配器代碼:我也讀過一些其他帖子,但他們不適合我。帶按鈕點擊事件的自定義列表視圖

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.media.Image; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 



public class CustomAdapter extends BaseAdapter { 


private Activity activity; 
private ArrayList data; 
private static LayoutInflater inflater=null; 
public Resources res; 
ListModel getList = null; 
int i=0; 

public CustomAdapter(Activity a, ArrayList data,Resources resLocal) { 

    activity = a; 
    this.data = data; 
    res = resLocal; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

public int getCount() { 

    if(data.size() <= 0) 
     return 1; 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 


public static class ViewHolder{ 

    public TextView txt_Name; 
    public TextView txt_Status; 
    public ImageButton imgbtn_senka; 
    public ImageButton imgbtn_fuenka; 
    public TableRow tblrow_status; 
    //public TableRow tblrow_btn; 
    public RelativeLayout layoutbtn; 
    public ImageView image; 


} 


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

    View vi = convertView; 
    ViewHolder holder; 

    if(convertView==null){ 

     //Inflate tabitem.xml file for each row 
     vi = inflater.inflate(R.layout.tabitem, null); 

     holder=new ViewHolder(); 
     holder.txt_Name = (TextView)vi.findViewById(R.id.txtmemberName); 
     holder.txt_Status = (TextView)vi.findViewById(R.id.txtmemberStatus); 
     holder.image = (ImageView)vi.findViewById(R.id.imgPlayer); 
     holder.imgbtn_fuenka = (ImageButton) vi.findViewById(R.id.btnfusenka); 
     holder.imgbtn_senka = (ImageButton) vi.findViewById(R.id.btnsenka); 
     holder.tblrow_status = (TableRow)vi.findViewById(R.id.tblrowstatus); 
     holder.layoutbtn = (RelativeLayout)vi.findViewById(R.id.layoutbutton); 


     vi.setTag(holder); 
    } 
    else 
     holder=(ViewHolder)vi.getTag(); 

    if(data.size() <= 0){ 

     holder.txt_Name.setText("No Data"); 

    } 
    else{ 

     getList = null; 
     getList = (ListModel) data.get(position); 
     if(getList.getMemberStatus() == 3){ 


      holder.tblrow_status.setVisibility(View.INVISIBLE); 
      holder.layoutbtn.setVisibility(View.VISIBLE); 
      if(getList.getEntry() == 0){ 
        Log.i("abc", getList.getEntry()+""); 
       //holder.imgbtn_fuenka.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/senka.png",null,null)); 
       holder.imgbtn_senka.setImageResource(R.drawable.fusanka); 
       holder.imgbtn_senka.setEnabled(false); 
      } 

     } 

     else{ 


      holder.tblrow_status.setVisibility(View.VISIBLE); 
      holder.layoutbtn.setVisibility(View.INVISIBLE); 
      if(getList.getEntry() == 0){ 
       holder.txt_Status.setText("未確認"); 
      } 
      else if(getList.getEntry() == 1){ 
       holder.txt_Status.setText("參加"); 
      } 

      else if(getList.getEntry() == 2){ 
       holder.txt_Status.setText("不參加"); 
      } 
      else if(getList.getEntry() == 3){ 
       holder.txt_Status.setText("取消"); 
      } 
     } 


     holder.txt_Name.setText(getList.getMemberName()); 
     String url="http://10.0.2.2/football365/Photo/"+getList.getImage(); 

     try { 
      Bitmap bitmap= BitmapFactory.decodeStream((InputStream) new URL(url).getContent()); 
      holder.image.setImageBitmap(bitmap); 
     } 
     catch (MalformedURLException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
     // holder.image.setImageResource(res.getIdentifier("http://10.0.2.2/football365/Photo/"+getList.getImage(),null,null)); 



    } 




    return vi; 
} 



} 
+0

_「問題請求代碼必須展示對正在解決的問題的最小理解,包括嘗試解決方案,爲什麼他們不工作以及預期的結果。「_因此,請提供更多信息。 –

回答

0

使用如下代碼:

 if(convertView==null){ 

    //Inflate tabitem.xml file for each row 
    vi = inflater.inflate(R.layout.tabitem, null); 

    holder=new ViewHolder(); 
    holder.txt_Name = (TextView)vi.findViewById(R.id.txtmemberName); 
    holder.txt_Status = (TextView)vi.findViewById(R.id.txtmemberStatus); 
    holder.image = (ImageView)vi.findViewById(R.id.imgPlayer); 
    holder.imgbtn_fuenka = (ImageButton) vi.findViewById(R.id.btnfusenka); 
    holder.imgbtn_senka = (ImageButton) vi.findViewById(R.id.btnsenka); 
    holder.tblrow_status = (TableRow)vi.findViewById(R.id.tblrowstatus); 
    holder.layoutbtn = (RelativeLayout)vi.findViewById(R.id.layoutbutton); 
    // add this line 
    holder.imgbtn_fuenka.setOnClickListener(this); 
    holder.imgbtn_senka.setOnClickListener(this); 
    // do for how many object that you want 


    vi.setTag(holder); 
} 

和的onClick方法:

@Override 
public void onClick(View v) { 
    switch (v.getId()) 
    { 
     case R.id.btnfusenka: 

     break; 
     case R.id.btnsenka: 

     break; 
    } 

必須導入後續行,

import View.OnClickListener 
+0

方法setOnClickListener(View.OnClickListener)類型View不適用爲參數(CustomAdapter)。這就是顯示的錯誤 – user3032822

+0

你在CustomAdapter類中實現了OnClickListener嗎? –

+0

你必須導入後續行,'import View.OnClickListener' –

0

您的return前加入getView()

holder.imgbtn_senka.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Do your imgbtn_senka buttons stuff 

      } 
     }); 
+0

我已經試過這個。不工作。感謝關心。 – user3032822

+0

真的,你可以發佈該代碼嗎? – SathishKumar

0

使用此代碼裏面getview

holder.imgbtn_fuenka.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      // write your code here 

     } 
    }); 

使用該屬性列表視圖。

android:descendantFocusability="afterDescendants" 
0

所有你需要做的是遵循:

1.In的getView()函數把view.onClickListener (假設它的一個按鈕),所以寫

holder.BUTTON_NAME.setON(new View.onClickListener) 

      @Override 
      public void onClick(View v) { 
       // Do your imgbtn_senka buttons stuff 

      } 
     }); 
相關問題