-1

こんにちは、私は次のフォームで動的にしています: 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();

            }
        });

これも役に立ちません(さらにアプリがクラッシュします):

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 に答える