Stu Pitt
Stu Pitt

Reputation: 45

Android ListView onClickListener Custom Adapter

I read posts about custom adapters and how to index them but it seems i cannot make mine work. I overwrite the getView and my XML contains 1 TextView and 2 Buttons. I made it that both buttons were detected by the onClickListener however i couldnt differentiate which ListView element was the one who triggered the ClickEvent. I tried i different approach but i always get a NullPointerException in the onClick Method.

    @Override
public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder;      
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(R.layout.listexample, null);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.commandLine_text);
        holder.start = (Button) convertView.findViewById(R.id.test_start_button);
        holder.stop = (Button) convertView.findViewById(R.id.test_stop_button);
        convertView.setTag(holder);
        convertView.findViewById(R.id.commandLine_text);
        convertView.findViewById(R.id.test_start_button);
        convertView.findViewById(R.id.test_stop_button);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.textView.setText(this.getItem(position));
    holder.start.setOnClickListener(this);
    holder.stop.setOnClickListener(this);
    return convertView;

}
@Override
public void onClick(View v) {
 //Here i want to know which button of the two (start,stop) was clicked and what position
    int position =(Integer)v.getTag();
    Log.d("OnClick","Position: "+position);

}
static class ViewHolder {
    TextView textView;
    Button start;
    Button stop;
}

Upvotes: 0

Views: 7524

Answers (3)

Kushan
Kushan

Reputation: 5984

It will be much much easier if you use anonymous inner listeners inside the getView. It will make life much easier in the long run. Although it cant take some lag(less than a sec) in very heavy list items.

@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;      
if(convertView == null){
    LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.listexample, null);
    holder = new ViewHolder();
    holder.textView = (TextView) convertView.findViewById(R.id.commandLine_text);
    holder.start = (Button) convertView.findViewById(R.id.test_start_button);
    holder.stop = (Button) convertView.findViewById(R.id.test_stop_button);
    convertView.setTag(holder);
    convertView.findViewById(R.id.commandLine_text);
    convertView.findViewById(R.id.test_start_button);
    convertView.findViewById(R.id.test_stop_button);

} else {
    holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(this.getItem(position));
holder.start.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v) {
   //Here i want to know which button of the two (start,stop) was clicked and what position

   Log.d("OnClick start","Position: "+position);

   }
});

holder.stop.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v){
      Log.d("OnClick stop","Position: "+position);
   }

});
return convertView;

}

Upvotes: 0

EZDsIt
EZDsIt

Reputation: 931

I think your mistake is in:

v.getTag()

You are trying to get the tag from the button view and you should get it from the list item. Assuming your buttons lie directly on the list item view, you should get the parent view of the button and get the tag from there.

View parentView = (View)v.getParent();
ViewHolder viewHolder = (ViewHolder)parentView.getTag();

If you want the item's position, add int Position to your ViewHolder class and you would get it like this:

int position = viewHolder.Position;

Your ViewHolder class would look like this:

static class ViewHolder 
{
    TextView textView;
    Button start;
    Button stop;
    int Position;
}

To get the button id you simply need to do:

v.getId()

Upvotes: 0

Karakuri
Karakuri

Reputation: 38585

Try using getPositionForView(v) to find the position that correlates to the button that was pressed.

Upvotes: 1

Related Questions