您可以構建自己的類,該列由listview elemnt的字段組成,並添加一個字段(isChecked)。當你重新創建適配器,你使用已經創建的對象和檢查狀態被保存,並在每行的getview你可以檢查\取消選中此項目的屬性
這是我的自定義複選框,你可以使用它並修改,如果你想,我認爲它可以適合你的目的(我想借此代碼是從我的應用程序,你可以檢查它是如何工作的this app):
package ru.human.notification;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckedTextView;
public class CustomCheckBox extends CheckedTextView
{
private int CHECKED_IMAGE;
private int UNCHECKED_IMAGE;
private main_full parent;
private ArrayList<View> viewsToCheck;
private int msgType;
public void setParent(main_full parent, int msgType)
{
this.parent = parent;
this.msgType = msgType;
setOnLongClickListener(longClickListner);
}
public void addViewToCheck(View view)
{
this.viewsToCheck.add(view);
}
OnLongClickListener longClickListner = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (parent != null && isChecked())
parent.showTimeDialog(msgType);
return true;
}
};
OnClickListener listener = new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
if (!isChecked())
{
setTextColor(getResources().getColor(R.color.green));
setPaintFlags(0);
setChecked(true);
setCheckMarkDrawable(CHECKED_IMAGE);
if (parent != null)
parent.showTimeDialog(msgType);
}
else
{
setTextColor(getResources().getColor(R.color.lightgrey));
setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
setChecked(false);
setCheckMarkDrawable(UNCHECKED_IMAGE);
}
for (View view: viewsToCheck)
view.setVisibility(isChecked()?View.VISIBLE:View.GONE);
}
};
public CustomCheckBox(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setOnClickListener(listener);
System.out.println("1");
// TODO Auto-generated constructor stub
// TODO mmmm
}
public CustomCheckBox(Context context, AttributeSet attrs)
{
super(context, attrs);
viewsToCheck = new ArrayList<View>();
String xmlns="http://schemas.android.com/apk/res/ru.human.notification";
setOnClickListener(listener);
System.out.println("2");
int k = R.drawable.alarmgrey;
CHECKED_IMAGE = attrs.getAttributeResourceValue(xmlns, "checkedImage", 404);
UNCHECKED_IMAGE = attrs.getAttributeResourceValue(xmlns, "uncheckedImage", 404);
setCheckMarkDrawable(UNCHECKED_IMAGE);
setTextColor(getResources().getColor(R.color.lightgrey));
setTextSize(18f);
setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
// setChecked(false);
System.out.println(CHECKED_IMAGE +" " + UNCHECKED_IMAGE + " " +k);
// TODO Auto-generated constructor stub
}
public CustomCheckBox(Context context)
{
super(context);
final Context parent = context;
System.out.println("3");
// TODO Auto-generated constructor stub
setOnClickListener(listener);
}
}
attrs.xml:
</declare-styleable>
<declare-styleable name="CustomCheckBox">
<attr name="uncheckedImage" format="integer"/>
<attr name="checkedImage" format="integer"/>
</declare-styleable>
</resources>
用法:
<ru.human.notification.CustomCheckBox
android:id="@+id/delayed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/settingsbutton"
android:checked="false"
customcheckbox:checkedImage="@drawable/clockcolor"
android:text="@string/delay"
customcheckbox:uncheckedImage="@drawable/clockgrey" />
將此視圖添加到您的listview並創建setChecked方法(代碼與onClick方法中的代碼相同)。不要關注setParent和addViewToCheck方法 - 它們是在我的應用程序中控制UI的方法。
您應該添加一些代碼來展示如何構建可檢查的ListView。 – Luksprog 2012-07-26 08:34:53
我很確定它不需要做可檢查的ListView的構建方式,我的問題是當前使用搜索的實現會在用戶輸入時創建一個新的適配器,從而會丟失檢查哪些項目的數據。 – 2012-07-26 18:12:09
什麼是阻止你保存'ListView'中的選中項目的當前狀態並重新應用它,你已經構建了新的適配器? – Luksprog 2012-07-26 18:20:24