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.
Related
I have a listview getting filtered by an edit text on top.However when i click on the item searched i get the result of another item being selected. there r also some data stored in my array which is not visible on listview , is also returning wrong data.
On searching C you get the result filtered as C.But clicking on it results is like the position does not change for the other data but uses the original ArrayList adapter position.
Any help will be appreciated.
My items:
public class Leave_master_items {
public String leave_id;
public String leave_Name;
public String is_hourly;
public Leave_master_items(String leave_Name) {
this.leave_Name = leave_Name;
}
public String getLeave_id() {
return leave_id;
}
public void setLeave_id(String leave_id) {
this.leave_id = leave_id;
}
public String getLeave_Name() {
return leave_Name;
}
public void setLeave_Name(String leave_Name) {
this.leave_Name = leave_Name;
}
public String getIs_hourly() {
return is_hourly;
}
public void setIs_hourly(String is_hourly) {
this.is_hourly = is_hourly;
}
}
My Adapter:
public class Leave_name_adapter extends BaseAdapter implements Filterable{
ArrayList<Leave_master_items> cm = new ArrayList<>();
Context context;
ArrayList<Leave_master_items> mStringFilterList;
ValueFilter valueFilter;
#Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
// return null;
}
static class ListViewHolder {
TextView leaveType;
}
public Leave_name_adapter(ArrayList<Leave_master_items> cm, Context context) {
this.cm = cm;
this.context = context;
mStringFilterList = cm;
}
#Override
public int getCount() {
return cm.size();
}
#Override
public Object getItem(int position) {
return cm.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ListViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.leaves_listview, parent, false);
viewHolder = new ListViewHolder();
viewHolder.leaveType = (TextView) row.findViewById(R.id.leavestype);
row.setTag(viewHolder);
} else {
viewHolder = (ListViewHolder) row.getTag();
}
Leave_master_items l = (Leave_master_items) getItem(position);
viewHolder.leaveType.setText(l.getLeave_Name());
return row;
}
private class ValueFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// cm.clear();
if (constraint != null && constraint.length() > 0) {
ArrayList<Leave_master_items> filterList = new ArrayList<Leave_master_items>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getLeave_Name().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Leave_master_items country = new Leave_master_items(mStringFilterList.get(i).getLeave_Name());
filterList.add(country);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
cm = (ArrayList<Leave_master_items>) results.values;
notifyDataSetChanged();
}
}
}
My activity:
leave_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Leave_list().execute();
a.setVisibility(View.INVISIBLE);
b.setVisibility(View.VISIBLE);
v.setBackgroundResource(R.drawable.date_button_bg);
date.setBackgroundResource(R.drawable.leave_button_bg);
leave_btn.setTextColor(Color.parseColor("#293038"));
date.setTextColor(Color.parseColor("#F8F8F8"));
search_leave.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Leave_application.this.leave_name_adapter.getFilter().filter(arg0);
}
#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
}
});
}
});
private class Leave_list extends AsyncTask<String, Void, Boolean> {
protected void onPreExecute() {
leave_master_itemses.clear();
}
#Override
protected void onPostExecute(final Boolean success) {
leave_name_adapter=new Leave_name_adapter(leave_master_itemses,getApplicationContext());
listview_leave.setAdapter(leave_name_adapter);
listview_leave.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object a = parent.getAdapter().getItem(position);
String b = (String) parent.getItemAtPosition(position);
Leave_master_items leave_master_items=leave_master_itemses.get(position);
selectedFromList=leave_master_items.getLeave_id();
Log.d("Yourtag", selectedFromList);
// Log.d("Clicked item field", " "+ item.getColum(your colum index));
}
});
}
protected Boolean doInBackground(final String... args) {
try {
Login_json_parser jParser = new Login_json_parser();
String s = session.isWEBURL()+"?Function=" + session.isKEY_USERNAME() + "&Emp=" + session.iskey_emp_id();
Log.d("s", s);
JSONObject json1 = jParser.getJSONFromUrl(s);
Log.d("Inputfdfdtsale Stream", "json1");
try {
JSONObject object = (JSONObject) new JSONTokener(json1.toString()).nextValue();
Leave = object.getString("Leave");
Log.d("ew", Leave);
JSONArray jsonArray = new JSONArray(Leave);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = new JSONObject();
jsonObject=jsonArray.getJSONObject(i);
Leave_master_items leave_master_items = new Leave_master_items(null);
leave_master_items.setIs_hourly(jsonObject.getString("is_hourly"));
leave_master_items.setLeave_id(jsonObject.getString("leave_id"));
leave_master_items.setLeave_Name(jsonObject.getString("leave_Name"));
leave_master_itemses.add(leave_master_items);
}
}
catch(JSONException e){
e.printStackTrace();
}
}
catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
}
You have to ask the adapter to get the right object.
Instead of
Leave_master_items leave_master_items = leave_master_itemses.get(position);
try
Leave_master_items leave_master_items = listview_leave.getAdapter().getItem(position);
UPDATE
When you filter you don't have to create a new object with the same content. Just use the object which already exist.
Instead of
Leave_master_items country = new Leave_master_items(mStringFilterList.get(i).getLeave_Name());
filterList.add(country);
Do
filterList.add(mStringFilterList.get(i));
UPDATE2
Don't return 0 in getItemId()
#Override
public long getItemId(int position) {
return position;
}
The problem here is as soon as you start searching something in edittext, your list content changes and you might be using the old list to get the item.
Create a setter in your adapter class for the list you just got after filtering and use that list in your onItemCLick to fetch the position you want to fetch.
I'm trying to implement a search function in my application. I have used an adapter to because I want to display 2 textviews in listview and listview should be clickable (I don;t think it's possible to do without an adapter).
My problem is I can't use addTextChangedListener in the adapter class. it gives exception (It worked in my activity class).
search activity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results_activity);
new SearchMenuAsyncTask(getApplicationContext(), this).execute();
listtv = (LinearLayout) findViewById(R.id.product_lv);
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"]");
adapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
final List<String> descriptions = new ArrayList<String>();
final List<String> menudescriptions = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
final JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("5")
&& (object.getString("SubCategoryID")).equals("6")
&& (object.getString("Visible")).equals("true")) {
Log.i("descriptionsBev ", object.getString("Description"));
descriptions.add(object.getString("Description"));
Log.i("MenuDescription ",
object.getString("MenuDescription"));
menudescriptions
.add(object.getString("MenuDescription"));
}
adapter = new CustomListSearch(
getApplicationContext(), descriptions,
menudescriptions);
lv.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
CustomListSearch.java
public class CustomListSearch extends BaseAdapter implements Filterable {
private Context context;
private final List<String> descriptions;
private List<String>filteredData = null;
private final List<String> menudescriptions;
private ItemFilter mFilter = new ItemFilter();
private LayoutInflater mInflater;
ArrayAdapter<String> adapter;
public CustomListSearch(Context c, List<String> data,
List<String> menudescriptions) {
this.context = c;
this.filteredData = data ;
this.descriptions = data;
this.menudescriptions = menudescriptions;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return filteredData.size();
}
#Override
public Object getItem(int position) {
return filteredData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
private TextView tvMenudescriptions;
private TextView tvDescriptions;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.search_list_item, parent, false);
holder.tvDescriptions = (TextView) convertView
.findViewById(R.id.product_name);
holder.tvMenudescriptions = (TextView) convertView
.findViewById(R.id.product_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvDescriptions.setText(descriptions.get(position));
holder.tvMenudescriptions.setText(menudescriptions.get(position));
LinearLayout itemlist = (LinearLayout) convertView
.findViewById(R.id.product_lv);
itemlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Still under constructions...",
Toast.LENGTH_LONG).show();
}
});
return convertView;
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
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 = descriptions;
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();
}
}
}
I was following below answer and worked this out
Custom Listview Adapter with filter Android
I have implement search for my application. For that I have used an adapter as well. I have loaded 30 items to the search screen. When I pressed letter z to search, then letter z belongs to 4 items out of 30 items in the list.
According to the below shown code it identifies the number 4 and shows the first 4 items out of the 30 items. When I debug inside getFilter(), it shows the filtered items correctly(I have provided a screen shot).
My problem is how can I load the filtered items. Bit confused here.
search.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results_activity);
new SearchMenuAsyncTask(getApplicationContext(), this).execute();
listtv = (LinearLayout) findViewById(R.id.product_lv);
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
lv.setTextFilterEnabled(true);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"]");
adapter.getFilter().filter(s.toString());
TextView headerTV = (TextView) findViewById(R.id.search_header);
headerTV.setText("SEARCH RESULTS");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
final List<String> menudescriptions = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
final JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")
&& (object.getString("Visible")).equals("true")) {
Log.i("descriptionsTop ", object.getString("Description"));
descriptions.add(object.getString("Description"));
Log.i("MenuDescription ",
object.getString("MenuDescription"));
menudescriptions
.add(object.getString("MenuDescription"));
}
adapter = new CustomListSearch(
getApplicationContext(), descriptions,
menudescriptions);
lv.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
CustomListSearch.java
public class CustomListSearch extends BaseAdapter implements Filterable {
private Context context;
List<String> descriptions;
private List<String>filteredDescriptions;
private final List<String> menudescriptions;
private ItemFilter mFilter = new ItemFilter();
private LayoutInflater mInflater;
ArrayAdapter<String> adapter;
public CustomListSearch(Context c, List<String> data,
List<String> menudescriptions) {
this.context = c;
this.filteredDescriptions = data ;
this.descriptions = data;
this.menudescriptions = menudescriptions;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return filteredDescriptions.size();
}
#Override
public Object getItem(int position) {
return filteredDescriptions.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
private TextView tvMenudescriptions;
private TextView tvDescriptions;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.search_list_item, parent, false);
holder.tvDescriptions = (TextView) convertView
.findViewById(R.id.product_name);
holder.tvMenudescriptions = (TextView) convertView
.findViewById(R.id.product_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvDescriptions.setText(descriptions.get(position));
holder.tvMenudescriptions.setText(menudescriptions.get(position));
LinearLayout itemlist = (LinearLayout) convertView
.findViewById(R.id.product_lv);
itemlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Still under constructions...",
Toast.LENGTH_LONG).show();
}
});
return convertView;
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
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 = descriptions;
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) {
filteredDescriptions = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
Screen shot
To filter adapter that contains list of custom object
Create a Object:
public class TvObject {
String name;
String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
After:
public class CustomListSearch extends BaseAdapter implements Filterable {
private Context context;
private ItemFilter mFilter = new ItemFilter();
private LayoutInflater mInflater;
private List<TvObject> mListTvObject = new ArrayList<>();
private List<TvObject> mListTvObjectFiltered = new ArrayList<>();
public CustomListSearch(Context c, List<TvObject> mListTvObject) {
this.context = c;
mInflater = LayoutInflater.from(context);
this.mListTvObject = mListTvObject;
this.mListTvObjectFiltered = mListTvObject;
}
#Override
public int getCount() {
return mListTvObjectFiltered.size();
}
#Override
public Object getItem(int position) {
return mListTvObjectFiltered.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
private TextView tvMenudescriptions;
private TextView tvDescriptions;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.search_list_item, parent, false);
holder.tvDescriptions = (TextView) convertView.findViewById(R.id.product_name);
holder.tvMenudescriptions = (TextView) convertView.findViewById(R.id.product_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvDescriptions.setText(mListTvObjectFiltered.get(position).getName());
holder.tvMenudescriptions.setText(mListTvObjectFiltered.get(position).getDescription());
LinearLayout itemlist = (LinearLayout) convertView.findViewById(R.id.product_lv);
itemlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Still under constructions...",Toast.LENGTH_LONG).show();
}
});
return convertView;
}
#Override
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();
int count = mListTvObject.size();
final ArrayList<TvObject> mListResult = new ArrayList<>();
String name;
for (int i = 0; i < count; i++) {
TvObject mTvObject = mListTvObject.get(i);
name = mTvObject.getName();
if (name.toLowerCase().contains(filterString)) {
mListResult.add(mTvObject);
}
}
results.values = mListResult;
results.count = mListResult.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
mListTvObjectFiltered = (ArrayList<TvObject>) results.values;
notifyDataSetChanged();
}
}
}
Edited:
In your Activity do something like this!
try {
final List<TvObject > mListObjects = new ArrayList<>();
for (int i = 0; i < responseJson.length(); ++i) {
final JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")
&& (object.getString("Visible")).equals("true")) {
Log.i("descriptionsTop ", object.getString("Description"));
Log.i("MenuDescription ", object.getString("MenuDescription"));
TvObject mTvObject = new TvObject();
mTvObject.setName(object.getString("Description"));
mTvObject.setDescription(object.getString("MenuDescription"));
mListObjects.add(mTvObject);
}
adapter = new CustomListSearch( getApplicationContext(), mListObjects);
lv.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
I have a Listview in my asynctask and i am setting the adapter. I should implement the autosearch complete feature. My codes works fine but the list is not filtered when i enter the text in editable textview.
#Override
protected void onPostExecute(ArrayList<RouteTrackerUser> result)
{
dialog.dismiss();
if(result!=null)
{
//System.out.println("User with result===>"+result);
/*for(RouteTrackerUser r :result ){
System.out.println("Contents===>"+r.getStoreid()+" "+r.getStorename()+" "+r.getStorecode()+" "+r.getStoreno()+" "+r.getVisitcount());}
*/ Intent intent=getIntent();
TextView username=(TextView)findViewById(R.id.usrname);
final String sname=intent.getExtras().getString("user_name");
username.setText("Welcome, "+sname);
userList = (ListView) findViewById(R.id.historylist);
userAdapter = new HistoryAdapter(History.this, R.layout.history_visits,R.id.vcount,result);
userList.setAdapter(userAdapter);
EditText inputSearch = (EditText) findViewById(R.id.autocomplete);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
System.out.println("afafasdf"+cs);
History.this.userAdapter.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
}
});
}
and my customAdapter is as follows :
private static class ViewHolder {
public final TextView storename;
public final TextView storevisitcount;
public ViewHolder(TextView name, TextView count) {
this.storename = name;
this.storevisitcount = count;
}
}
public class HistoryAdapter extends ArrayAdapter<RouteTrackerUser>
{
Context context;
//EditText autocomplete;
public HistoryAdapter(Context context,int resource,int textViewResourceId,ArrayList<RouteTrackerUser> result) {
super(context,resource,textViewResourceId, result);
this.context=context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView name;
TextView vcount;
final RouteTrackerUser User = getItem(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.history_visits, parent, false);
name = (TextView) convertView.findViewById(R.id.sname);
vcount = (TextView) convertView.findViewById(R.id.vcount);
convertView.setTag(new ViewHolder(name, vcount));
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewdata(User.getUsername(),User.getUid(),User.getStoreid(),User.getStorename(),User.getStorecode(),User.getStoreno(),User.getVisitcount());
}});
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
name = viewHolder.storename;
vcount = viewHolder.storevisitcount;
}
name.setText(User.getStorename());
vcount.setText("Visit Count: "+User.getVisitcount());
return convertView;
}
}
Note: My main class name is History.java and Asynctask name is HistorySoapTask.
After searching few blogs and other posts i found the answer for my question. I made a mistake, i doesn't implement the Filter concept in my program and now it works fine. So to filter the list we should define the filter as below :
private class OrderAdapterFilter extends Filter
{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterText = (String) constraint;
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<RouteTrackerUser> filt = new ArrayList<RouteTrackerUser>();
ArrayList<RouteTrackerUser> lItems = new ArrayList<RouteTrackerUser>(fullitems);
for(int i = 0, l = lItems.size(); i < l; i++)
{
RouteTrackerUser m = lItems.get(i);
if(m.getStorename().toLowerCase().contains(constraint))
filt.add(m);
}
result.count = filt.size();
result.values = filt;
}
else
{
ArrayList<RouteTrackerUser> list = new ArrayList<RouteTrackerUser>(fullitems);
result.values = list;
result.count = list.size();
}
return result;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// NOTE: this function is *always* called from the UI thread.
filtered = (ArrayList<RouteTrackerUser>)results.values;
originalitems.clear();
for(int i = 0, l = filtered.size(); i < l; i++)
originalitems.add(filtered.get(i));
notifyDataSetChanged();
}
}
Here is the code, I used a custom adapter, and applied the Filter method, now the ListView does not update when the backspace key is entered, on when one switches back from an item activity. I read it is a bug in filter, but how do I bypass it, I know of apps that have achieved this kind of setup I want.
public class IndexPageActivity extends Activity implements OnItemClickListener{
ListView listView;
EditText editTextB;
PagesAdapter adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
adapter1 = new PagesAdapter(this);
listView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(this);
editTextB.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
IndexPageActivity.this.adapter1.getFilter().filter(cs.toString());
adapter1.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) { }
#Override
public void afterTextChanged(Editable arg0) { }
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
Intent i;
String name = adapter1.getItem(position);
Log.d("id", name);
if (name.equals("Item1"))
{
i = new Intent(this, anActivity.class);
startActivity(i);
}
else if (name.equals("Item2"))
{
i = new Intent(this, anActivity2.class);
startActivity(i);
}
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements Filterable{
ArrayList<String> pagelist;
List<String> arrayList;
Context context;
String [] pagedatas;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<String>();
Resources res = c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(pagedatas[i]);
}
}
#Override
public int getCount() {
return pagelist.size();
}
#Override
public String getItem(int i) {
return pagelist.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewG) {
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
String temp=pagelist.get(i);
pagetitle.setText(temp);
return row;
}
public class filter_here extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if(constraint.length() == 0 ){
Result.values = pagelist;
Result.count = pagelist.size();
return Result;
}
ArrayList<String> Filtered_Names = new ArrayList<String>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<pagelist.size(); i++){
filterableString = pagelist.get(i);
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
return Result;
}
#Override
protected void publishResults(CharSequence constraint,FilterResults results) {
pagelist = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
#Override
public Filter getFilter() {
return new filter_here();
}
}
1) Backspace Filtering
In your publishResults, you are modifying your pagelist, so if you press backspace and filter again, pagelist only contains a small portion of the original pagelist entries:
So, you need to keep a copy of your original data! Something like:
List<String> allData;
PagesAdapter(Context c) {
...
allData = pagelist;
...
}
and in your filter method, use the allData object:
for (int i = 0; i < allData.size(); i++) {
filterableString = allData.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
Filtered_Names.add(filterableString);
}
}
and also where you reset your data back to the original:
// if constraint is empty return the original names
if (constraint.length() == 0) {
Result.values = allData;
Result.count = allData.size();
return Result;
}
2) Clear filter after returning from another activity:
This can be done in onResume... Personally, i would clear the text in the editText as to reset the filter like so:
#Override
protected void onResume() {
if (editTextB != null) {
editTextB.setText("");
}
super.onResume();
}
Finally found the bug of the backspace, you had some error in the filter function, that am returning an ArrayList, instead of the List that holds the variable the code should the final code is:
public class IndexPageActivity extends Activity implements OnItemClickListener{
ListView listView;
EditText editTextB;
PagesAdapter adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
adapter1 = new PagesAdapter(this);
listView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(this);
editTextB.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
IndexPageActivity.this.adapter1.getFilter().filter(cs.toString());
adapter1.notifyDataSetChanged();
}
#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
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
Intent i;
String name = adapter1.getItem(position);
Log.d("id", name);
if (name.equals("Item1"))
{
i = new Intent(this, anActivity.class);
startActivity(i);
}
else if (name.equals("Item2"))
{
i = new Intent(this, anActivity2.class);
startActivity(i);
}
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements Filterable{
ArrayList<String> pagelist;
List<String> arrayList;
Context context;
String [] pagedatas;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<String>();
Resources res = c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(pagedatas[i]);
}
arrayList = pagelist;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public String getItem(int i) {
// TODO Auto-generated method stub
return arrayList.get(i);
}
#Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
String temp=arrayList.get(i);
pagetitle.setText(temp);
return row;
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arrayList = (List<String>) results.values;
notifyDataSetChanged();
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<String> FilteredArrayNames = new ArrayList<String>();
if (pagelist == null) {
pagelist = new ArrayList<String>(arrayList);
}
if (constraint == null || constraint.length() == 0) {
results.count = pagelist.size();
results.values = pagelist;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < pagelist.size(); i++) {
String dataNames = pagelist.get(i);
if (dataNames.toLowerCase().startsWith(constraint.toString())) {
FilteredArrayNames.add(dataNames);
}
}
results.count = FilteredArrayNames.size();
System.out.println(results.count);
results.values = FilteredArrayNames;
Log.e("VALUES", results.values.toString());
}
return results;
}
};
return filter;
}
}
How about putting
adapter1.notifyDataSetChanged();
in onResume() method of the Activity?