2013-08-02 53 views
3

我想在我的BaseAdapter中實現搜索功能。我製作了代碼,但沒有正確過濾。我已經找過一些例子,但他們都沒有幫助我解決它。Listview Baseadapter搜索功能

這裏是我的MyAdapter:

public class ListViewAdapter extends BaseAdapter implements Filterable { 

// Declare Variables 
Context context; 
LayoutInflater inflater; 

List<HashMap<String, String>> data2; 
List<HashMap<String, String>> filteredData; 

ImageLoader imageLoader; 

String TAG = "Søge Resultater: "; 


public ListViewAdapter(Context context, 
     ArrayList<HashMap<String, String>> arraylist) { 
    this.context = context; 
    data2 = arraylist; 
    imageLoader = new ImageLoader(context); 

    //To start, set both data sources to the incoming data 
    filteredData = arraylist; 
} 

public int getCount() 
{ 
    return filteredData.size(); 
} 

    public Object getItem(int position) 
{ 
    return filteredData.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    // Declare Variables 
    TextView fraction_name; 
    TextView fraction_typetext; 
    TextView fraction_type; 
    ImageView fraction_pictogram; 

    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View itemView = inflater.inflate(R.layout.list_catabc_item, parent, false); 
    // Get the position from the results 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

    resultp = filteredData.get(position); 

    // Locate the TextViews in listview_item.xml 
    fraction_name = (TextView) itemView.findViewById(R.id.affaldabc_fraction_name); 
    fraction_typetext = (TextView) itemView.findViewById(R.id.affaldabc_fraction_typetext); 
    fraction_type = (TextView) itemView.findViewById(R.id.affaldabc_fraction_type); 
    // Locate the ImageView in listview_item.xml 
    fraction_pictogram = (ImageView) itemView.findViewById(R.id.pictogram); 

    // Capture position and set results to the TextViews 
    fraction_name.setText(resultp.get(CatAbc.TAG_fraction_name)); 
    fraction_typetext.setText(resultp.get(CatAbc.TAG_fraction_typetext)); 
    fraction_type.setText(" "+resultp.get(CatAbc.TAG_fraction_type)); 
    // Capture position and set results to the ImageView 
    // Passes flag images URL into ImageLoader.class to download and cache 
    // images 
    imageLoader.DisplayImage(resultp.get(CatAbc.TAG_fraction_pictogram), fraction_pictogram); 
    // Capture button clicks on ListView items 
    itemView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Get the position from the results 
      HashMap<String, String> resultp = new HashMap<String, String>(); 
      resultp = data2.get(position); 

      AlertDialog alertDialog = new AlertDialog.Builder(context).create(); //Read Update 
      alertDialog.setTitle(resultp.get(CatAbc.TAG_fraction_name)); 
      alertDialog.setMessage(resultp.get(CatAbc.TAG_fraction_typetext) +" " + resultp.get(CatAbc.TAG_fraction_type) +"\n" + resultp.get(CatAbc.TAG_fraction_description)); 

      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // here you can add functions 
        } 
       }); 
       alertDialog.show(); //<-- See This! 
      } 
    }); 

    return itemView; 
} 

@Override 
public Filter getFilter() 
{ 
    return new Filter() 
    { 
     @Override 
     protected FilterResults performFiltering(CharSequence charSequence) 
     { 
      FilterResults results = new FilterResults(); 

      //If there's nothing to filter on, return the original data for your list 
      if(charSequence == null || charSequence.length() == 0) 
      { 
       results.values = data2; 
       results.count = data2.size(); 
      } 
      else 
      { 
       ArrayList<HashMap<String,String>> filterResultsData = new ArrayList<HashMap<String,String>>(); 

       for(HashMap<String,String> data : data2) 
       { 
        if(charSequence == data) 
        { 
         Log.d(TAG, " Dage = Testing 2 chargSquence: " + charSequence); 
         Log.d(TAG, " Dage = Testing 3 data values string: " + data.values().toString()); 

         filterResultsData.add(data); 
        } 
       }    

       results.values = filterResultsData; 
       results.count = filterResultsData.size(); 
      } 

      return results; 
     } 

     @Override 
     protected void publishResults(CharSequence charSequence, FilterResults filterResults) 
     { 
      filteredData = (ArrayList<HashMap<String,String>>)filterResults.values; 
      notifyDataSetChanged(); 
     } 
    }; 
} 
} 

這裏是我的myActivity和textwatcher:

protected void onPostExecute(String file_url) { 
     if(mProgressDialog.isShowing()){ 
      mProgressDialog.dismiss(); 
     } 
     // Locate the listview in listview_main.xml 
     adapter = new ListViewAdapter(CatAbc.this, abcList); 
     // Binds the Adapter to the ListView 
     listview.setAdapter(adapter); 
    } 
} 

private TextWatcher filterTextWatcher = new TextWatcher() 
{ 
    public void afterTextChanged(Editable s) { 
     } 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 
    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     CatAbc.this.adapter.getFilter().filter(s); 
      } 
}; 

誰能幫助我解決這個問題?

回答

0

我從來沒有使用過濾的,但我肯定實現這樣的過濾方案,我BaseAdapter:

private EditText editText; 

editText = (EditText) findViewById(R.id.yourId); 

editText.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 

     } 

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

     } 

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

      textLength = editText.getText().length(); 
      text_sort.clear(); 
      image_sort.clear(); 

      for (int i = 0; i < mTablesTitles.length; i++) { 
       if (textLength <= mTablesTitles[i].length()) { 
        if (editText.getText().toString().equalsIgnoreCase((String)mTablesTitles[i].subSequence(0,textLength))) { 
         text_sort.add(mTablesTitles[i]); 
        } 
       } 
      } 

      mDrawerList.setAdapter(new CustomAdapter(text_sort)); 

      } 
      }); 
+1

那麼謝謝你,但它不是這是我期待的。我研究了我發現我必須在BaseAdapter上實現getFilter,我已經這樣做了,但它沒有找到任何東西。 – Tirolel

+0

這找不到任何東西。它只會通過您輸入的字母來過濾您的列表視圖。您是否嘗試過使用可搜索的界面? –

+0

什麼是mTablesTitles? – pmb