Search code examples
javaandroidcustom-lists

Custom List view with button click event


I would like to add click event into two button which are in Custom List View . My custom list view extends Base Adapter . I don't know how to write it . Help me Please . Here's is my Adapter Code : I also read some other posts but they don't work for me .

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;
}



}

Solution

  • use follow code:

          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);
    }
    

    and in onClick method:

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

    you must import follow line,

      import View.OnClickListener