2013-12-09 1 views
0

확장형 목록보기에서 검색 필터를 구현하려고합니다.확장형 목록보기에서 검색을 구현할 수 없습니다.

그러나 editText에 키워드를 입력하려고 할 때 응용 프로그램이 손상됩니다.

다음은 두 개의 자바 클래스 인 MainActivityExpandableListAdapter입니다.

MainActivity 클래스

package com.ahmedabadjobs.dashboard; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnGroupClickListener; 
import androidhive.dashboard.R; 

import com.ahmedabadjobs.expandablelistview.ExpandableListAdapter; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 

    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<String>> listDataChild; 
    EditText inputSearch; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // get the listview 
     expListView = (ExpandableListView) findViewById(R.id.lvExp); 

     // preparing list data 
     prepareListData(); 

     listAdapter = new ExpandableListAdapter(this, listDataHeader, 
       listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 

     // Listview Group click listener 
     expListView.setOnGroupClickListener(new OnGroupClickListener() { 

      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
        int groupPosition, long id) { 
       // Toast.makeText(getApplicationContext(), 
       // "Group Clicked " + listDataHeader.get(groupPosition), 
       // Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     }); 

     inputSearch = (EditText) findViewById(R.id.inputSearch); 

     inputSearch.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence cs, int arg1, int arg2, 
        int arg3) { 
       // ((Filter) listAdapter.getFilter()).filter(cs); 
       NewsFeedActivity.this.listAdapter.getFilter().filter(cs); 
      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, 
        int arg2, int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable arg0) { 
       // When user changed the Text 

      } 
     }); 

    } 

    /* 
    * Preparing the list data 
    */ 
    private void prepareListData() { 
     listDataHeader = new ArrayList<String>(); 
     listDataChild = new HashMap<String, List<String>>(); 

     // Adding child data 
     listDataHeader.add("Company 0"); 
     listDataHeader.add("Company 1"); 
     listDataHeader.add("Company 2"); 

     // Adding child data 
     List<String> cignex = new ArrayList<String>(); 
     company0.add("Address Line 1"); 
     company0.add("Address Line 2"); 
     company0.add("Address Line 3"); 
     company0.add("Phone number"); 
     company0.add("Email Address"); 

     List<String> company1 = new ArrayList<String>(); 
     company1.add("Address Line 1"); 
     company1.add("Address Line 2"); 
     company1.add("Address Line 3"); 
     company1.add("Phone number"); 
     company1.add("Email Address"); 

     List<String> company2 = new ArrayList<String>(); 
     company2.add("Address Line 1"); 
     company2.add("Address Line 2"); 
     company2.add("Address Line 3"); 
     company2.add("Phone number"); 
     company2.add("Email Address"); 

     listDataChild.put(listDataHeader.get(0), company0); // Header, Child 
                  // data 
     listDataChild.put(listDataHeader.get(1), company1); 
     listDataChild.put(listDataHeader.get(2), company2); 
    } 

} 

ExpandableListAdapter 클래스

package com.ahmedabadjobs.expandablelistview; 

import java.util.HashMap; 
import java.util.List; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.Filter; 
import android.widget.Filterable; 
import android.widget.TextView; 
import androidhive.dashboard.R; 

public class ExpandableListAdapter extends BaseExpandableListAdapter implements 
     Filterable { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
      HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item, null); 
     } 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItem); 

     txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    @Override 
    public Filter getFilter() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

EDITTEXT에 키워드를 제공 할 때 내가 울부 짖는 오류를 얻고있다.

12-09 03:37:48.080: E/InputEventSender(1139): Exception dispatching finished signal. 
12-09 03:37:48.080: E/MessageQueue-JNI(1139): Exception in MessageQueue callback: handleReceiveCallback 
12-09 03:37:48.161: E/MessageQueue-JNI(1139): java.lang.NullPointerException 

기능을 추가하기 위해 필요한 변경 사항을 알려주십시오.

답변

0

사용 editext

edit=(EditText)findViewById(R.id.editText1); 
       edit.addTextChangedListener(filterTextWatcher); 

private class MangaNameFilter extends Filter 
      { 

       @Override 
       protected FilterResults performFiltering(CharSequence constraint) { 
        // NOTE: this function is *always* called from a background thread, and 
        // not the UI thread. 
        constraint = edit.getText().toString().toLowerCase(); 
        FilterResults result = new FilterResults(); 
        if(constraint != null && constraint.toString().length() > 0) 
        { 
         detailsList=detailsSer.GetAlldetails(); 
         dupCatList=detailsList; 

         ArrayList<detailsEntity> filt = new ArrayList<detailsEntity>(); 
         ArrayList<detailsEntity> lItems = new ArrayList<detailsEntity>(); 
         synchronized(this) 
         { 
          lItems.addAll(dupCatList); 
         } 
         for(int i = 0, l = lItems.size(); i < l; i++) 
         { 
          detailsEntity m = lItems.get(i); 
          if(m.description.toLowerCase().contains(constraint)) 
          filt.add(m); 
         } 
         result.count = filt.size(); 
         result.values = filt; 
        } 
        else 
        { 
         detailsList=detailsSer.GetAlldetails(); 
         dupCatList=detailsList; 
         synchronized(this) 
         { 
          result.count = dupCatList.size(); 
          result.values = dupCatList; 
         } 
        } 
        return result; 
       } 
       @SuppressWarnings("unchecked") 
       @Override 
       protected void publishResults(CharSequence constraint, FilterResults result) { 
        // NOTE: this function is *always* called from the UI thread. 

        filtered = (ArrayList<detailsEntity>)result.values; 

        ArrayList<Integer> IdList = new ArrayList<Integer>(); 
       IdList.clear(); 
       for(int i=0;i<filtered.size();i++) 
       { 
        IdList.add(filtered.get(i).catID); 
       } 

       HashSet<Integer> hashSet = new HashSet<Integer>(IdList); 
       midList = new ArrayList<Integer>(hashSet) ; 
       Collections.sort(midList); 
       Adapter = new CategoryListAdapter(context, R.layout.list1, R.layout.list2, filtered, midList); 
         List.setAdapter(Adapter); 

} 
를 다음과 같이

public class ListAdapter extends BaseExpandableListAdapter implements Filterable { 


     public void notifyDataSetInvalidated() 
     { 
      super.notifyDataSetInvalidated(); 
     } 
      public Filter getFilter() 
      { 
      if(filter == null) 
        filter = new MangaNameFilter(); 
       return filter; 
      } 

필터 클래스를 다음과 같이

private TextWatcher filterTextWatcher =new TextWatcher() 
    { 

    public void beforeTextChanged(CharSequence s, int start, int count,int after) 
     { 

     } 
    public void onTextChanged(CharSequence s, int start, int before,int count) 
     { 

     } 

    public void afterTextChanged(Editable s) 
     { 
     // TODO Auto-generated method stub 
     ((Filterable) ((ListAdapter) Adapter)).getFilter().filter(edit.getText().toString()); 
     } 
    }; 

가 listadapter을 textwatcher 쓰기 다음과 같이

+0

ExpandableListAdapter.java에서 변경해야합니까? –

0

다음은 도움이 될만한 또 다른 게시물입니다. ExpandableListView.OnChildClickListener

참고 : 여기에는 editText가 사용되지 않습니다.

나는 actionBar에서 검색을 사용했습니다.

희망이 도움이됩니다.