3

由於PreferenceFragment不支持庫可我創建了一個ListFragment顯示的設置列表用戶,因爲我沒有很多的設置來顯示。我還創建了一個自定義ArrayAdapter來自定義列表項。我需要處理,當用戶檢查CheckBox s之一,所以我可以保存天氣或不檢查它。所以如果它被選中,那麼它將保持檢查直到用戶取消選中它。如果列表中只有一個設置,但現在有2個,我可能需要添加更多。所以我需要能夠確定哪一個被檢查。我可以處理檢查並取消選中罰款我只是無法找到一種方式來確定哪一個被檢查。獲取項目的位置從自定義適配器

守則

這裏是我的列表項:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" 
         android:orientation="horizontal"> 

<TextView android:id="@+id/pref_edit_text" android:layout_width="0dp" android:layout_height="30dp" 
       android:layout_weight="5"/> 
    <CheckBox android:id="@+id/pref_check_box" android:layout_width="0dp" android:layout_height="wrap_content" 
       android:layout_weight="1" android:onClick="onCheckBoxClick"/> 
    </LinearLayout> 

<TextView android:id="@+id/pref_edit_text2" android:layout_width="match_parent" 
       android:layout_height="50dp"/> 

這裏是我的適配器getView()

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //mIntCheckBoxPosition = position; 
     Typeface tf = Typeface.createFromAsset(mMainActivity.getAssets(), "fonts/ArchitectsDaughter.ttf"); 
     LayoutInflater inflater = mMainActivity.getLayoutInflater(); 
     View view = inflater.inflate(mIntLayoutId, parent, false); 

     TextView text = (TextView) view.findViewById(R.id.pref_edit_text); 
     text.setText(mStringArrayTitle[position]); 
     text.setTypeface(tf, Typeface.BOLD); 

     text = (TextView) view.findViewById(R.id.pref_edit_text2); 
     text.setText(mStringArraySubTitle[position]); 
     text.setTypeface(tf); 

     mMainActivity.setTitle("Settings"); 

     return view; 
    } 

而且這裏是我處理當一個CheckBox點擊這是在ListFragment

public void onCheckBoxClick(View view) { 
     boolean isChecked = ((CheckBox) view).isChecked(); 
     Editor editor = mMainActivity.getSharedPreferences(PREF_KEY_CHECK_BOX, Activity.MODE_PRIVATE).edit(); 

     switch (view.getId()) { 
      case R.id.check_box : 
       if (isChecked) { 
        editor.putBoolean(PREF_KEY_ROUNDING, true).commit(); 
       } 
       else { 
        editor.putBoolean(PREF_KEY_ROUNDING, false).commit(); 
       } 
       break; 
      } 
     } 
    } 

這裏是我的設置是這樣的:
enter image description here

我曾嘗試
1.我曾嘗試在適配器設置變量到物品位置並使用吸氣劑來獲取位置,但是隻返回顯示的最後一個物品的位置。
2.我一直在使用一些在ListFragment的方法來獲取CheckBox的位置嘗試,但他們總是返回-1。
3.我已經做了很多谷歌搜索和SO搜索上,但我一直沒能找到一個解決方案,得到這個工作。

所以,如果有人知道一種方式,我可以得到CheckBox的位置或任何其他方式,我可以告訴哪一個被點擊,我將永遠感激。

+0

你不能使用行動SherlockBar任何理由嗎?它會爲您節省很多頭痛,因爲它幾乎包含了設置片段的模板。加上本機4.0的外觀。 – 2013-04-03 20:31:28

回答

10

您可以使用setTagint添加到指示其位置的視圖中,然後再使用getTag進行檢索。例如:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ... // your other code here 

    CheckBox checkbox = (CheckBox) view.findViewById(R.id.pref_check_box); 
    checkbox.setTag(new Integer(position)); 

} 

然後,在你onCheckBoxClick

public void onCheckBoxClick(View view) { 
    Integer pos; 
    pos = (Integer) view.getTag(); 

    ... // do what you want with `pos` here 

} 
相關問題