Android 如何使用扩展baseadapter的自定义适配器实现getfilter()

Android 如何使用扩展baseadapter的自定义适配器实现getfilter(),android,listview,Android,Listview,这是我的主要活动 package com.javacodegeeks.android.lbs; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toa

这是我的主要活动

package com.javacodegeeks.android.lbs;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import lbs.promotion.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Curve extends Activity {

    private ProgressDialog pDialog;
     JSONParser jParser = new JSONParser();
     ArrayList<HashMap<String, String>> DaftarPromotion = new ArrayList<HashMap<String, String>>();

     private static String url_daftar_promotion = "http://10.0.2.2/webserver_maaug/promotion/daftar_promotion.php";

     String lon = "101.5178";
     String lat = "3.0724";

     public static final String TAG_SUCCESS = "success";
     public static final String TAG_DAFTAR_PROMOTION = "daftar_promotion";
     public static final String TAG_ID_PROMO = "PromoID";
     public static final String TAG_NAMA_PROMO = "PromoName";
     public static final String TAG_LINK_IMAGE_PROMO = "PromoImage";
     public static final String TAG_DESC_PROMO= "PromoDesc";
     public static final String TAG_CATE_PROMO = "PromoCate";
     public static final String TAG_CONT_PROMO = "PromoContact";
     public static final String TAG_DATES_PROMO = "PromoDateStart";
     public static final String TAG_DATEE_PROMO = "PromoDateEnd";

     JSONArray daftar_promotion = null;

     ListView list;

     PromotionListAdapter adapter;

     EditText inputSearch;

     private Curve activity;

     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_curve);
     DaftarPromotion = new ArrayList<HashMap<String, String>>();

      new Activity().execute();
      activity = this;
      list = (ListView) findViewById(R.id.list);

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

      inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                Curve.this.adapter.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) {
                // TODO Auto-generated method stub
            }
        }); 




      list.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
          HashMap<String, String> map = DaftarPromotion.get(position);
          String pna = map.get(TAG_NAMA_PROMO);
          String pde = map.get(TAG_DESC_PROMO);
          String pca = map.get(TAG_CATE_PROMO);
          String pcon = map.get(TAG_CONT_PROMO);
          String pds = map.get(TAG_DATES_PROMO);
          String pdae = map.get(TAG_DATEE_PROMO);



            Toast.makeText(Curve.this, pna +"\n" + pde +"\n" + pca +"\n" + pds +" to " + pdae +"\n" + pcon,Toast.LENGTH_LONG).show();

      }
      }); 



      }


     public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.smenu, menu);
            return true;
        }
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
            case R.id.about:
                 Toast.makeText(Curve.this,"Mobile Advertising Application Using GPS V1.0",Toast.LENGTH_LONG).show();
            return true;
            case R.id.search:{
                 Intent intent = new Intent(Curve.this, Search.class);
                 intent.putExtra("lon",lon);
                 intent.putExtra("lat",lat);
                 startActivity(intent);
            //startActivity(new Intent(this, Search.class));

            }
            return true;
            default:
            return super.onOptionsItemSelected(item);
        }
        }

      public void SetListViewAdapter(ArrayList<HashMap<String, String>> daftar) {
      adapter = new PromotionListAdapter(activity, daftar);
      list.setAdapter(adapter);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == 100) {
      Intent intent = getIntent();
      finish();
      startActivity(intent);
      }
      }

      class Activity extends AsyncTask<String, String, String> {

      @Override
      protected void onPreExecute() {
      super.onPreExecute();
      pDialog = new ProgressDialog(Curve.this);
      pDialog.setMessage("Please Wait...");
      pDialog.setIndeterminate(false);
      pDialog.setCancelable(false);
      pDialog.show();
      }

      protected String doInBackground(String... args) {
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      JSONObject json = jParser.makeHttpRequest(url_daftar_promotion, "GET",params);

      Log.d("All Products: ", json.toString());
      try {
      int success = json.getInt(TAG_SUCCESS);
      if (success == 1) {
           daftar_promotion = json.getJSONArray(TAG_DAFTAR_PROMOTION);
           for (int i = 0; i < daftar_promotion.length();i++){
           JSONObject c = daftar_promotion.getJSONObject(i);
           String PromoID = c.getString(TAG_ID_PROMO);
           String PromoName = c.getString(TAG_NAMA_PROMO);
           String pat = "http://10.0.2.2/webserver_maaug/";
           String PromoImage = pat + c.getString(TAG_LINK_IMAGE_PROMO);
           String PromoDesc = c.getString(TAG_DESC_PROMO);
           String PromoCate = c.getString(TAG_CATE_PROMO);
           String PromoDateStart = c.getString(TAG_DATES_PROMO);
           String PromoDateEnd = c.getString(TAG_DATEE_PROMO);
           String PromoContact = c.getString(TAG_CONT_PROMO);

           HashMap<String, String> map = new HashMap<String, String>();

           map.put(TAG_ID_PROMO, PromoID);
           map.put(TAG_NAMA_PROMO, PromoName);
           map.put(TAG_LINK_IMAGE_PROMO, PromoImage);
           map.put(TAG_DESC_PROMO, PromoDesc);
           map.put(TAG_CATE_PROMO, PromoCate);
           map.put(TAG_DATES_PROMO, PromoDateStart);
           map.put(TAG_DATEE_PROMO, PromoDateEnd);
           map.put(TAG_CONT_PROMO, PromoContact);

           DaftarPromotion.add(map);
           }

           } else {
           }
           } catch (JSONException e) {
           e.printStackTrace();
           }
           return null;
           }

           protected void onPostExecute(String file_url) {
           pDialog.dismiss();
           runOnUiThread(new Runnable() {
           public void run() {

           SetListViewAdapter(DaftarPromotion);




                }
           });
        }
    }
    }
