8

我的屏幕上有一个列表视图,每个列表项都包含 2 个文本视图和一个按钮。单击按钮时,我想获取列表项选定的索引以从向量中获取一些数据。

这是我的列表自定义适配器。但我不知道该怎么做。

private class CustomAdapter extends ArrayAdapter<ServicesItems> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<ServicesItems> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        TextView item = null;
        TextView description = null;
        Button subNowBtn;

        ServicesItems ii = getItem(position);

        if (null == convertView) {
            convertView = mInflater.inflate(
                    R.layout.list_of_servics_item_2, null);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        // cat_name = holder.gettitle();

        Resources resources = getResources();

        ServicesItems bean1 = (ServicesItems) servicesVector
                .elementAt(position);

        String cat_name_str = bean1.getService_name().toString();
        String descreption = bean1.getDescription().toString();


        item = holder.getItem();
        item.setText(bean1.getDescription());

        description = holder.getDescription();
        description.setText(bean1.getService_name());

         subNowBtn=holder.getSubButton();

        return convertView;
    }

    private class ViewHolder {
        private View mRow;
        private TextView description = null;
        private TextView item = null;
        private Button sub = null;

        public ViewHolder(View row) {
            mRow = row;
        }

        public TextView getDescription() {
            if (null == description) {
                description = (TextView) mRow
                        .findViewById(R.id.category_tv);
            }
            return description;
        }

        public TextView getItem() {
            if (null == item) {
                item = (TextView)       
           mRow.findViewById(R.id.descreption_tv);
            }
            return item;
        }

        public Button getSubButton(){
            if(null==sub){
                sub=(Button)findViewById(R.id.subscribe_now_btn);


            }
            return sub;
        }

    }

}
4

2 回答 2

9

在您的适配器中试试这个:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;

    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // What you want
        }
    });

    return row;
}

编辑

你的包装类:

public class YourWrapper
{
    private View base;
    private Button button;

    public YourWrapper(View base)
    {
        this.base = base;
    }

    public Button getButton()
    {
        if (button == null)
        {
            button = (Button) base.findViewById(R.id.your_button);
        }
        return (button`);
    }
}

换成sub=(Button)findViewById(R.id.subscribe_now_btn);_sub=(Button) mRow.findViewById(R.id.subscribe_now_btn);

于 2012-11-24T16:51:19.830 回答
5

您可以在单击按钮时获取列表视图的索引,这里有两个示例:-

您可以在您onClick的侦听器方法中编写这些。

示例 1

View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);

示例 2

setTag创建视图时使用设置位置

mybutton.setTag(position);

在侦听器中获取位置

int position = (Integer) view.getTag();

希望这有帮助:)

于 2015-07-09T07:39:33.280 回答