如何使用listview实现autocompletetextview?

时间:2015-03-14 06:53:55

标签: android json listview search autocompletetextview

我从服务器获得响应并使用listview显示它并且工作正常,现在我正在尝试的是我添加了autocompletetextview来按名称搜索项目,但是当我运行我的应用程序时它崩溃并显示错误..我已经问过了这个

Tab1Activity.java

   public class Tab1Activity  extends ListActivity{


private ProgressDialog pDialog;
JSONArray Product=null;
private ListView listview;
ArrayList<HashMap<String,String>> aList;

ArrayList<HashMap<String, String>> arrayTemplist;
private static String PRODUCT_URL = "";
private static final String PRODUCT_DATA="Product";
private static final String PRODUCT_ID="productid";
private static final String PRODUCT_NAME="product_name";
private static final String PRODUCT_CODE="skucode";
private static final String PRODUCT_IMAGE="product_photo";
private static final String PRODUCT_WEIGHT="weight";
private static final String PRODUCT_SALERATE="sale_rate";
//private static final String PRODUCT_LOCATION="weight";
private CustomAdapterTabone adapter;
private TextView noacpt;
private AutoCompleteTextView inputSearch;

@Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_tabone);
    //noacpt=(TextView)findViewById(R.id.no_acceptedlist);
    /*String strtext = getIntent().getStringExtra("id");
System.out.println("<<<<<<<< id : " + strtext);*/

    final ListView listview = (ListView)findViewById(android.R.id.list);
    //listview.setSelector( R.drawable.list_selector);

    //listview.setSelector(R.drawable.listselector);
    new LoadAlbums().execute();

    aList = new ArrayList<HashMap<String, String>>();
    final ListView lv = getListView();

    inputSearch = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

    inputSearch.addTextChangedListener(new TextWatcher() {


        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

            arrayTemplist= new ArrayList<HashMap<String,String>>();
            String searchString =inputSearch.getText().toString().toLowerCase();

            for (int i = 0; i < aList.size(); i++)
            {
                String currentString =aList.get(i).get(Tab1Activity.PRODUCT_NAME);
                if (currentString.toLowerCase().startsWith(searchString ))
                {
                    arrayTemplist.add(aList.get(i));
                }
            }

            for (int i = 0; i < arrayTemplist.size(); i++)
            {
                String currentstrin = arrayTemplist.get(i).get(Tab1Activity.PRODUCT_NAME);
                //Toast.makeText(getApplicationContext(), currentstrin, Toast.LENGTH_LONG).show();

            }

            adapter=new CustomAdapterTabone(getApplicationContext(), arrayTemplist);
            listview.setAdapter(adapter);

    /*  SimpleAdapter adapters = new SimpleAdapter(Tab1Activity.this, arrayTemplist,R.layout.list_item_tabone, new String[] { PRODUCT_NAME
            }, new int[] {
                    R.id.txtpronameacptedlist});
            lv.setAdapter(adapters);
            */
        }

        @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                         
        }
    });


  }

    @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(getApplicationContext(), AllProductDetails.class);
intent.putExtra("productid", aList.get(position).get(PRODUCT_ID));
startActivity(intent);
 }

  class LoadAlbums extends AsyncTask<String, String,   ArrayList<HashMap<String,String>>> {
/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(Tab1Activity.this);
    pDialog.setMessage("Loading...");
    pDialog.setIndeterminate(true);
   // pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
    pDialog.setCancelable(false);
    pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
    ServiceHandler sh = new ServiceHandler();
    // Making a request to url and getting response
    ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
    String jsonStr = sh.makeServiceCall(PRODUCT_URL, ServiceHandler.GET);

    Log.d("Response: ", "> " + jsonStr);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            Product = jsonObj.getJSONArray(PRODUCT_DATA);

            for (int i = 0; i < Product.length(); i++) {
                JSONObject c = Product.getJSONObject(i);
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                // adding each child node to HashMap key => value
                map.put(PRODUCT_ID, c.getString(PRODUCT_ID));
                map.put(PRODUCT_NAME,c.getString(PRODUCT_NAME));
                map.put(PRODUCT_CODE, c.getString(PRODUCT_CODE));
                map.put(PRODUCT_IMAGE, c.getString(PRODUCT_IMAGE));
                map.put(PRODUCT_WEIGHT, c.getString(PRODUCT_WEIGHT));
                map.put(PRODUCT_SALERATE, c.getString(PRODUCT_SALERATE)+getResources().getString(R.string.rupee));
               // map.put(INTEREST_ACCEPT_LOCATION, c.getString(INTEREST_ACCEPT_LOCATION));
                // adding HashList to ArrayList
                data.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }
    return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
    super.onPostExecute(result);

    // dismiss the dialog after getting all albums
    if (pDialog.isShowing())
        pDialog.dismiss();
    // updating UI from Background Thread

        aList = new ArrayList<HashMap<String, String>>();
        aList.addAll(result);
        adapter = new CustomAdapterTabone(getApplicationContext(),result);
        setListAdapter(adapter);

        aList.addAll(result);
        adapter.notifyDataSetChanged();



}

}

