1

我想这样做,以便用户可以搜索 ListView 检查所需的项目。我已经创建了一个没有搜索实现的可检查 ListView。我尝试使用 EditText 和 TextChangedListener 添加搜索。这适用于普通的 ListView,但由于此方法在每次用户键入时都会创建一个新适配器,因此检查的项目会丢失。

我的问题:

A. 有没有办法在保持同一个适配器的同时通过 ListView 进行搜索?

B. 有没有办法使用我当前使用 TextChangedListener 的方法但保持检查的项目?

4

1 回答 1

1

您可以构建自己的包含列表视图元素字段的类并添加一个字段(isChecked)。重新创建适配器时,您使用已创建的对象并保存选中状态,并且在每一行的 getview 中,您可以通过此属性选中\取消选中此项

这是我的自定义复选框,您可以使用它并根据需要进行修改,我认为它可以满足您的目的(我从我的应用程序中获取此代码,您可以检查它在此应用程序中的工作方式):

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 的方法。

于 2012-07-26T18:20:36.617 回答