2014-07-01 84 views
0

我有一個listView與n行數每行包含textview1(對於文本),textview2(對於id)和微調。我想在多行中選擇微調器。這裏的問題是一旦我滾動listView選擇不見了(回收listView)。微調選擇不工作Android

我已經存儲在一個ArrayList(選擇哪些行旋轉器)和旋轉器的選定位置的ID。在listView的onScroll方法中,我嘗試使用spinner.setSelection(position);,但它不起作用。

我的樣本圖像enter image description here

而且我的源代碼:

level2.setOnScrollListener(new OnScrollListener() { 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // TODO Auto-generated method stub 
    } 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
     for (int i = 0; i < level2.getChildCount(); i++) { 
      RelativeLayout relative_layout = (RelativeLayout)level2.getChildAt(i); 
      TextView textview_1 = (TextView)relative_layout.getChildAt(0); 
      TextView textview_2 = (TextView)relative_layout.getChildAt(1); 
      Spinner spinner = (Spinner)relative_layout.getChildAt(2); 
      spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
       public void onItemSelected(AdapterView <?> arg0, 
        View arg1, int arg2, long arg3) { 
        //save my id and spinner selection here in a ArrayList 
        //Here the position is get value from arraylist 
        spinner.setSelection(position); 
       } 
       public void onNothingSelected(AdapterView <?> arg0) { 
        // TODO Auto-generated method stub 
       } 
      }); 
     } 
    } 
    }); 

它是不完整的代碼,它只是一塊。請幫幫我。

+0

顯示您的getView方法 –

+0

我沒有使用getView方法()。你想看看我如何加載listView? – Amsheer

+0

是的。你有沒有使用listActivity? –

回答

0

我會像這樣解決。試試這個。這只是示例活動。

public class MainActivity extends Activity { 
    SpinnerAdapter ratingAdapter; 
    ListView listView; 
    List<String> listString; 
    ArrayAdapter<String> dataAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listString = new ArrayList<String>(); 
     for(int listCount = 0; listCount < 20 ; listCount++){ 
      listString.add("ListCount"+listCount); 
     } 

     List<String> spinnerData = new ArrayList<String>(); 
     for (int i = 0; i < listString.size(); i++) { 
      spinnerData.add("MyTest="+i); 
     } 

     dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData); 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     fillAdapter(); 
    } 

    private void fillAdapter() { 
     ratingAdapter = new SpinnerAdapter(getApplicationContext()); 
     listView = (ListView) findViewById(R.id.listView1); 
     listView.setAdapter(ratingAdapter); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    class ViewHolder { 
     Spinner spinner; 
    } 

    class SpinnerAdapter extends ArrayAdapter { 
     Context context; 
     HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>(); 

     public SpinnerAdapter(Context context) { 
      super(context, R.layout.item_list_spinner_adaoter, listString); 
      this.context = context; 
     } 

      public int getCount() { 
       return listString.size(); 
      } 
      public Object getItem(int position) { 
       return listString.get(position); 
      } 
      public long getItemId(int position) { 
       return position; 
      } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      final ViewHolder holder; 
      if (convertView == null) { 
       holder = new ViewHolder(); 
       convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null); 
       holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1); 

       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 


      holder.spinner.setAdapter(dataAdapter); 
      if (selectedItems.get(position) != null) { 
       holder.spinner.setSelection(selectedItems.get(position)); 
      } 
      holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

       @Override 
       public void onItemSelected(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        selectedItems.put(position, arg2); 
       } 

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

       } 
      }); 

      return convertView; 
     } 
    } 
} 

activity_main.xml中:

<ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:stackFromBottom="false" 
     android:transcriptMode="disabled" > 
    </ListView> 

</LinearLayout> 

在item_list_spinner_adaoter.xml:

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

0

這裏如果你想設置選擇的項目,那麼你必須使用代碼spinner.setSelection(arg2);因爲你還沒有初始化你的位置變量,它的默認值將是0,所以它總是會顯示1st選擇項目。

+0

我不認爲這是問題。因爲只有滾動的時間我有問題。一旦我設置了spinner.setSelection(arg2);滾動時會發生什麼。 – Amsheer