Android ListView onClickListener自定义适配器

时间:2013-08-26 15:07:15

标签: android listview adapter onclicklistener

我阅读有关自定义适配器的帖子以及如何索引它们,但似乎我不能让我的工作。我覆盖了getView,我的XML包含1个TextView和2个按钮。我认为onClickListener检测到这两个按钮但是我无法区分哪个ListView元素是触发ClickEvent的那个元素。我尝试了不同的方法,但我总是在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;
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我认为你的错误在于:

v.getTag()

您正试图从按钮视图中获取标记,您应该从列表项中获取它。 假设您的按钮直接位于列表项视图中,您应该获得按钮的父视图并从那里获取标记。

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

如果你想要项目的位置,将int Position添加到你的ViewHolder类,你会得到这样的结果:

int position = viewHolder.Position;

您的ViewHolder类看起来像这样:

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

要获取按钮ID,您只需执行以下操作:

v.getId()

答案 2 :(得分:0)

如果在getView中使用匿名内部侦听器,将会容易得多。从长远来看,它将使生活更加轻松。虽然在非常繁重的列表项中不能延迟(不到一秒)。

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

}