2014-12-02 60 views
0

我實現了一個ArrayAdapterListView獲取Spinner的一個ListView的位置,並得到其選擇的價值

public class CalcAMRListAdapter extends ArrayAdapter<AMRStateListEntry> implements OnItemSelectedListener { 

    public CalcAMRListAdapter(Context context, List<AMRStateListEntry> entries) { 
     super(context, R.layout.activity_calc_amr_fragment_row, entries); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     AMRStateListEntry entry = this.getItem(position); 
     ViewHolder viewHolder = null; 
     LayoutInflater inflater = null; 

     if(convertView == null) { 
      viewHolder = new ViewHolder(); 
      inflater = LayoutInflater.from(this.getContext()); 
      convertView = inflater.inflate(R.layout.activity_calc_amr_fragment_row, parent, false); 
      viewHolder.spinnerActivity = (Spinner) convertView.findViewById(R.id.calc_amr_row_spinner_activity); 
      viewHolder.spinnerHours = (Spinner) convertView.findViewById(R.id.calc_amr_row_spinner_hours); 
      viewHolder.textViewPal = (TextView) convertView.findViewById(R.id.calc_amr_row_textview_pal); 
      viewHolder.imageButtonDelete = (ImageButton) convertView.findViewById(R.id.calc_amr_row_button_delete); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     String pal = null; 

     switch (entry.getActivity()) { 
     case 1: pal = "" + 0.95; 
      break; 
     case 2: pal = "" + 1.2; 
      break; 
     case 3: pal = "" + 1.5; 
      break; 
     case 4: pal = "" + 1.7; 
      break; 
     case 5: pal = "" + 1.9; 
      break; 
     case 6: pal = "" + 2.4; 
      break; 
     default: pal = ""; 
      break; 
     } 

     viewHolder.spinnerActivity.setSelection(entry.getActivity()); 
     viewHolder.spinnerActivity.setOnItemSelectedListener(this); 
     viewHolder.spinnerHours.setSelection(entry.getHour()); 
     viewHolder.spinnerHours.setOnItemSelectedListener(this); 
     viewHolder.textViewPal.setText(pal); 
     viewHolder.imageButtonDelete.setImageResource(R.drawable.ic_remove_row); 

     return convertView; 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) {} 

    private static class ViewHolder { 
     Spinner spinnerActivity = null; 
     Spinner spinnerHours = null; 
     TextView textViewPal = null; 
     ImageButton imageButtonDelete = null; 
    } 
} 

正如你所看到的我ListView每個項目包含兩個Spinner。在onItemSelected方法中,我現在需要獲取ListView中行的位置,其中Spinner用於選擇一個值,我還需要在一行中的兩個spinners之間有所不同。我的數據模型是AMRStateListEntry。我需要在我的AMRStateListEntry中設置旋鈕的選定值。

我該怎麼做?

+0

可能重複做同樣的[你怎麼Spinner的選擇的價值?(http://stackoverflow.com/questions/2652414/how-do-you-得到選定的價值的微調) – Sufian 2014-12-02 11:53:14

+0

我不能看到我可以從您發佈的鏈接在lisview中的行的位置。我知道我是如何得到一個微調器的價值,但首先我需要從列表視圖中正確的行得到正確的微調。 – Mulgard 2014-12-02 12:02:08

回答

1
viewHolder.spinnerHours.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 
       int positionInSpinner, long id) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
      // TODO Auto-generated method stub 

     } 
    }); 

改爲像這樣創建它。然後你設置public View getView(final int position, View convertView, ViewGroup parent)的位置爲final。現在您可以在onItemSelected內使用final int position

併爲您的其他Spinner

相關問題