在listview中检索NumberPicker的值(自定义适配器)

时间:2015-03-13 10:34:12

标签: android android-listview android-adapter numberpicker

我有一个活动从前一个活动中获取数据(arraylist),并通过自定义适配器将其放入listview中。在这个自定义适配器中,我声明了一个textview(显示数据)并为每一行添加一个数字选择器。

我希望用户能够选择一个数字(带有numberpicker),当他完成时,点击一个按钮(fini),在textview中显示带有numberpicker值的数据。 我的活动代码(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());
            }
        }
    }
            );
}
}

以下是我的惯例的代码:

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;
}       
}

与适配器相关的XML:

<?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>

到目前为止,我设法使用setOnItemClickListener执行此操作,如此代码中所述(来自活动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();

        }
    });

但我不知道如何修改我的fini.setOnClickListener()以达到相同的效果...... 任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:1)

最后自己找到解决方案。

    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);}

}