2015-03-13 53 views
0

我有一個活動,它從先前的活動中獲取數據(arraylist),並通過自定義適配器將其置於listview中。在這個自定義適配器中,我聲明瞭一個textview(顯示數據)併爲每一行添加一個數字選擇器。在列表視圖(自定義適配器)中檢索NumberPicker的值

我希望用戶能夠選擇一個數字(帶有numberpicker),當他完成後,點擊一個按鈕(fini),在textview中顯示帶有其數字選擇器值的數據。 我的活動代碼(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()); 
      } 
     } 
    } 
      ); 
} 
} 

這裏是我的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; 
}  
} 

和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來完成此操作(來自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(); 

     } 
    }); 

但我不知道如何修改我的fini.setOnClickListener()來實現同樣的結果... 任何幫助將不勝感激......

+0

按鈕處於活動和TextView的是適配器,以便當u點擊活動按鈕,在適配器中所有textviews挑選有值,並顯示在textvew是這款U想achive? – KOTIOS 2015-03-13 10:40:14

+0

@Diva並非如此:活動中有一個textview(logQr)。我想將(numberpicker的數據+值)發送到該文本視圖。我知道如何獲得數據,但我不知道如何引用numberpicker的值... – 2015-03-13 11:09:03

回答

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

} 
相關問題