0

我正在寻找一个教程来学习如何在我的 Android 列表视图的所有行上添加一个数字选择器。

列表视图代码是:

 ListView barcodeList = (ListView) findViewById(R.id.listView1);
                barcodeList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bcResultArray));
4

3 回答 3

2

我从未使用过数字选择器,但我想它会像其他所有东西一样工作。

您需要为自己创建一个适配器。在ArrayAdapter的getView()方法中,您可以简单地扩展布局,而不是使用例如 android.R.layout.simple_list_item_1

public class MyXYZAdapter extends ArrayAdapter<XYZ> {
    //other stuff
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater li = (LayoutInflater)    
            c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.list_item_xyz, null);
        }

        //Object o = v.findViewById(...);

        return v;

    }
    //other stuff
}

现在您需要创建list_item_xyz.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout [...] >

    <TextView
        [...] />

    <TextView
        [...] />

    <NumberPicker
        [...] />


</RelativeLayout>
于 2013-08-18T17:21:14.227 回答
0

创建你自己的适配器,通过扩展 BaseAdapter,这应该让一切更清楚

在 getView() 中扩充列表视图,您可以在此处进行所有自定义。getItem(int index) 将返回包含列表项内容的对象

于 2013-08-18T16:34:15.063 回答
0

它奏效了,我将在这里分享代码,因为我做了一些小改动。listalayout.xml 只有我想出现在每一行的组件(包括 numberPicker)

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

    // We came from the scanning activity; the return intent contains a RESULT_EXTRA key
    // whose value is an ArrayList of BarcodeResult objects that we found while scanning.
    // Get the list of objects and add them to our list view.
    if (resultCode == RESULT_OK) 
    {           

        ArrayList<BarcodeResult> barcodes = data.getParcelableArrayListExtra(BarcodeScanActivity.RESULT_EXTRA);
        if (barcodes != null)
        {
            for (int i =0;i<barcodes.size();i++)
            {
                bcResultArray.add(barcodes.get(i).barcodeString);

            }

            ListView barcodeList = (ListView) findViewById(R.id.listView1);
            ListAdapter customAdapter = new MyXYZAdapter(this, R.layout.listalayout, bcResultArray);   
            barcodeList.setAdapter(customAdapter);
        }
    }
}

public class MyXYZAdapter extends ArrayAdapter<String> {
    private final List<String> list;

    public MyXYZAdapter(Context context, int resource, List<String> items) {
        super(context, resource, items);
        list = items;

    }

    //other stuff
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {

            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.listalayout, null);

        }

        TextView tv1 = (TextView) v.findViewById(R.id.lltitulo);
       tv1.setText(list.get(position));
       NumberPicker np = (NumberPicker) v.findViewById(R.id.numberPicker1);
       np.setMaxValue(999);
       np.setMinValue(0);
       np.setValue(1);
        return v;

    }
    //other stuff
}
于 2013-08-18T22:11:33.317 回答