2012-02-21 67 views
1

我已經使用ListView與自定義適配器,所以我的ListView包括複選框,但問題是,當我向下滾動,選中CheckBoxes取消選中,我該如何使用此代碼?如何在ListView中滾動時保存CheckBox的狀態?

UPDATE

我的自定義適配器:

public class AppsArrayAdapter extends ArrayAdapter<String> { 
    private final Context context; 

    private List<Integer> checkedItems; 

    private final String DefaultApps = "def_apps"; 

    public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX 
     super(context, R.layout.apps, values); 
     this.context = context; 
     this.checkedItems = checkedItems;//XXX 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE); 
     String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, ""); 
     String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, ""); 


     LayoutInflater inflater = (LayoutInflater)context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.apps, parent, false); 
     TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps); 
     CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps); 
     text_apps.setText(prefNameDefaultApps.split("\n")[position]); 

     check.setChecked(checkedItems.contains((Integer) position)); 
     return rowView; 
    } 
} 

我的活動:

public class AppsActivity extends ListActivity { 
    private final String DefaultApps = "def_apps"; 

    private List<Integer> checkedItems = new ArrayList<Integer>(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"), checkedItems); 
     setListAdapter(adapter); 
} 
} 
@Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     if(checkedItems.contains((Integer) position)) checkedItems.remove((Integer) position); 
     else checkedItems.add((Integer) position); 
    } 
+0

看看我的答案http://stackoverflow.com/questions/9309250/checkbox-gets-unchecked-on-scroll-in-a-custom-listview/14724554#14724554 – 2013-02-06 08:37:40

回答

0
// initialise the list 
private List<Integer> selection = new ArrayList<Integer>(); 

for (String str: prefNameDefaultApps.split("\n")) { 
     selection.add(0); 
} 


// setting the list 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
     if(CheckBox is checked) {//(check if the checkbox is checked) 
       selection.set(position, 1); 
     } 
     else{ 
       selection.set(position, 0); 
     } 
} 

// show in the listView 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

     .... 
     .... 
     check.setChecked(selection.get(position) == 0 ? false : true); 

} 

這應該讓你去:)

+0

仍然沒有工作。 (String str:prefNameDefaultApps.split(「\ n」)){0} {0} {0} {0}我嘗試將它放到公共AppsArrayAdapter(Context context,String []值),然後公共View getView(int position,View convertView,ViewGroup parent),但仍然不起作用。 – Adam 2012-02-21 19:22:51

+0

將代碼「prefNameDefaultApps」初始化。 – 1HaKr 2012-02-22 19:56:53

0

我不能完全肯定是否有Android的標準解決方案,但這應該做的伎倆:

public class AppsArrayAdapter extends ArrayAdapter<String> { 
private final Context context; 
private List<Integer> checkedItems;//Here we save the position of the selected items. 

private final String DefaultApps = "def_apps"; 
//This will be invoked when one of the CheckBoxes gets clicked. It will update the checkedItems list. 
private View.OnClickListener onClickListener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Integer position = (Integer) v.getTag(); 
     if(checkedItems.contains(position)) checkedItems.remove(position); 
     else checkedItems.add(position); 
    } 
}; 

public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX 
    super(context, R.layout.apps, values); 
    this.context = context; 
    this.checkedItems = checkedItems;//XXX 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE); 
    String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, ""); 

    //You shouldn't inflate the View everytime getView() is called, 
    /*LayoutInflater inflater = (LayoutInflater)context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View rowView = inflater.inflate(R.layout.apps, parent, false);*/ 
    //rather let the superclass handle it as it's more efficient. 
    View rowView = super.getView(position, convertView, parent); 

    TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps); 
    CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps); 
    text_apps.setText(prefNameDefaultApps.split("\n")[position]); 

    check.setChecked(checkedItems.contains((Integer) position));//XXX 
    check.setTag((Integer) position);//XXX 
    check.setOnClickListener(onClickListener);//XXX 
    return rowView; 
} 

}

在你ListActivity,添加一個字段private List<Integer> checkedItems = new ArrayList<Integer>;。你必須在Adapter構造函數中傳遞這個List!
如果要檢查你的ListView被選中的第n項是否只是看n是否保存在checkedItems,即

if(checkedItems.contains(requestedItemPosition)) { 
    //Item was selected. 
} else { 
    //Item was not selected. 
} 

我真的建議你不要自己膨脹的意見。

+0

告訴我這是否需要進一步解釋。 – m1ntf4n 2012-02-21 19:15:22

+0

你能給我一些例子或詳細的解釋,應該看看我的活動。 MyActivity擴展了ListActivity。我試圖管理它,但我得到了一個力量關閉。 – Adam 2012-02-21 19:30:58

+0

我更新了我的帖子。 – m1ntf4n 2012-02-21 20:02:59

0

試試這個:(您的自定義適配器)

public class AppsArrayAdapter extends ArrayAdapter<String> { 
private final Context context; 

private List<Integer> checkedItems; 

private final String DefaultApps = "def_apps"; 
LayoutInflater inflater; 

public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX 
    super(context, R.layout.apps, values); 
    this.context = context; 
    this.checkedItems = checkedItems;//XXX 
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE); 
    String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, ""); 
    String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, ""); 


View rowView = convertView; 
ViewHolder viewH = new ViewHolder(); 

if (rowView == null){ 
     rowView = inflater.inflate(R.layout.apps, parent, false); 
     viewH.text_apps = (TextView)rowView.findViewById(R.id.text_apps); 
     viewH.check = (CheckBox)rowView.findViewById(R.id.check_apps); 
    rowView.setTag(viewH); 
}else{ 
    viewH = (ViewHolder) rowView.getTag(); 
} 

    viewH.text_apps.setText(prefNameDefaultApps.split("\n")[position]); 
    viewH.check.setChecked(checkedItems.contains((Integer) position)); 

    return rowView; 
} 

static class ViewHolder { 
    public TextView text_apps; 
    public CheckBox check; 

} 

}

希望這有助於:)

0

你應該使用 MyAdapter newad =(MyAdapter)adapter.getAdapter(); newad.notifyDataSetChanged(); 這個代碼onitem點擊方法 和MyAdapter是類適配器,使新對象像這樣的 和適配器類,你應該寫一行

LayoutInflater中; in =(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 在適配器構造簡單

相關問題