将额外值传递给getView onClick方法

时间:2016-11-16 11:25:28

标签: android listview

对不起,这里只有一个菜鸟,我的老板从前开发者那里把这个交给我,我对于android没什么好看的。我的问题是如何在getView>中传递额外的动态值? onClick方法让我可以访问。

categoryCursorAdaptor

class categoryCursorAdaptor extends BaseAdapter {

    Context context;
    String[] data;
    private static LayoutInflater inflater = null;

    public categoryCursorAdaptor(Context context, String[] data) {
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) vi = inflater.inflate(R.layout.item_category, null);
        TextView text = (TextView) vi.findViewById(R.id.specialCatItemName);
        text.setText(data[position]);
        Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
        Button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //Access extra values here
            }
        });
        return vi;
    }
}

CategoryActivity

    String[] catValsArr = new String[arrayCatVals.size()];
    catValsArr = arrayCatVals.toArray(catValsArr);
    lvSpecialCatList = (ListView) findViewById(R.id.lvSpecialCatList);
    lvSpecialCatList.setAdapter(new categoryCursorAdaptor(this, catValsArr));

1 个答案:

答案 0 :(得分:2)

position内已有getView(....),因此请使用此String Array

获取正确的数据
String myData = data[position]; //Use myData
<{1>}

下的