Reputation: 227
i have create an application in which the in my adapter class i set the arraylist in textview..,but on search i want to add another list that have filtered data that user search with the textwatcher in the textview.As my adapter is having whole contact list,i want to replace it with new list that is created with the help of textwatcher. MainAactivity
case R.id.action_search:
searchText.setVisibility(View.VISIBLE);
searchText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub
s=searchText.getText().toString();
ma.getFilter().filter(s);
}
});
break;
}
return super.onOptionsItemSelected(item);
}
MyAdapter
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = mInflater.inflate(R.layout.row, null);
tv = (TextView) vi.findViewById(R.id.textView1);
tv1 = (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText(name1.get(position));
tv1.setText(phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
// Toast.makeText(getApplication(), String.valueOf(name1.size()).toString(), Toast.LENGTH_LONG).show();
return vi;
}
getfilter()
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
Filter filter = new Filter() {
public void publishResults(CharSequence s, FilterResults results) {
//Toast.makeText(getApplication(), String.valueOf(name1.size()).toString(), Toast.LENGTH_LONG).show();
arrayListNames = (List<String>) results.values;
notifyDataSetChanged();
}
@Override
public FilterResults performFiltering(CharSequence s) {
search=true;
FilterResults results = new FilterResults();
ArrayList<String> FilteredArrayNames = new ArrayList<String>();
Toast.LENGTH_LONG).show();
for (int i = 0; i < name1.size(); i++) {
String dataNames = name1.get(i).toString();
if (dataNames.toLowerCase().trim().startsWith(s.toString())) {
Toast.makeText(getApplication(),s.toString(), Toast.LENGTH_LONG).show();
FilteredArrayNames.add(name1.get(i));
}
}
results.count = FilteredArrayNames.size();
results.values = FilteredArrayNames;
return results;
}
};
return filter;
}
}
Upvotes: 1
Views: 796
Reputation: 6108
MainActivity.java
package com.example.mylistviewtest;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listview;
private EditText edittext;
private List<ProfileBean> list;
private SearchableAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
edittext = (EditText) findViewById(R.id.edittext);
list = new ArrayList<ProfileBean>();
list.add(new ProfileBean("AAA", "AA", "123"));
list.add(new ProfileBean("AAB", "AB", "100"));
list.add(new ProfileBean("AAC", "BC", "101"));
list.add(new ProfileBean("AAD", "DC", "154"));
list.add(new ProfileBean("BBB", "DF", "22"));
list.add(new ProfileBean("BBA", "AD", "22"));
list.add(new ProfileBean("BBD", "FD", "44"));
list.add(new ProfileBean("BAT", "AD", "44"));
list.add(new ProfileBean("BBD", "FF", "75"));
list.add(new ProfileBean("BAD", "AA", "88"));
adapter = new SearchableAdapter(getApplicationContext(), list);
listview.setAdapter(adapter);
edittext.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ProfileBean bean = (ProfileBean) arg1.getTag();
Toast.makeText(getApplicationContext(), bean.getName(), Toast.LENGTH_LONG).show();
}
});
}
}
ProfileBean
package com.example.mylistviewtest;
public class ProfileBean {
private String name;
private String lname;
private String no;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public ProfileBean(String name, String lname, String no) {
super();
this.name = name;
this.lname = lname;
this.no = no;
}
}
SearchableAdapter.java
package com.example.mylistviewtest;
import java.util.ArrayList;
import java.util.List;
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.TextView;
// The standard text view adapter only seems to search from the beginning of whole words
// so we've had to write this whole class to make it possible to search
// for parts of the arbitrary string we want
public class SearchableAdapter extends BaseAdapter implements Filterable {
private List<ProfileBean>originalData = null;
private List<ProfileBean>filteredData = null;
private LayoutInflater mInflater;
private ItemFilter mFilter = new ItemFilter();
public SearchableAdapter(Context context, List<ProfileBean> data) {
this.filteredData = data ;
this.originalData = data ;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unnecessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.lname = (TextView) convertView.findViewById(R.id.lname);
holder.no = (TextView) convertView.findViewById(R.id.no);
// Bind the data efficiently with the holder.
convertView.setTag(R.layout.list_item,holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag(R.layout.list_item);
}
// If weren't re-ordering this you could rely on what you set last time
ProfileBean bean = filteredData.get(position);
holder.name.setText(bean.getName());
holder.lname.setText(bean.getLname());
holder.no.setText(bean.getNo());
convertView.setTag(bean);
return convertView;
}
static class ViewHolder {
TextView name;
TextView lname;
TextView no;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<ProfileBean> list = originalData;
int count = list.size();
final ArrayList<ProfileBean> nlist = new ArrayList<ProfileBean>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
ProfileBean bean = list.get(i);
filterableString = bean.getName();
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(bean);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<ProfileBean>) results.values;
notifyDataSetChanged();
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="16sp" >
</TextView>
<TextView
android:id="@+id/lname"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="16sp" >
</TextView>
<TextView
android:id="@+id/no"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="16sp" >
</TextView>
</LinearLayout>
Upvotes: 1