Here is my class.
package com.android.list;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
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.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText _edittext;
CustomListAdapter _listadapter;
private List<User> _arrayList=new ArrayList<User>();
private ListView _listview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_edittext = (EditText) findViewById(R.id.EditText01);
_edittext.addTextChangedListener(searchTextWatcher);
_listview = (ListView) findViewById(R.id.ListView01);
User user = new User();
user.setFirstName("india");
_arrayList.add(user);
User user1 = new User();
user1.setFirstName("usa");
_arrayList.add(user1);
User user2 = new User();
user2.setFirstName("china");
_arrayList.add(user2);
User user3 = new User();
user3.setFirstName("srilanka");
_arrayList.add(user3);
User user4 = new User();
user4.setFirstName("japan");
_arrayList.add(user4);
User user5 = new User();
user5.setFirstName("england");
_arrayList.add(user5);
_listadapter = new CustomListAdapter(this);
_listview.setAdapter(_listadapter);
}
private class CustomListAdapter extends BaseAdapter implements
Filterable {
private LayoutInflater _inflater;
public CustomListAdapter(Context context) {
_inflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return _arrayList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertview, ViewGroup arg2)
{
ViewHolder _viewholder;
if (convertview == null) {
convertview = _inflater.inflate(R.layout.listrow, null);
_viewholder = new ViewHolder();
_viewholder.text = (TextView) convertview
.findViewById(R.id.TextView01);
_viewholder.icon = (ImageView) convertview
.findViewById(R.id.ImageView01);
convertview.setTag(_viewholder);
} else {
_viewholder=(ViewHolder)convertview.getTag();
}
_viewholder.text.setText(_arrayList.get(position).getFirstName());
_viewholder.icon.setImageResource(R.drawable.icon);
return convertview;
}
public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
_arrayList = (List<User>) results.values;
CustomListAdapter.this.notifyDataSetChanged();
}
protected FilterResults performFiltering(CharSequence
prefix) {
FilterResults results = new FilterResults();
ArrayList<User> i = new ArrayList<User>();
if (prefix!= null && prefix.toString().length() > 0) {
for (int index = 0; index < _arrayList.size();
index++) {
User si = _arrayList.get(index);
Log.i("----------------si.getFirstName()---------","."+si.getFirstName());
Log.i("----------------prefix---------","."+prefix.toString());
if(si.getFirstName().compareTo(prefix.toString()) == 0){
i.add(si);
}
}
results.values = i;
results.count = i.size();
}
else{
synchronized (_arrayList){
results.values = _arrayList;
results.count = _arrayList.size();
}
}
return results;
}
};
}
}
static class ViewHolder {
TextView text;
ImageView icon;
}
private TextWatcher searchTextWatcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// ignore
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// ignore
}
public void afterTextChanged(Editable s) {
_listadapter.getFilter().filter(s.toString());
}
};
}
as i have add textchangelistner to edittext.when i add only one letter it
compares the list and return me null(empty list);
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:hint="search" android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></EditText>
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
listrow.xml
------------------
<?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="fill_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"></ImageView>
<TextView android:text="Textrow" android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
User.java
-------------
package com.android.list;
public class User {
private int UserId;
private String FirstName;
private String LastName;
public int getUserId() {
return UserId;
}
public void setUserId(int UserId) {
this.UserId = UserId;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
}
Just try this one u ll get idea about my problem.
On Sat, Jul 30, 2011 at 12:10 AM, dhirendra <[email protected]>wrote:
> In getView function of baseAdapter you make each view of list and
> return it. There you can do whatever you want to do with view.
> If this is not the answer, then please specify what do you mean by
> filtering here !!!
>
> On Jul 30, 10:47 am, Hitendrasinh Gohil
> <[email protected]> wrote:
> > Hi,
> > I am using baseadapter with listview.my listrow contains imagview and
> > textview.so how can i filter listview?
>
> --
> 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
>
--
Regards,
Hitendrasinh Gohil
--
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