handle list view with check box and search implemention - android

i have a list view which has check box on each row and i`ve implemented a serch view to search in the list view. the problem is that when i search the list view and select item at postion X from the modifided list, delete the text from the search view i see the original list with the item checked in position X.
Example - list of a,b,c....z .
i can see 5 items on the list:
A
B
C
D
E
then search for z so i see only Z and check it. then i delete Z and see A-E again and A is checked.
this is my main:
public class AddRoomatesScreen extends Activity implements AdapterView.OnItemClickListener,SearchView.OnQueryTextListener {
SharedPreferences preferences;
List<String> nameList = new ArrayList<String>();
List<String> phoneList = new ArrayList<String>();
MyAdapter adapter;
Button btnSelect;
String apartmentnumber;
SearchView searchView;
ArrayList<Contact> contactList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_roomate);
preferences = getSharedPreferences("appData", 0);
int apartmentNumber = preferences.getInt("apartmentNumber", 0);
apartmentnumber = Integer.toString(apartmentNumber);
getAllContacts(this.getContentResolver());
ListView lv = (ListView) findViewById(R.id.lv);
adapter = new MyAdapter(nameList,contactList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
searchView = (SearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(this);
// adding
btnSelect = (Button) findViewById(R.id.addSelectedContacts);
btnSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i<adapter.mCheckStates.size(); i++){
Contact contact = (adapter.contactsListCopy.get(adapter.mCheckStates.keyAt(i)));
new addRoommate().execute(apartmentnumber, contact.getPhoneNumber());
}
preferences = getSharedPreferences("appData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("roomatesLoadedFromDB", false);
editor.apply();
}
});
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
adapter.toggle(arg2);
}
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
nameList.add(name);
phoneList.add(phoneNumber);
contactList.add(new Contact(name,phoneNumber));
}
phones.close();
}
this is my adapter:
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener,Filterable {
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv;
CheckBox cb;
ValueFilter filter;
ArrayList<Contact> contactsList;
ArrayList<Contact> contactsListCopy;
MyAdapter(List<String> nameList , ArrayList<Contact> contactsList) {
mCheckStates = new SparseBooleanArray(contactsList.size());
mInflater = (LayoutInflater) AddRoomatesScreen.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.contactsList = contactsList;
this.contactsListCopy = this.contactsList;
getFilter();
}
#Override
public int getCount() {
return contactsListCopy.size();
}
#Override
public Object getItem(int position) {
return contactsListCopy.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#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.contact_list_item, null);
TextView tv = (TextView) vi.findViewById(R.id.textView3);
cb = (CheckBox) vi.findViewById(R.id.checkBox);
Contact contact = contactsListCopy.get(position);
tv.setText(contact.getName());
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
#Override
public Filter getFilter() {
if (filter == null){
filter = new ValueFilter();
}
return filter;
}
private class ValueFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Contact> temp = new ArrayList<Contact>();
for (int i = 0; i < contactList.size(); i++) {
Contact c = contactList.get(i);
if ((c.getName().toString())
.contains(constraint.toString())) {
temp.add(c);
}
}
results.count = temp.size();
results.values = temp;
} else {
results.count = contactList.size();
results.values = contactList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
contactsListCopy = (ArrayList<Contact>) results.values;
notifyDataSetChanged();
}
}
}
this is my model:
public class Contact{
private String phoneNumber;
private String name;
public Contact(String name , String phoneNumber){
this.phoneNumber = phoneNumber;
this.name = name;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
}

EDIT:
You can use the ID from the cursor as your unique id.
This code would go with the code originally posted below.
Add id to your Contact class:
public class Contact{
private long id;
private String phoneNumber;
private String name;
public Contact(long id, String name , String phoneNumber) {
this.id = id;
this.phoneNumber = phoneNumber;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
This is how you get the ID from the cursor:
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
long id = phones.getLong(phones.getColumnIndex(ContactsContract.Data._ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
nameList.add(name);
phoneList.add(phoneNumber);
contactList.add(new Contact(id, name, phoneNumber));
}
phones.close();
}
Your mCheckStates in the adapter changes to:
private LongSparseArray<Boolean> mCheckStates;
Some of the original answer below has been changed to match the long type used for id.
The problem is that your mCheckStates boolean map isn't kept in sync with contactsListCopy as it is filtered.
You filtered on Z so Z is now item 0,
You checked Z which sets position 0 as checked,
You redisplay the whole list. Now A is position 0 so it shows as checked.
Instead of tracking checked positions, you need to track checked contacts. To do that, you need to use the unique ID from the contact record.
Here's the changes you need to make:
Make your adapter use the unique id:
#Override
public long getItemId(int position) {
return contactsListCopy.get(position).getId();
}
Now your activity has to use the unique id instead of position:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
adapter.toggle(arg3);
}
In your adapter getView(), you have to use the ID for the check box now
cb.setTag(contact.getId());
cb.setChecked(mCheckStates.get(contact.getId(), false));
I would change some other methods just to avoid confusion:
public boolean isChecked(long id) {
return mCheckStates.get(id, false);
}
public void setChecked(long id, boolean isChecked) {
mCheckStates.put(id, isChecked);
notifyDataSetChanged();
}
public void toggle(long id) {
setChecked(id, !isChecked(id));
}

Related

Android listview with filter issue

I have a listview of items coming from a database. Now I have put an edit text at the top of my listview. What I want is that when a user start typing in the edit text, the listview should be filtered. For example, if the user enters a letter "A", only the names starting with an "A" should appear in the list. I have used a custom adapter in my code. I asked this question many times, but haven't found a solution yet.
This is my data list activity code:
public class DataListActivity extends Activity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
FoodDbHelper foodDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
private Button button1;
ListDataAdapter dataAdapter = null;
Button button;
DataProvider dataProvider;
ArrayList<HashMap<String, String>> namessList;
EditText inputSearch;
String search_name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.data_list_layout);
inputSearch = (EditText)findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changes the Text
listDataAdapter.getFilter().filter(cs);
}
#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
}
});
listView = (ListView) findViewById(R.id.list_View);
listDataAdapter = new ListDataAdapter(getApplicationContext(),
R.layout.row_layout) {
#Override
protected void showCheckedButton(int position, boolean value) {
// TODO Auto-generated method stub
DataProvider item = (DataProvider) listDataAdapter
.getItem(position);
Log.i("", "");
item.setSelected(value);
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText
.append("The following dishes were selected...\n");
ArrayList<DataProvider> list = listDataAdapter
.getSelectedIndexes();
int sum = 0;
for (int i = 0; i < list.size(); i++) {
DataProvider dataProvider = list.get(i);
sum = sum + dataProvider.getCalorie();
responseText.append("\n" + dataProvider.getName()
+ " : " + dataProvider.getCalorie()
+ " kcal"
);
}
Toast.makeText(getApplicationContext(), ""+responseText+"\n"+"................................."
+"\n"+"Total Calories In Your Menu Is : " +sum,
Toast.LENGTH_LONG).show();
}
});
}
};
listView.setAdapter(listDataAdapter);
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getReadableDatabase();
cursor = foodDbHelper.getInformations(sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String name, quantity, fat, protein, sugar, vitamins;
boolean selected = false;
String names = null;
Integer calorie;
name = cursor.getString(0);
quantity = cursor.getString(1);
calorie = Integer.valueOf(cursor.getString(2));
fat = cursor.getString(3);
protein = cursor.getString(4);
sugar = cursor.getString(5);
vitamins = cursor.getString(6);
DataProvider dataProvider = new DataProvider(name, quantity,
calorie, fat, protein, sugar, vitamins, names, selected);
listDataAdapter.add(dataProvider);
} while (cursor.moveToNext());
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = (String) ((TextView) view
.findViewById(R.id.text_dish_name)).getText();
String quantity = (String) ((TextView) view
.findViewById(R.id.text_dish_quantity)).getText();
String calorie = (String) ((TextView) view
.findViewById(R.id.text_dish_calorie)).getText();
String fat = (String) ((TextView) view
.findViewById(R.id.text_dish_fat)).getText();
String protein = (String) ((TextView) view
.findViewById(R.id.text_dish_protein)).getText();
String sugar = (String) ((TextView) view
.findViewById(R.id.text_dish_sugar)).getText();
String vitamins = (String) ((TextView) view
.findViewById(R.id.text_dish_vitamins)).getText();
String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(),
"dish name is : " + name, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),
Detail.class);
intent.putExtra("Dish name", name);
intent.putExtra("Dish quantity", quantity);
intent.putExtra("Dish calorie", calorie);
intent.putExtra("Dish fat", fat);
intent.putExtra("Dish protein", protein);
intent.putExtra("Dish sugar", sugar);
intent.putExtra("Dish vitamins", vitamins);
startActivity(intent);
}
});
}
public void gobackk(View view) {
Intent intent = new Intent(this, MainMenu.class);
startActivity(intent);
}
Use the addTextChangedListener() method of your EditText. It would look something like this
inputSearch = (EditText)findViewById(R.id.editText);
....
....
listView.setAdapter(listDataAdapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changes the Text
listDataAdapter.getFilter().filter(cs);
}
#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
}
});
Instead of EditText can you use SearchView in your Xml?? If that's not problem then here you have complete working code with adapter and xml everything.!
XML
<RelativeLayout 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"
tools:context=".MyActivity" >
<SearchView
android:id="#+id/searchPlayers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/Blue600"
android:layout_alignParentTop="true" />
<ListView
android:id="#+id/playersList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/searchPlayers"
android:divider="#color/Teal"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true" />
Let your mainActivity extends Activity implements SearchView.OnQueryTextListener, Filterable,SearchView.OnCloseListener
Inside onCreate() of MainActivity:
searchView = (SearchView) findViewById(R.id.searchPlayers);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
searchView.setQueryHint("Search by Player");
Override these in you MainActivity:
#Override
public boolean onQueryTextChange(String newText) {list will get filtered as per input.
if (TextUtils.isEmpty(newText)) {
listView.clearTextFilter();
customAdapter.getFilter().filter("");
} else {
if(customAdapter!=null){
PlayersCustomAdapter ca = (PlayersCustomAdapter) listView.getAdapter();
ca.getFilter().filter(newText);
}
}
return true;
}
#Override
public boolean onQueryTextSubmit(String abc) {
return false;
}
#Override
public android.widget.Filter getFilter() {
return null;
}
#Override
public boolean onClose() {//it will set the list to initial position when searchView is closed
customAdapter.getFilter().filter("");
return false;
}
Here is the Custom Adapter..!
public class PlayersCustomAdapter extends BaseAdapter implements Filterable {
Context context;
ArrayList<PlayersMapper> playersMapperList, filteredplayersMapperList;
public PlayersCustomAdapter(Context context, ArrayList<PlayersMapper> list) {
super();
//PlayersMapper is the model~mapper class, where i have all views which i am getting from my web service.
this.context = context;
this.playersMapperList = list;
}
public PlayersCustomAdapter(){
}
class ViewHolder {
protected TextView tvPlayerName;
protected TextView tvTeamName;
protected TextView tvNationality;
protected ImageView ivPlayerPic;
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filteringResult = new FilterResults();
final ArrayList<PlayersMapper> filterdResult = new ArrayList<PlayersMapper>();
if (filteredplayersMapperList == null)
filteredplayersMapperList = new ArrayList<PlayersMapper>(playersMapperList);
if (constraint != null) {
if (filteredplayersMapperList != null
&& filteredplayersMapperList.size() > 0) {
for (final PlayersMapper pmObj : filteredplayersMapperList) {
if (pmObj.PlayerName.contains(constraint.toString()))
filterdResult.add(pmObj);
}
}
filteringResult.values = filterdResult;
filteringResult.count = filterdResult.size();
}
return filteringResult;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
playersMapperList = (ArrayList<PlayersMapper>) results.values;
notifyDataSetChanged();
}
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return playersMapperList.size();
}
#Override
public Object getItem(int position) {
return playersMapperList.get(position);
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
view = inflator.inflate(R.layout.players_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.tvPlayerName = (TextView) view
.findViewById(R.id.txtViewPlayerName);
viewHolder.tvTeamName = (TextView) view
.findViewById(R.id.txtViewTeamName);
viewHolder.tvNationality = (TextView) view
.findViewById(R.id.txtViewCountryName);
viewHolder.ivPlayerPic = (ImageView) view
.findViewById(R.id.imgViewPlayers);
view.setTag(viewHolder);
ViewHolder holder = (ViewHolder) view.getTag();
holder.tvPlayerName.setText(playersMapperList.get(position).PlayerName);
holder.tvTeamName.setText(playersMapperList.get(position).TeamName);
holder.tvNationality
.setText(playersMapperList.get(position).Nationality);
return view;
}
}

position of checkbox get change

I am trying to apply filter on my custom adapter which extends BaseAdapter, in which I am facing some problems, after I filter input based on the text in EditText and check the CheckBox to select one value and if I erase the text in the EditText to search for some other thing the position of the checked checkbox changes.
MainActivty
public class MainActivity extends Activity implements OnItemClickListener {
EditText searchText;
ArrayList<String> phno0 = new ArrayList<String>();
List<String> arrayListNames;
// private ListView listview;
// private EditText edittext;
public List<ProfileBean> list;
public SearchableAdapter adapter;
ProfileBean bean;
String[] cellArray = null;
String contacts;
ListView lv;
String phoneNumber, name;
// StringBuilder b = new StringBuilder();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(
Color.parseColor("#00aef0"));
actionBar.setBackgroundDrawable(colorDrawable);
setContentView(R.layout.get);
// mStatusView = (TextView) findViewById(R.id.text1);
searchText = (AutoCompleteTextView) findViewById(R.id.autocomplete);
lv = (ListView) findViewById(R.id.listview);
list = new ArrayList<ProfileBean>();
getAllCallLogs(this.getContentResolver());
adapter = new SearchableAdapter(getApplicationContext(), list);
lv.setAdapter(adapter);
lv.setItemsCanFocus(false);
lv.setOnItemClickListener(this);
lv.setTextFilterEnabled(true);
contacts = SmsSend.contacts;
if (SmsSend.contacts != null) {
cellArray = contacts.split(";");
//contacts=null;
// Toast.makeText(getApplication(), contacts.toString(),
// Toast.LENGTH_LONG).show();
for (int i = 0; i < cellArray.length; i++) {
for (int j = 0; j < list.size(); j++) {
if (cellArray[i].equals(list.get(j).getNumber())) {
adapter.setChecked(j, true);
// break;
}
}
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contact_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
//StringBuilder checkedcontacts = new StringBuilder();
// System.out.println(".............." +
// adapter.mCheckStates.size());
for (int i = 0; i < list.size(); i++)
{
if (adapter.mCheckStates.get(i) == true) {
phno0.add(list.get(i).getNumber());
//checkedcontacts.append(list.get(i).toString());
// checkedcontacts.append("\n");
// Toast.makeText(getApplicationContext(),
// list.get(i).getNumber().toString(),
// Toast.LENGTH_LONG).show();
} else {
System.out.println("..Not Checked......"
+ list.get(i).getNumber().toString());
}
}
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);
setResult(RESULT_OK, returnIntent);
finish();
break;
case R.id.addPage:
break;
case R.id.action_search:
searchText.setVisibility(View.VISIBLE);
searchText.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());
}
});
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
phno0.clear();
//StringBuilder checkedcontacts = new StringBuilder();
// System.out.println(".............." + adapter.mCheckStates.size());
for (int i = 0; i < list.size(); i++)
{
if (adapter.mCheckStates.get(i) == true) {
phno0.add(list.get(i).getNumber());
} else {
System.out.println("..Not Checked......"
+ list.get(i).getNumber().toString());
}
}
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);
setResult(RESULT_OK, returnIntent);
finish();
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
// TODO Auto-generated method stub
adapter.toggle(position);
}
public void getAllCallLogs(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
list.add(new ProfileBean(name, phoneNumber));
}
phones.close();
}
}
SearchableAdapter
public class SearchableAdapter extends BaseAdapter implements Filterable, OnCheckedChangeListener {
public SparseBooleanArray mCheckStates;
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) {
//mCheckStates = new SparseBooleanArray(filteredData.size());
mCheckStates = new SparseBooleanArray(data.size());
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) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView1);
holder.number = (TextView) convertView.findViewById(R.id.textView2);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.chk.setTag(position);
convertView.setTag(R.layout.row,holder);
} else {
holder = (ViewHolder) convertView.getTag(R.layout.row);
}
holder.chk.setOnCheckedChangeListener(null);
holder.chk.setOnCheckedChangeListener(this);
ProfileBean bean = filteredData.get(position);
holder.name.setText(bean.getName());
holder.number.setText(bean.getNumber());
convertView.setTag(bean);
return convertView;
}
static class ViewHolder {
TextView name;
TextView number;
CheckBox chk;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
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.toString().toLowerCase())) {
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();
}
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
ProfileBean Class
public class ProfileBean {
private String name;
private String number;
//private boolean checked = false ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public ProfileBean(String name, String number) {
super();
this.name = name;
this.number = number;
}
}
Change your Adapter like this..
public class SearchableAdapter extends BaseAdapter implements Filterable,
OnCheckedChangeListener {
public SparseBooleanArray mCheckStates;
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) {
// mCheckStates = new SparseBooleanArray(filteredData.size());
mCheckStates = new SparseBooleanArray(data.size());
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) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView1);
holder.number = (TextView) convertView.findViewById(R.id.textView2);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.chk.setTag(position);
convertView.setTag(R.layout.row, holder);
} else {
holder = (ViewHolder) convertView.getTag(R.layout.row);
}
ProfileBean bean = filteredData.get(position);
holder.name.setText(bean.getName());
holder.number.setText(bean.getNumber());
holder.chk.setOnCheckedChangeListener(null);
holder.chk.setChecked(bean.isChecked);
holder.chk.setOnCheckedChangeListener(this);
convertView.setTag(bean);
return convertView;
}
static class ViewHolder {
TextView name;
TextView number;
CheckBox chk;
}
public void toggle(int position) {
ProfileBean bean = filteredData.get(position);
bean.isChecked = !bean.isChecked;
}
public android.widget.Filter getFilter() {
return mFilter;
}
private class ItemFilter extends android.widget.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.toString().toLowerCase())) {
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();
}
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int position = (Integer) buttonView.getTag();
ProfileBean profileBean = filteredData.get(position);
profileBean.isChecked = isChecked;
// mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
and your bean class like..
public class ProfileBean {
private String name;
private String number;
public boolean isChecked;
// private boolean checked = false ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public ProfileBean(String name, String number) {
super();
this.name = name;
this.number = number;
}
}
And keep remaining same..try like this and let know if any problem..
Change your onBkpress method like this..
#Override
public void onBackPressed() {
phno0.clear();
for (ProfileBean bean : list) {
if (bean.isChecked) {
phno0.add(bean.getNumber());
} else {
System.out.println("..Not Checked......"
+ list.get(i).getNumber().toString());
}
}
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);
setResult(RESULT_OK, returnIntent);
finish();
}

Listview search with custom adapter not showing search results properly

Searching in Listview are not working properly. If i enter string that is in my array list it shows all the records but if i enter any other string in my edit text field it shows an empty list.
public class GetCustomList extends ListActivity {
TweetListAdaptor myAdaptor;
ListView lv;
EditText searchTxt;
public ArrayList<Contact> tweets = new ArrayList<Contact>();
DatabaseHandler db = new DatabaseHandler(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_info);
tweets = db.getAllContacts();
lv = (ListView) findViewById(android.R.id.list);
lv.setTextFilterEnabled(true);
searchTxt = (EditText) findViewById(R.id.editTextSearchNameField);
searchTxt.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
myAdaptor.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Button btnSearch = (Button) findViewById(R.id.buttonSearch);
btnSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onPostExecute(null);
}
});
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("786","zahid", "bilaltown", '0'));
db.addContact(new Contact("123","Shahid", "bilaltown", '0'));
db.addContact(new Contact("123","waqas", "bilaltown", '0'));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
for (Contact cn : tweets) {
String log = "Id: "+cn.get_id()+ ",Code:"+ cn.get_code() +" ,Name: " + cn.get_name() + " ,Address: " + cn.get_address() + " ,Phone: " + cn.get_limit();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
protected void onPostExecute(Void result) {
myAdaptor = new TweetListAdaptor(GetCustomList.this, tweets);
myAdaptor.notifyDataSetChanged();
setListAdapter(myAdaptor);
}
public class TweetListAdaptor extends ArrayAdapter<Contact> implements Filterable {
private ArrayList<Contact> mOriginalValues = new ArrayList<Contact>(); // Original Values
private ArrayList<Contact> mDisplayedValues = new ArrayList<Contact>(); // Values to be displayed
LayoutInflater inflater;
Context context;
public TweetListAdaptor(Context context,ArrayList<Contact> items) {
super(context, R.layout.custom_list_info, items);
this.mOriginalValues = items;
this.mDisplayedValues = items;
this.context = context;
// inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mDisplayedValues.size();
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
private TextView CustomerId;
private TextView CustomerShop;
private TextView date;
private TextView phoneNumber;
public ViewHolder(View v) {
this.CustomerId = (TextView) v.findViewById(R.id.textViewCustomerID);
this.CustomerShop = (TextView) v.findViewById(R.id.textViewShopName);
// this.date = (TextView) v.findViewById(R.id.date);
// this.status = (TextView) v.findViewById(R.id.staus);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_list_info, null);
holder = new ViewHolder(v);
v.setTag(holder);
}else {
holder = (ViewHolder) v.getTag();
}
final Contact o = getItem(position);
if (o != null) {
holder.CustomerId.setText(""+o._id);
holder.CustomerShop.setText(o._name);
//holder.date.setText(o.date);
//holder.status.setText(o.status_txt);
/*if(o.status.equals("1")){
RelativeLayout llr = (RelativeLayout) v.findViewById(R.id.testRes);
llr.setBackgroundResource(R.drawable.decline);
}else if(o.status.equals("2")){
RelativeLayout llr = (RelativeLayout) v.findViewById(R.id.testRes);
llr.setBackgroundResource(R.drawable.acceptence);
}
}*/
ImageButton btnSearchCustomer = (ImageButton) v.findViewById(R.id.btnSearchCustomer);
btnSearchCustomer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(GetCustomList.this, CustomerDetailedInfo.class);
intent.putExtra("ccode", o._code);
intent.putExtra("cname", o._name);
intent.putExtra("caddress", o._address);
intent.putExtra("climit", o._limit);
startActivity(intent);
}
});
}
return v;
}
///////////////////////////////
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,FilterResults results) {
mDisplayedValues = (ArrayList<Contact>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<Contact> FilteredArrList = new ArrayList<Contact>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<Contact>(mDisplayedValues); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i)._name;
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(new Contact(mOriginalValues.get(i)._name,mOriginalValues.get(i)._code,mOriginalValues.get(i)._address,mOriginalValues.get(i)._limit));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
///////////////////
}
Contact class
public class Contact {
//private variables
int _id;
String _code;
String _name;
String _address;
int _limit;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String _code,String name,String address, int _limit){
this._id = id;
this._code = _code;
this._name = name;
this._address = address;
this._limit = _limit;
}
// constructor
public Contact(String _code,String name, String address,int _limit){
this._code = _code;
this._name = name;
this._address = address;
this._limit = _limit;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String get_code() {
return _code;
}
public void set_code(String _code) {
this._code = _code;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String get_address() {
return _address;
}
public void set_address(String _address) {
this._address = _address;
}
public int get_limit() {
return _limit;
}
public void set_limit(int _limit) {
this._limit = _limit;
}
}
There are a couple of minor mistakes in the code. First, you need to override the getItem() method in TweetListAdaptor:
#Override
public Contact getItem(int position)
{
return mDisplayedValues.get(position);
}
Then, in the Filter's performFiltering() method, the code and name parameters are switched for the Contact constructor in the following line:
FilteredArrList.add(new Contact(mOriginalValues.get(i)._code, mOriginalValues.get(i)._name, mOriginalValues.get(i)._address, mOriginalValues.get(i)._limit));
I ran a test with 20 names, and I believe that those corrections should fix the problem.

how to get the checked contacts details in a string

i want to get the contact numbers in a string when user checked the contact,after which onBackpress the selected values can be stored in database.user can select multiple contacts at a time and when user returns back on the activity,the check box remains checked, so that he can see his selected contacts.
MainActivity
public class MainActivity extends Activity {
String[] cellArray = null;
String contacts;
String phoneNumber, name;
ArrayList<String> phno0 = new ArrayList<String>();
StringBuilder b = new StringBuilder();
//private ArrayAdapter<String> adapter;
List<String> arrayListNames;
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>();
getAllCallLogs(this.getContentResolver());
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();
}
});
}
public void getAllCallLogs(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
list.add(new ProfileBean(name, phoneNumber));
}
}
}
Adapterclass
public class SearchableAdapter extends BaseAdapter implements Filterable, OnCheckedChangeListener {
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) {
//mCheckStates = new SparseBooleanArray(filteredData.size());
this.filteredData = data ;
this.originalData = data ;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.lname = (TextView) convertView.findViewById(R.id.lname);
holder.no = (CheckBox) convertView.findViewById(R.id.no);
holder.no.setTag(position);
holder.no.setOnCheckedChangeListener(this);
convertView.setTag(R.layout.list_item,holder);
} else {
holder = (ViewHolder) convertView.getTag(R.layout.list_item);
}
ProfileBean bean = filteredData.get(position);
holder.name.setText(bean.getName());
holder.lname.setText(bean.getLname());
convertView.setTag(bean);
return convertView;
}
static class ViewHolder {
TextView name;
TextView lname;
CheckBox 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;
}
BeanClass
package com.example.mylistviewtest;
public class ProfileBean {
private String name;
private String lname;
private boolean checked = false ;
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 ProfileBean(String name, String lname) {
super();
this.name = name;
this.lname = lname;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public void toggleChecked() {
checked = !checked ;
}
}

Android custom search in sectioned listview/baseadapter

Hello guys I want to search my custom/sectioned listview. This is what I've tried so far:
MyAdapter.class:
public class MyAdapter extends BaseAdapter implements SectionIndexer, Filterable {
private ArrayList<String> filteredData = null;
private ItemFilter mFilter = new ItemFilter();
private ArrayList<String> stringArray;
private Context context;
public MyAdapter(Context _context, ArrayList<String> arr) {
stringArray = arr;
filteredData = arr;
context = _context;
}
public int getCount() {
return stringArray.size();
}
public Object getItem(int arg0) {
return stringArray.get(arg0);
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View v, ViewGroup parent) {
LayoutInflater inflate = (( Activity ) context).getLayoutInflater();
View view = (View) inflate.inflate(R.layout.listview_row, null);
LinearLayout header = (LinearLayout) view.findViewById(R.id.section);
String label = stringArray.get(position);
char firstChar = label.toUpperCase().charAt(0);
if (position == 0) {
setSection(header, label);
} else {
String preLabel = stringArray.get(position - 1);
char preFirstChar = preLabel.toUpperCase().charAt(0);
if (firstChar != preFirstChar) {
setSection(header, label);
} else {
header.setVisibility(View.GONE);
}
}
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(label);
return view;
}
private void setSection(LinearLayout header, String label) {
TextView text = new TextView(context);
//#33D633
header.setBackgroundColor(0xff33D633);
text.setTextColor(Color.BLACK);
text.setText(label.substring(0, 1).toUpperCase());
text.setTextSize(20);
text.setPadding(5, 0, 0, 0);
text.setGravity(Gravity.CENTER_VERTICAL);
header.addView(text);
}
public int getPositionForSection(int section) {
if (section == 35) {
return 0;
}
for (int i = 0; i < stringArray.size(); i++) {
String l = stringArray.get(i);
char firstChar = l.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
public int getSectionForPosition(int arg0) {
return 0;
}
public Object[] getSections() {
return null;
}
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<String> list = stringArray;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
MainActivity.class:
String searchString;
MyAdapter adapter;
ArrayList<String> stringList, searchResults;
etSearch.addTextChangedListener(new TextWatcher (){
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
Log.e("TAG", cs.toString());
adapter.getFilter().filter(cs.toString());
}
});
private ArrayList<String> InitListViewData() {
String[] allWords = dbHelper.getAllWords();
stringList = new ArrayList<String>(Arrays.asList(allWords));
return stringList;
}
private void initControls(View rootView) {
etSearch = (EditText) rootView.findViewById(R.id.etSearch);
listView1 = (ListView) rootView.findViewById(R.id.myListView);// list view finding
// Adding data to array lists
listView1.setTextFilterEnabled(true);
dbConnect();
stringList = InitListViewData();
adapter = new MyAdapter(getActivity(), stringList);
listView1.setAdapter(adapter);
SideBar indexBar = (SideBar) rootView.findViewById(R.id.sideBar);
indexBar.setListView(listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
//TextView tvChild = (TextView) v.findViewById(R.id.tvChild);
String selected = parent.getItemAtPosition(pos).toString();
//tvChild.getText().toString().trim();
Toast.makeText(getActivity(), "" + selected, Toast.LENGTH_SHORT).show();
}
});
}
But it's not filtering at all :( I wonder why. I already attached addTextChangedListener to my editText and added getFilter method in my BasedAdapter to no avail. Any ideas? I would gladly appreciate your help. thanks!
you can do custom search like this, pass the string value from editext on text changed method to this method.
public ArrayList<String> matchedValue(String value)
{
ArrayList<String> local=new ArrayList<String>();
for(int i=0;i<Stringarray.size();i++)
{
if(Stringarray.contains(value))
{
local.add(Stringarray.get(i));
}
}
return local;
}
it will return the matched value then update the listview adapter using this returned arraylist.

Categories

Resources