2012-07-26 103 views
1

我想让它使用户可以通过ListView检查需要的项目进行搜索。我已经创建了一个没有搜索实现的可检查的ListView。我曾尝试使用EditText和TextChangedListener添加搜索。这适用于正常的ListView,但由于此方法在用户输入时会创建新的适配器,因此检查的项目会丢失。Android搜索可检查列表查看

我的问题:

A.有什么方法通过一个ListView,而维修器材同一个适配器来搜索?

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

+0

您应该添加一些代码来展示如何构建可检查的ListView。 – Luksprog 2012-07-26 08:34:53

+0

我很确定它不需要做可检查的ListView的构建方式,我的问题是当前使用搜索的实现会在用户输入时创建一个新的适配器,从而会丢失检查哪些项目的数据。 – 2012-07-26 18:12:09

+1

什么是阻止你保存'ListView'中的选中项目的当前状态并重新应用它,你已经构建了新的适配器? – Luksprog 2012-07-26 18:20:24

回答

1

您可以构建自己的类,该列由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的方法。

+0

伟大的建议!我重写了代码来适应这一点,但我遇到了一个问题,似乎在可检查的列表视图中,您不能从getView()方法设置检查值。我会进一步研究这一点。 – 2012-07-26 21:04:14

+0

您可以扩展checkedTextView并将您的新视图构建到listView(查看我的版本) – dilix 2012-07-27 05:59:55