2013-08-18 14 views
0

我正在尋找教程,瞭解如何在我的Android列表視圖的所有行上添加數字選擇器。Android的列表視圖與數字選擇器

ListView控件代碼:

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

你說的numberpicker是什麼意思? – Pavlos

+0

我需要像這樣的列表視圖http://img706.imageshack.us/img706/7056/nti8.jpg – Igor

+0

您必須爲列表視圖創建自己的適配器,然後相應地填充它!它不會很容易做到適配器! 你可以在這裏學習基礎知識http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – Pavlos

回答

0

創建自己的適配器,通過擴展BaseAdapter,這應該使一切更清楚

充氣,getView()的列表視圖中,你可以做所有定製這裏。 getItem(int index)將返回包含列表項目內容的對象

+0

你能更清楚一點嗎?添加一些代碼或鏈接一些教程? – Igor

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> 
+0

感謝我的朋友,它像一個魅力!!!! – Igor

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 
}