带有数字选择器的Android列表视图

时间:2013-08-18 16:28:32

标签: android listview android-listview datapicker

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

列表视图代码是:

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

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

通过扩展BaseAdapter创建自己的适配器,这应该使一切更加清晰

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

内容的对象

答案 2 :(得分: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
}