2012-02-23 58 views
0

我有在每个视图中checkedtextview的列表视图,我的所有项目都通过数据库操作,一切正常,但我不能保存列表项的选中状态,我有一个可绘制的设置为每个点击,但我不知道如何保存它,如果我开始另一项活动,与代码的答案将不胜感激,谢谢 请帮助!android保存checkedtextview检查

listview.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View v, int position, 
       long id) { 
      // TODO Auto-generated method stub 
      CheckedTextView tt = (CheckedTextView) v 
        .findViewById(R.id.checkedview1); 
        if (!tt.isChecked()) { 
        tt.setChecked(true); 

        tt.setCheckMarkDrawable(R.drawable.checkedbox); 
        } else { 
        tt.setChecked(false); 
        tt.setCheckMarkDrawable(R.drawable.nullbox); 
        } 
     } 

    }); 

这里是我的列表视图的XML和我checkedtextview

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/checkedview1" 
    android:paddingLeft="20dip" 
    android:paddingRight="20dip" 
    android:paddingTop="10dip" 
    android:paddingBottom="10dip" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:checkMark="@drawable/nullbox" 
    android:textSize = "18sp" 
    android:textColor="#000000" 
    android:listSelector="#ef0000" 

>

而我的列表视图

<ListView 
    android:id="@+id/list1" 
    android:layout_width="match_parent" 
    android:layout_height="404dp" 
    android:cacheColorHint="#ffffff" /> 

这是我使用的适配器负载我的清单 checklistDB.open(); c = checklistDB.getAllTask​​s();

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     String task = c.getString(1); 
     adapter.add(task); 
    } 
    checklistDB.close(); 

回答

0
class CheckAdapter extends BaseAdapter { 
    class NormalListHolder { 
     ImageView check; 
    } 

    LayoutInflater layoutInflater; 
    NormalListHolder normalListHolder; 

    public CheckAdapter(Context context) { 
     layoutInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = layoutInflater.inflate(R.layout.list_item, 
        null); 
      normalListHolder = new NormalListHolder(); 
      convertView.setTag(normalListHolder); 
     } else { 
      normalListHolder = (NormalListHolder) convertView.getTag(); 
     } 

     normalListHolder.favoriteTag.setId(position); 
     normalListHolder.favoriteTag 
       .setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (favoriteStatus.get(v.getId())) { // favorite 
          favoriteStatus.set(v.getId(), false); 
          normalListHolder.favoriteTag.setImageSrc(); 
         } else { 
          favoriteStatus.set(v.getId(), true); 
          normalListHolder.favoriteTag.setImageSrc(); 
         } 
         notifyDataSetChanged(); 
        } 
       }); 
     return convertView; 
    } 

} 

我从我的代码编辑,但我不知道它是否工作(我编辑不小心),我将解释我做什么如下:

1创建的ArrayList其中保存的物品状态(或当您滚动列表视图的复选框会混淆)

ArrayList<Boolean> favoriteStatus = new ArrayList<Boolean>(); 

2创建一个新的适配器可并作出在它的持有人绑定在LISTI意见一起工作。

3 setTag到列表项的持有人

4时,点击它,寻找在statusArray状态,并把正确的状态视图,然后notifyDatasetChange(如果它是一个复选框,也许并不需要,因为我在这里使用一个ImageView的),你也可以做更新数据库或其它在这里的行动

对不起我的英语池,希望它可以帮助你