Any body please tel me how to apply filtering on ListView using editText

Discussion in 'Application Development' started by kiranuday43, May 7, 2010.

  1. kiranuday43

    kiranuday43 Member
    15

    Mar 3, 2010
    20
    0
    15
    I had used this following code,,,but it's not working



    private ProgressDialog m_ProgressDialog = null;
    private ArrayList<Order> m_orders = null;
    private OrderAdapter m_adapter;
    private Runnable viewOrders;
    private EditText filterText = null;
    ArrayAdapter<String> adapter = null;
    ArrayList<String> data = new ArrayList<String>();


    LayoutInflater vi;
    View v;
    TextView tt;
    TextView bt;
    ImageView iv;
    ListView lv;

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

    //-------
    data.add("India");
    data.add("US");
    data.add("Pak");
    filterText = (EditText) findViewById(R.id.searchbox);
    filterText.addTextChangedListener(filterTextWatcher);
    adapter = new ArrayAdapter<String>(this,
    R.id.top,
    data);
    //-------


    m_orders = new ArrayList<Order>();
    this.m_adapter = new OrderAdapter(this, R.layout.row,
    m_orders);
    setListAdapter(this.m_adapter);


    viewOrders = new Runnable(){

    @Override
    public void run() {
    getOrders();
    }
    };
    Thread thread = new Thread(null, viewOrders,
    "MagentoBackground");
    thread.start();
    m_ProgressDialog =
    ProgressDialog.show(Listitems.this,
    "Please wait...", "Retrieving data ...", true);

    //------------

    lv = (ListView) findViewById(android.R.id.list);
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),
    ((TextView)arg1.findViewById(R.id.toptext)).getText() ,
    Toast.LENGTH_SHORT).show();

    }
    });

    }
    private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    //adapter.getFilter().filter(s);
    }

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

    public void onTextChanged(CharSequence s, int start, int
    before,
    int count) {
    adapter.getFilter().filter(s);
    /* String text=filterText.getText().toString();
    System.out.println(data.get(0));
    System.out.println(text);

    for(int k=0;k<data.size();k++) {
    if(data.get(k).matches(text)) {
    data.remove(text);
    adapter = new ArrayAdapter<String>(Listitems.this, R.id.top,data);

    }
    }*/
    }

    };
    private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
    if(m_orders != null && m_orders.size() > 0){
    m_adapter.notifyDataSetChanged();
    for(int i=0;i<m_orders.size();i++)
    m_adapter.add(m_orders.get(i));
    }
    m_ProgressDialog.dismiss();
    m_adapter.notifyDataSetChanged();
    }
    };
    private void getOrders(){
    try{
    m_orders = new ArrayList<Order>();
    Order o1 = new Order();
    o1.setOrderName("India");
    o1.setOrderStatus("Asia");
    // o1.setFlagName("in_flag");

    Order o2 = new Order();
    o2.setOrderName("US");
    o2.setOrderStatus("North America");
    // o2.setFlagName("us_flag");

    Order o3 = new Order();
    o3.setOrderName("Pak");
    o3.setOrderStatus("Asia");
    // o3.setFlagName("pk_flag");
    m_orders.add(o1);
    m_orders.add(o2);
    m_orders.add(o3);
    Thread.sleep(500);
    Log.i("ARRAY", ""+ m_orders.size());
    } catch (Exception e) {
    Log.e("BACKGROUND_PROC", e.getMessage());
    }
    runOnUiThread(returnRes);
    }
    private class OrderAdapter extends ArrayAdapter<Order> {

    private ArrayList<Order> items;

    public OrderAdapter(Context context, int textViewResourceId,
    ArrayList<Order> items) {
    super(context, textViewResourceId, items);
    this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup
    parent) {
    v = convertView;
    if (v == null) {
    vi =
    (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.coverlist, null);
    }
    Order o = items.get(position);
    if (o != null) {
    tt = (TextView) v.findViewById(R.id.top);

    iv = (ImageView)v.findViewById(R.id.icon);
    if (tt != null) {

    tt.setText(o.getOrderName()); }
    if(bt != null){
    bt.setText(o.getOrderStatus());
    }
    // int resId =
    // getResources().getIdentifier(o.getFlagName(), "drawable",
    // "com.softberries.lve");
    // iv.setBackgroundResource(resId);

    }
    return v;
    }
    }
     

    Advertisement

Share This Page

Loading...