2012-07-26 98 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