CustomAdapter

  public class CustomAdapterTabone extends BaseAdapter{


private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;

private static final String TAG_NAME="product_name";
private static final String TAG_PROFILE="skucode";
private static final String TAG_IMAGE="product_photo";
private static final String TAG_CAST="weight";
private static final String TAG_AGE="sale_rate";
  // private static final String TAG_LOCATION="weight";

public CustomAdapterTabone(Context context,ArrayList<HashMap<String,String>> listData) {
    this.context = context;
    this.listData=listData;
    aQuery = new AQuery(this.context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_tabone, null);
        holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txtpronameacptedlist);
        holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecastacptedlist);
        holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
       // holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtproname.setText(listData.get(position).get(TAG_NAME));
    holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
    holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
    holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
    //holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));

    aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
    // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
    return convertView;
}
class ViewHolder{
    ImageView propic;
    TextView txtproname;
    TextView txtproid;
    TextView txtprofilecast;
    TextView txtprofileage;
    TextView txtprofileplace;
}
}

1 个答案:

答案 0 :(得分:17)

首先创建一个自定义适配器实现filterable:

public class MyFilterableAdapter extends BaseAdapter implements Filterable {
    private Context context;
    private List<String> items;
    private List<String> filteredItems;
    private ItemFilter mFilter = new ItemFilter();

    public MyFilterableAdapter(Context context, List<String> items) {
        //super(context, R.layout.your_row, items);
        this.context = context;
        this.items = items;
        this.filteredItems = items;
    }

    @Override
    public int getCount() {
        return filteredItems.size();
    }

    @Override
    public Object getItem(int position) {
        return filteredItems.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.row_search_merchant, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String location = filteredItems.get(position);
        if (!location.isEmpty() || viewHolder != null) {
            viewHolder.tvTitle.setText(location);
        }
        return convertView;
    }

    public static class ViewHolder {
        TextView tvTitle;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String filterString = constraint.toString().toLowerCase();
            FilterResults results = new FilterResults();

            int count = items.size();
            final List<String> tempItems = new ArrayList<>(count);

            for (int i = 0; i < count; i++) {
                if (items.get(i).toLowerCase().contains(filterString)) {
                    tempItems.add(items.get(i));
                }
            }

            results.values = tempItems;
            results.count = tempItems.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredItems = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }
    }

    public Filter getFilter() {
        return mFilter;
    }
}

而不是创建一个textwatcher

public class MyTextWatcher implements TextWatcher {
    private MyCustomAdapter lAdapter;

    public MyTextWatcher(MyCustomAdapter lAdapter) {
        this.lAdapter = lAdapter;
    }

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

    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
        lAdapter.getFilter().filter(s.toString().toLowerCase());
    }
}

最后将TextWatcher添加到搜索编辑文本中:

final MyCustomAdapter lAdapter = new MyCustomAdapter(context, items);
EditText etSearch = (EditText) view.findViewById(R.id.et_search);
        etSearch.addTextChangedListener(new SearchTextWatcher(lAdapter));

我希望这对你有所帮助。