2015-11-11 180 views
0

我正在實現自定義列表視圖與自定義列表項目,其中有微調框和文本框。我想在列表視圖的列表項中使用onItemSelected()保存選定的微調值,但我沒有任何想法找到包含用戶正在處理的微調控件的列表項。我怎麼能得到這個,任何人都可以幫助這個?自定義列表視圖與自定義列表項目有微調

以下是我在列表視圖適配器中的代碼。

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

    if (listItemView == null) { 
     listItemView = inflater.inflate(R.layout.listitem, null); 
     ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.view1 = (Spinner) listItemView.findViewById(R.id.view1); 
     viewHolder.view2 = (TextView) listItemView.findViewById(R.id.view2); 

     viewHolder.view1.setFocusable(false); 
     viewHolder.view2.setFocusable(false); 

     viewHolder.view1.setAdapter(view1Adapter); 
     viewHolder.view1.setPrompt("Select"); 
     viewHolder.view1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       String selectedId = spinnerItemList.get(position).id; 
       // todo : need to find a way to get list item of list view, which has this spinner, like the position of getView(). 
      } 

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

      } 
     }); 

     listItemView.setTag(viewHolder); 
    } 

    CustomListItem item = listViewItemList.get(position); 
    ViewHolder viewHolder = listItemView.getTag(); 
    viewHolder.view1.setSelection(item.view1Value); 
    viewHolder.view2.setText(String.valueOf(item.view2Value)); 

    return listItemView; 
} 
+0

@Loc是的,它不需要是實例,CustomListItem的索引會很好。 – user3110409

回答

0

我張貼我發現作爲一個對於那些和我有同樣問題的人來說。其實我從Loc的回答中看到了這個想法,但他以某種方式刪除了他的答案。也許這個解決方案可能存在一個嚴重的問題。無論如何,這現在對我有用。

我用setTag()和getTag()來獲得CustomListItem。請參閱下面的代碼。

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

    if (listItemView == null) { 
    listItemView = inflater.inflate(R.layout.listitem, null); 
    ViewHolder viewHolder = new ViewHolder(); 
    viewHolder.view1 = (Spinner) listItemView.findViewById(R.id.view1); 
    viewHolder.view2 = (TextView) listItemView.findViewById(R.id.view2); 

    viewHolder.view1.setFocusable(false); 
    viewHolder.view2.setFocusable(false); 

    viewHolder.view1.setAdapter(view1Adapter); 
    viewHolder.view1.setPrompt("Select"); 
    viewHolder.view1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      String selectedId = spinnerItemList.get(position).id; 
      // todo : need to find a way to get list item of list view, which has this spinner, like the position of getView(). 
      CustomListItem item = (CustomListItem)parent.getTag(); 
      item.view1Value = selectedId; 
     } 

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

     } 
    }); 

    listItemView.setTag(viewHolder); 
    } 

    CustomListItem item = listViewItemList.get(position); 
    ViewHolder viewHolder = listItemView.getTag(); 
    viewHolder.view1.setSelection(item.view1Value); 
    viewHolder.view.setTag(item); 
    viewHolder.view2.setText(String.valueOf(item.view2Value)); 

    return listItemView; 
} 
0

如果你要處理適配器內部微調的onItemSelected試試這個:

//傳遞活動/片段的背景下,適配器

context.saveSelectedValue(selectedId); 

//保存選中它的活動/片段

public void saveSelectedValue(String id){ 
    //code to save 
} 
+0

謝謝,但我想我也需要知道CustomListItem的索引。而實現它的方式是我真正想知道的。 – user3110409