How to Implement Autocompletetextview with Listview

How to implement autocompletetextview with listview?

First create a custom adapter implements filterable:

public class MyFilterableAdapter extends BaseAdapter implements Filterable {
private Context context;
private List<String> items;
private List<String> filteredItems;
private ItemFilter mFilter = new ItemFilter();

public MyFilterableAdapter(Context context, List<String> items) {
//super(context, R.layout.your_row, items);
this.context = context;
this.items = items;
this.filteredItems = items;
}

@Override
public int getCount() {
return filteredItems.size();
}

@Override
public Object getItem(int position) {
return filteredItems.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(R.layout.row_search_merchant, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}

String location = filteredItems.get(position);
if (!location.isEmpty() || viewHolder != null) {
viewHolder.tvTitle.setText(location);
}
return convertView;
}

public static class ViewHolder {
TextView tvTitle;
}

private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();

int count = items.size();
final List<String> tempItems = new ArrayList<>(count);

for (int i = 0; i < count; i++) {
if (items.get(i).toLowerCase().contains(filterString)) {
tempItems.add(items.get(i));
}
}

results.values = tempItems;
results.count = tempItems.size();

return results;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredItems = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}

public Filter getFilter() {
return mFilter;
}
}

And than create a textwatcher

public class MyTextWatcher implements TextWatcher {
private MyCustomAdapter lAdapter;

public MyTextWatcher(MyCustomAdapter lAdapter) {
this.lAdapter = lAdapter;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
lAdapter.getFilter().filter(s.toString().toLowerCase());
}
}

And finally add your TextWatcher to your Search Edit Text:

final MyCustomAdapter lAdapter = new MyCustomAdapter(context, items);
EditText etSearch = (EditText) view.findViewById(R.id.et_search);
etSearch.addTextChangedListener(new SearchTextWatcher(lAdapter));

I hope this'll help you.

Identify AutocompleteTextView in ListView Item

I found a workaround to get reference of AutoCompleteTextView in listview.

holder.productCodeView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
currentFocusView = v;
}

}
});

holder.productCodeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {

if (currentFocusView != null) {
int iPosition = (Integer) currentFocusView.getTag();

}

}
});

Android AutoCompleteTextView as ListView header

This question regarding focus of EditText views within a ListView & some reading of the AutoCompleteTextView source helped me find the answer. Setting the order of focus on the ListView to afterDescendants allowed the dialog to be shown normally.

<ListView
android:id="@+id/ResultList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dip"
android:descendantFocusability="afterDescendants"
android:fadingEdge="none" >
</ListView>

Android AutoCompleteTextView result in List instead of default dropdown

i can give you the logic. check out this link.
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and this too http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html

and filteration is done in adapter so first bind listview with your empty adapter and in textWatcher bindlistview with datasource filteration and notifie for changes.

Add items to listview dynamically from autocompletetextview

You need to call notifyDataSetChanged() on your related ListView's adapter after adding item to Boodschappenlst. This method should do what you need:

public void AutocmpleteMeth() {
// Hieronder is het code voor Autocomplete [BEGIN]
final AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_dropdown_item_1line, lstProduct);
ACTV.setAdapter(adapter);
ACTV.setThreshold(1);

ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView < ? > arg0, View arg1, int position, long arg3) {

String selectedItem = (String) arg0.getItemAtPosition(position);
Boodschappenlst.add(selectedItem);
// Call notifyDatasetChanged() here on the related ListView's adapter here to recognise new item change
ACTV.setText("");
}
//Autocmplete [END]
});
}


Related Topics



Leave a reply



Submit