Search code examples
androidandroid-listviewandroid-adapternumberpicker

retrieve values of NumberPicker in listview (custom adapter)


I have an activity that get data (arraylist) from a previous activity and put it in a listview through a custom adapter. In this custom adapter i declare a textview (to show the data) and add a numberpicker for each row.

I want the user to be able to select a number (with numberpicker) and when he has finished, to click on a button (fini) that display data with its numberpicker value in a textview. My activity code (choozQr3.java):

public class ChoozQr3 extends Activity {
Button fini;    
ListView lv;
TextView logQr;
Context context;
ChoozQr3Adapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.choozqr_step3);

    fini = (Button) findViewById(R.id.fini);
    lv = (ListView) findViewById(R.id.listView1);   
    logQr = (TextView) findViewById(R.id.textView2);

    Bundle b = getIntent().getExtras(); 
    String[] sTemp = b.getStringArray("arrangedItems");
    ArrayList<String> resultArr = new ArrayList<String>(Arrays.asList(sTemp));

    adapter = new ChoozQr3Adapter(this, R.layout.choozqr_step3_textview, resultArr);
    lv.setAdapter(adapter);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    fini.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //ArrayList<String> finalItems = new ArrayList<String>();
            for (int i = 0; i < adapter.getCount(); i++) {
                Toast.makeText(getApplicationContext(),i+". " +adapter.getItem(i).toString(),Toast.LENGTH_LONG).show();
            //finalItems.add(adapter.getItem(i).toString());
            }
        }
    }
            );
}
}

Here is the code of my customadapter :

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

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

@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.choozqr_step3_textview, parent, false);
        //ContentValues cv = new ContentValues();
    }

    TextView tvNomDuQr=(TextView)v.findViewById(R.id.NomQr);
    NumberPicker npNbJours=(NumberPicker)v.findViewById(R.id.numberPickerOccurence);

    tvNomDuQr.setText(list.get(position));
    npNbJours.setMaxValue(365);
    npNbJours.setMinValue(1);
    npNbJours.setValue(1);
    npNbJours.setWrapSelectorWheel(true);

    return v;
}       
}

And the XML related to the adapter :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >

<TextView
    android:id="@+id/NomQr"
    android:layout_width="400dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<NumberPicker
    android:id="@+id/numberPickerOccurence"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:layout_alignTop="@+id/NomQr"
    android:layout_marginLeft="20dp"
    android:layout_toLeftOf="@+id/textView1"        
    android:orientation="horizontal" />

<TextView
android:id="@+id/textView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:text="jours"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Until now, i managed to do it with setOnItemClickListener as described in this code (from activity choozQr3.java) :

lv.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3){
            // when user clicks on ListView Item , onItemClick is called
            // with position and View of the item which is clicked
            // we can use the position parameter to get index of clicked item
            TextView tvNomDuQr=(TextView)v.findViewById(R.id.NomQr);
            NumberPicker npNbJours=(NumberPicker)v.findViewById(R.id.numberPickerOccurence);
            String NomDuQr=tvNomDuQr.getText().toString();
            Integer NbJours=npNbJours.getValue();

            // Show The result
            Toast.makeText(getApplicationContext(),"répéter "+NbJours+" fois le questionnaire "+NomDuQr,Toast.LENGTH_LONG).show();

        }
    });

But i don't know how to modify my fini.setOnClickListener () to achieve same result... Any help would be greatly appreciated...


Solution

  • Finally find the solution by myself.

        public void onClick(View v) {
        ArrayList<String> QrEtOccurence = new ArrayList<String>();
        TextView tvNomDuQr;
        NumberPicker npNbJours;
        Integer j = 0;
    
        for (int i = 0; i < lv.getCount(); i++) {
            v = lv.getChildAt(i);
            tvNomDuQr=(TextView)v.findViewById(R.id.NomQr);
            npNbJours=(NumberPicker)v.findViewById(R.id.numberPickerOccurence);
            String NomDuQr=tvNomDuQr.getText().toString();
            Integer NbJours=npNbJours.getValue();                       
            String output = "Présenter durant "+NbJours+" jours le questionnaire "+NomDuQr;
            QrEtOccurence.add(output);}         
    
        String[] outputStrArr = new String[QrEtOccurence.size()];
        for (int i = 0; i < QrEtOccurence.size(); i++) {
            outputStrArr[i] = QrEtOccurence.get(i);}
    
    }