Hi,

I am having a custom listview [list with image and 2 textbox] and i
want to apply filter for it.

desc of cust listview:

1:Image i.e flag of country

2:1st textbox (name of country)

3:2nd textbox (name of continent)

so when i write " i " in a textbox [different from the ones mentioned
above] i want the list to get filtered according to country name.

My code details:

1:_________________________row.xml [each row of
list]__________________________

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"/>
<!--        android:src="@drawable/icon" />-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="35dip">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        />
    </LinearLayout>
</LinearLayout>

2:__________________main.xml_____________________

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <EditText android:id="@+id/search_box"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="type to filter"
        android:inputType="text"
        android:maxLines="1"/>
<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
<TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/main_no_items"/>
</LinearLayout>

3:_______________________softwarepassionview.java___________________________
package com.softberries.lve;

import java.util.ArrayList;

//import com.TryFilter.R;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Filter.FilterListener;


public class SoftwarePassionView extends ListActivity{

    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.main);

        //-------
        data.add("India");
        data.add("US");
        data.add("Pak");
        filterText = (EditText) findViewById(R.id.search_box);
        filterText.addTextChangedListener(filterTextWatcher);
        adapter = new ArrayAdapter<String>(this,
                R.id.toptext,
                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(SoftwarePassionView.this,
              "Please wait...", "Retrieving data ...", true);

        //------------
        lv = (ListView)findViewById(android.R.id.list);
        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);
                //m_adapter.getFilter().filter(s,(FilterListener)tt);
        }

    };
    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.row, null);
                }
                Order o = items.get(position);
                if (o != null) {
                        tt = (TextView) v.findViewById(R.id.toptext);
                        bt = (TextView)
v.findViewById(R.id.bottomtext);
                        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;
        }
}
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to