package com.javacodegeeks.android.lbs;
导入android.os.Bundle;
导入android.app.Activity;
导入android.content.Intent;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.widget.Toast;
进口lbs.促销。*;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.AdapterView.OnItemClickListener;
公共课曲线延伸活动{
私人对话;
JSONParser jParser=新的JSONParser();
ArrayList daftar升级=新建ArrayList();
私有静态字符串url\u daftar\u升级=”http://10.0.2.2/webserver_maaug/promotion/daftar_promotion.php";
字符串lon=“101.5178”;
字符串lat=“3.0724”;
公共静态最终字符串标记_SUCCESS=“SUCCESS”;
公共静态最终字符串TAG\u DAFTAR\u PROMOTION=“DAFTAR\u PROMOTION”;
公共静态最终字符串标记\u ID\u PROMO=“promid”;
公共静态最终字符串标记\u NAMA\u PROMO=“PromoName”;
公共静态最终字符串TAG\u LINK\u IMAGE\u PROMO=“PromoImage”;
公共静态最终字符串标记\u DESC\u PROMO=“promdesc”;
公共静态最终字符串标记\u CATE\u PROMO=“promotate”;
公共静态最终字符串标记\u CONT\u PROMO=“PromoContact”;
公共静态最终字符串标记\u日期\u PROMO=“promotdatestart”;
公共静态最终字符串标记\u DATEE\u PROMO=“promotdateend”;
JSONArray daftar_promotion=null;
列表视图列表;
升级列表适配器;
编辑文本输入搜索;
私人曲线活动;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_曲线);
DaftarPromotion=newarraylist();
新活动().execute();
活动=此;
list=(ListView)findViewById(R.id.list);
inputSearch=(EditText)findViewById(R.id.search\u框);
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
Curve.this.adapter.getFilter().filter(cs);
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
}
}); 
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
HashMap map=DaftarPromotion.get(位置);
字符串pna=map.get(TAG_NAMA_PROMO);
字符串pde=map.get(TAG_DESC_PROMO);
字符串pca=map.get(TAG\u CATE\u PROMO);
字符串pcon=map.get(TAG\u CONT\u PROMO);
字符串pds=map.get(TAG\u DATES\u PROMO);
字符串pdae=map.get(TAG_DATEE_PROMO);
Toast.makeText(Curve.this,pna+“\n”+pde+“\n”+pca+“\n”+pds+”到“+pdae+“\n”+pcon,Toast.LENGTH_LONG)。show();
}
}); 
}
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(R.menu.smenu,menu);
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.关于:
Toast.makeText(Curve.this,“使用GPS V1.0的移动广告应用程序”,Toast.LENGTH_LONG.show();
返回true;
案例R.id.search:{
意向意向=新意向(Curve.this、Search.class);
意向。额外(“lon”,lon);
意向。额外加价(“lat”,lat);
星触觉(意向);
//startActivity(新意图(this,Search.class));
}
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
public void SetListViewAdapter(ArrayList daftar){
adapter=新的PromotionListAdapter(活动,daftar);
list.setAdapter(适配器);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
如果(结果代码==100){
Intent=getIntent();
完成();
星触觉(意向);
}
}
类活动扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(Curve.this);
setMessage(“请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url_daftar_promotion,“GET”,params);
Log.d(“所有产品:,json.toString());
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
daftar\u promotion=json.getJSONArray(TAG\u daftar\u promotion);
for(int i=0;ipackage lbs.promotion;

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

import com.javacodegeeks.android.lbs.Curve;
import com.javacodegeeks.android.lbs.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class PromotionListAdapter extends BaseAdapter {

 private Activity activity;
 private ArrayList<HashMap<String, String>> data;
 private static LayoutInflater inflater = null;
 public ImageLoader imageLoader;

 public PromotionListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {

 activity = a;
 data = d;
 inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 imageLoader = new ImageLoader(activity.getApplicationContext());
 }

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

 public Object getItem(int position) {
 return position;
 }

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

 public View getView(int position, View convertView, ViewGroup parent) {

     View vi = convertView;
      if (convertView == null)vi = inflater.inflate(R.layout.item_list_promotion, null);

      TextView PromoID = (TextView) vi.findViewById(R.id.PromoID);
      TextView PromoName = (TextView) vi.findViewById(R.id.PromoName);
      TextView PromoImage = (TextView) vi.findViewById(R.id.PromoImage);
      TextView PromoDesc = (TextView) vi.findViewById(R.id.PromoDesc);
      TextView PromoCate = (TextView) vi.findViewById(R.id.PromoCate);
      TextView PromoContact = (TextView) vi.findViewById(R.id.PromoContact);
      TextView PromoDateStart = (TextView) vi.findViewById(R.id.PromoDateStart);
      TextView PromoDateEnd = (TextView) vi.findViewById(R.id.PromoDateEnd);
      ImageView thumb_image = (ImageView) vi.findViewById(R.id.image_promo);

      HashMap<String, String> daftar_promotion = new HashMap<String, String>();
      daftar_promotion = data.get(position);

      PromoID.setText(daftar_promotion.get(Curve.TAG_ID_PROMO));

      PromoName.setText(daftar_promotion.get(Curve.TAG_NAMA_PROMO));

      PromoImage.setText(daftar_promotion.get(Curve.TAG_LINK_IMAGE_PROMO));

      PromoDesc.setText(daftar_promotion.get(Curve.TAG_DESC_PROMO));

      PromoCate.setText(daftar_promotion.get(Curve.TAG_CATE_PROMO));

      PromoContact.setText(daftar_promotion.get(Curve.TAG_CONT_PROMO));

      PromoDateStart.setText(daftar_promotion.get(Curve.TAG_DATES_PROMO));

      PromoDateEnd.setText(daftar_promotion.get(Curve.TAG_DATEE_PROMO));

      imageLoader.DisplayImage(daftar_promotion.get(Curve.TAG_LINK_IMAGE_PROMO),thumb_image);

      return vi;

      }




}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

 <!-- Editext for Search -->
    <EditText android:id="@+id/search_box"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="type to search"
        android:inputType="text"
        android:maxLines="1"/>

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#040404"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />

</LinearLayout>
public class PromotionListAdapter extends BaseAdapter implements Filterable
{
    private Activity activity;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    //Two data sources, the original data and filtered data
    private ArrayList<HashMap<String, String>> originalData;
    private ArrayList<HashMap<String, String>> filteredData;

    public PromotionListAdapter(Activity a, ArrayList<HashMap<String, String>> d) 
    {
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());

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

    //For this helper method, return based on filteredData
    public int getCount() 
    {
        return filteredData.size();
    }

    //This should return a data object, not an int
    public Object getItem(int position) 
    {
        return filteredData.get(position);
    }

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

    //The only change here is that we'll use filteredData.get(position)
    //Even better would be to use getItem(position), which uses the helper method (see the getItem() method above)
    public View getView(int position, View convertView, ViewGroup parent) 
    {

       View vi = convertView;
       if (convertView == null)vi = inflater.inflate(R.layout.item_list_promotion, null);

      TextView PromoID = (TextView) vi.findViewById(R.id.PromoID);
      TextView PromoName = (TextView) vi.findViewById(R.id.PromoName);
      TextView PromoImage = (TextView) vi.findViewById(R.id.PromoImage);
      TextView PromoDesc = (TextView) vi.findViewById(R.id.PromoDesc);
      TextView PromoCate = (TextView) vi.findViewById(R.id.PromoCate);
      TextView PromoContact = (TextView) vi.findViewById(R.id.PromoContact);
      TextView PromoDateStart = (TextView) vi.findViewById(R.id.PromoDateStart);
      TextView PromoDateEnd = (TextView) vi.findViewById(R.id.PromoDateEnd);
      ImageView thumb_image = (ImageView) vi.findViewById(R.id.image_promo);

      HashMap<String, String> daftar_promotion = new HashMap<String, String>();

      //Get data from filtered Data
      //This line can be replaced with:
      //     daftar_promotion = getItem(position);
      daftar_promotion = filteredData.get(position);

      PromoID.setText(daftar_promotion.get(Curve.TAG_ID_PROMO));

      PromoName.setText(daftar_promotion.get(Curve.TAG_NAMA_PROMO));

      PromoImage.setText(daftar_promotion.get(Curve.TAG_LINK_IMAGE_PROMO));

      PromoDesc.setText(daftar_promotion.get(Curve.TAG_DESC_PROMO));

      PromoCate.setText(daftar_promotion.get(Curve.TAG_CATE_PROMO));

      PromoContact.setText(daftar_promotion.get(Curve.TAG_CONT_PROMO));

      PromoDateStart.setText(daftar_promotion.get(Curve.TAG_DATES_PROMO));

      PromoDateEnd.setText(daftar_promotion.get(Curve.TAG_DATEE_PROMO));

      imageLoader.DisplayImage(daftar_promotion.get(Curve.TAG_LINK_IMAGE_PROMO),thumb_image);

      return vi;

    }

    @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 = originalData;
                    results.count = originalData.size();
                }
                else
                {
                    ArrayList<HashMap<String,String>> filterResultsData = new ArrayList<HashMap<String,String>>();

                    for(HashMap<String,String> data : originalData)
                    {
                        //In this loop, you'll filter through originalData and compare each item to charSequence.
                        //If you find a match, add it to your new ArrayList
                        //I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
                        if(data matches your filter criteria)
                        {
                            filterResultsData.add(data);
                        }
                    }            

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

                return results;
            }

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