ListView中的按钮onClick

时间:2013-09-10 07:15:58

标签: android listview

嗨我有动态 ListView下一个表单:

----------------------------
[TextView][Button]
----------------------------

我希望能够收到OnClick按钮;我怎么能这样做:
以下方法没有帮助:

   lv = (ListView)findViewById(R.id.listViewmain);
...
        lv.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("click on button", "click on button");
                Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

            }
        });

这个也没有帮助(此外app崩溃了):

Button b = (Button)findViewById(R.id.buttonTest);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

我该怎么做?

3 个答案:

答案 0 :(得分:1)

如果你在列表视图中有一个按钮,你甚至需要在该按钮上捕获onclick。你必须覆盖基础适配器。

public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;

public CustomBaseAdapter(Context context, List<RowItem> items) {
    this.context = context;
    this.rowItems = items;
}

/*private view holder class*/
private class ViewHolder {
    ImageView button;
    TextView txtTitle;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater)
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.button= (Button) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

holder.button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }
});

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

答案 1 :(得分:0)

// Tty this one
1. Add this class to your adapter.
static class ViewHolder {
  TextView textview;
  Button button;
}

2. Your getView() look like 
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if(convertView==null){
                convertView = LayoutInflater.from(yourActivityName.this).inflate(R.layout.yourListItemXml, null);
                holder = new ViewHolder();
                holder.textview = (TextView) convertView.findViewById(R.id.textview);
                holder.button = (Button) convertView.findViewById(R.id.button);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.textview.setText("textview value");
            holder.button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // write your start another activity code here
                }
            });
        }

答案 2 :(得分:0)

在您的活动中按如下所示创建自定义适配器

static class ViewHolder {

    public TextView text;
    public Button button;

}

private class myAdapter extends BaseAdapter {

    LayoutInflater inflater;

    public ApproversListAdapter(Context context) {

        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myarrayList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        holder.text = (TextView) convertView
                .findViewById(R.id.listitem_text);
        holder.button = (Button) convertView
        .findViewById(R.id.listitem_button);

        holder.text.setText("Your String Here");
        holder.button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                //your code to be executed on button click

            }
        });
        return convertView;
    }

}

现在创建我的适配器的一个对象,并将其用作列表适配器。

希望有所帮助