简体   繁体   English

Android ListView onClickListener自定义适配器

[英]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. 我覆盖了getView,我的XML包含1个TextView和2个按钮。 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. 我确定两个按钮都被onClickListener检测到,但是我无法区分哪个ListView元素是触发ClickEvent的那个。 I tried i different approach but i always get a NullPointerException in the onClick Method. 我尝试了不同的方法,但是在onClick方法中始终会收到NullPointerException。

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

尝试使用getPositionForView(v)查找与按下的按钮相关的位置。

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类中,您将像这样获得它:

int position = viewHolder.Position;

Your ViewHolder class would look like this: 您的ViewHolder类如下所示:

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

To get the button id you simply need to do: 要获取按钮ID,您只需要做:

v.getId()

It will be much much easier if you use anonymous inner listeners inside the getView. 如果在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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM