2012-11-20 62 views
1

我有一個listview(多選)。 它完美的作品。我使用了一個自定義適配器。每行都有一個文本和一個複選框。 已標記的項目將作爲結果顯示在文本視圖中。Spinner in listview

我想在listview行內添加一個微調器。我得到零點異常錯誤。 在此先感謝。

public class MainActivity extends Activity { 

ListView myListView; 
Button getResult; 
Spinner spinner; 

private ArrayList<String> dayOfWeekList = new ArrayList<String>(); 

private void initDayOfWeekList(){ 
    dayOfWeekList.add("Sunday"); 
    dayOfWeekList.add("Monday"); 
    dayOfWeekList.add("Tuesday"); 
    dayOfWeekList.add("Wednesday"); 
    dayOfWeekList.add("Thursday"); 
    dayOfWeekList.add("Friday"); 
    dayOfWeekList.add("Saturday"); 

} 

String[] androidBooks = 
    { 
    "Hello", 
    "Professional", 
    "Unlocking" 
    }; 

MyArrayAdapter myArrayAdapter; 
ArrayAdapter<String> spinnerAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    initDayOfWeekList(); 
    setContentView(R.layout.activity_main); 
    spinnerAdapter = new ArrayAdapter<String> (this, 
      android.R.layout.simple_spinner_item, androidBooks); 




    myListView = (ListView)findViewById(R.id.list); 

    myArrayAdapter = new MyArrayAdapter(
      this, 
      R.layout.row, 
      android.R.id.text1, 
      dayOfWeekList 
      ); 

    myListView.setAdapter(myArrayAdapter); 
    myListView.setOnItemClickListener(myOnItemClickListener); 

    getResult = (Button)findViewById(R.id.getresult); 
    getResult.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      String result = ""; 

      /* 
      //getCheckedItemPositions 
      List<Integer> resultList = myArrayAdapter.getCheckedItemPositions(); 
      for(int i = 0; i < resultList.size(); i++){ 
       result += String.valueOf(resultList.get(i)) + " "; 
      } 
      */ 

      //getCheckedItems 
      List<String> resultList = myArrayAdapter.getCheckedItems(); 
      for(int i = 0; i < resultList.size(); i++){ 
       result += String.valueOf(resultList.get(i)) + "\n"; 
      } 

      myArrayAdapter.getCheckedItemPositions().toString(); 
      Toast.makeText(
        getApplicationContext(), 
        result, 
        Toast.LENGTH_LONG).show(); 
     }}); 

} 

OnItemClickListener myOnItemClickListener 
= new OnItemClickListener(){ 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     myArrayAdapter.toggleChecked(position); 

    }}; 

private class MyArrayAdapter extends ArrayAdapter<String>{ 

    private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>(); 

    public MyArrayAdapter(Context context, int resource, 
      int textViewResourceId, List<String> objects) { 
     super(context, resource, textViewResourceId, objects); 

     for(int i = 0; i < objects.size(); i++){ 
      myChecked.put(i, false); 
     } 
    } 

    public void toggleChecked(int position){ 
     if(myChecked.get(position)){ 
      myChecked.put(position, false); 
     }else{ 
      myChecked.put(position, true); 
     } 

     notifyDataSetChanged(); 
    } 

    public List<Integer> getCheckedItemPositions(){ 
     List<Integer> checkedItemPositions = new ArrayList<Integer>(); 

     for(int i = 0; i < myChecked.size(); i++){ 
      if (myChecked.get(i)){ 
       (checkedItemPositions).add(i); 
      } 
     } 

     return checkedItemPositions; 
    } 

    public List<String> getCheckedItems(){ 
     List<String> checkedItems = new ArrayList<String>(); 

     for(int i = 0; i < myChecked.size(); i++){ 
      if (myChecked.get(i)){ 
       (checkedItems).add(dayOfWeekList.get(i)); 
      } 
     } 

     return checkedItems; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 

     spinner = (Spinner) row.findViewById(R.id.planets_spinner); 
     spinner.setAdapter(spinnerAdapter); 

     if(row==null){ 
      LayoutInflater inflater=getLayoutInflater(); 
      row=inflater.inflate(R.layout.row, parent, false); 
     } 

     CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1); 
     checkedTextView.setText(dayOfWeekList.get(position)); 

     Boolean checked = myChecked.get(position); 
     if (checked != null) { 
      checkedTextView.setChecked(checked); 
     } 

     return row; 
    } 

} 

}

+0

你可以發佈從LogCat你的錯誤跟蹤? – fiddler

+0

@NamikazeMinato:Spinner是否位於「R.layout.row」佈局中的「R.id.planets_spinner」? – Ashwin

回答

1

您在getView有你的代碼亂序(..)

您試圖誇大你的觀點之前。如果convertView爲空(檢索您的微調它將通過列表中的第一次)你的微調不會被發現..你不能調用findViewById的東西,是空..你的操作順序更改爲以下

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 

     if(row==null){ 
      LayoutInflater inflater=getLayoutInflater(); 
      row=inflater.inflate(R.layout.row, parent, false); 
     } 

     spinner = (Spinner) row.findViewById(R.id.planets_spinner); 
     spinner.setAdapter(spinnerAdapter); 
     CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1); 
     checkedTextView.setText(dayOfWeekList.get(position)); 

     Boolean checked = myChecked.get(position); 
     if (checked != null) { 
      checkedTextView.setChecked(checked); 
     } 

     return row; 
    } 

請注意,我是如何移動檢索微調後,檢查視圖爲空和充氣,如果需要

+0

謝謝親愛的。還有一個問題。我看到Spinner,但是當我想要獲得所選Spinner項目的結果(ftom SpinnerAdapter)時,我無法做到這一點。我使用「int item = spinner.getSelectedItemPosition();」和「getResources()。getStringArray(R.array.planets_array)[item]」來獲取getView中選定的Spinner的值。但它不會爲Toast提供選定的字符串(只給出第一個索引的字符串)。 –