Android listview with number picker

3.4k views Asked by At

I am looking for a tutorial to learn how to add a number picker on all the rows of my Android list view.

The listview code is:

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

There are 3 answers

1
Casper-Nodes On

Create your own adapter, by extending BaseAdapter, this should make everything more clear

Inflate the list views in getView(), and you can do all customization here. getItem(int index) will return the object with content for list item

0
Igor On

It worked, I will share the code here because I did some small changes. The listalayout.xml just have the components that I want to appear in each line (including the 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
}
0
David On

I have never used a number picker, but I guess it will work like everything else.

You need to create yourself an adapter. In the getView() methode of your ArrayAdapter you can simply inflate a layout instead of using e.g. 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
}

Now you need to create the list_item_xyz.xml layout file:

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

    <TextView
        [...] />

    <TextView
        [...] />

    <NumberPicker
        [...] />


</RelativeLayout>