-1

嗨,我有下一个表格的动态: ListView

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

我希望能够接收OnClickButton;我该怎么做:
以下方法没有帮助:

   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();

            }
        });

这个也无济于事(而且应用程序崩溃了):

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

            }
        });

我该怎么做?

4

3 回答 3

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));
}
于 2013-09-10T07:27:36.423 回答
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
                }
            });
        }
于 2013-09-10T07:29:11.320 回答
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;
    }

}

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

希望它有所帮助

于 2013-09-10T07:35:04.577 回答