0

我阅读了有关自定义适配器以及如何索引它们的帖子,但似乎我无法让我的工作。我覆盖了 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;
}
4

3 回答 3

1

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

于 2013-08-26T15:17:04.287 回答
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()
于 2014-09-16T12:03:30.427 回答
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;

}
于 2016-07-06T06:56:54.363 回答