0

我正在使用列表视图。我在该列表视图中添加了一个 Textview 和按钮。如何知道单击了哪个按钮。当我单击该按钮时,我需要获取相关文本?

谁能帮我?

4

2 回答 2

1

请参阅下面的示例代码...您从中得到想法..

这里 list_v 是列表视图

list_v.setAdapter(new ListViewAdapter_test(
                                this));

public class ListViewAdapter_test extends BaseAdapter {

        private LayoutInflater mInflater;

        public ListViewAdapter_test(Context con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

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

        public Object getItem(int position) {
            // TODO Auto-generated method stub

            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub

            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.scan_row1, null);
                holder = new ListContent();

                holder.name = (TextView) v.findViewById(R.id.sc_textname);


                holder.total_rate = (Button) v.findViewById(R.id.button1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.name.setId(position);

            holder.total_rate.setId(position);

            holder.total_rate.setOnClickListener(mOnTitleClickListener3);

            try {

                holder.name.setText("" + a_product_name.get(position));

                holder.total_rate.setText("Read " + a_totreviews.get(position)
                        + " reviews");

            } catch (Exception e) {
                // TODO: handle exception

            }

            return v;
        }
    }

    static class ListContent {



        TextView name;

        Button total_rate;

    }

public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
        public void onClick(View v) {
            final int position = list_v
                    .getPositionForView((View) v.getParent());

            /** do your code here whatever you want */

        }
    };
于 2012-07-06T08:21:13.833 回答
0

如果您不想使用列表项单击并且想要添加按钮,当您构建单元格并添加按钮时,您可以使用 button.setTag(int) 为其设置标签,您可以将标签设置为单元格的位置,因此在按钮的单击侦听器中,您可以检索使用 button.getTag() 单击的按钮的位置,如果您有一个包含文本行的数据结构,则可以检索文本

于 2012-07-06T08:02:13.047 回答