Reputation: 445
Heyy guys I hav list which is showing list of cuisine.now to sort those list items I hav defined EditText so as the user type within this edittext the list items get filtered by TextWatcher.But the problem is that the listview gives me wrong items.as I type first character it gives me 1st 6 items of the list as it is,when I type 2nd char it gives me 1st item of the list even if those character are not contained in those items strings.Whats is the mistake?
public class test extends Activity {
EditText editsearch;
LinearLayout Layout;
String temp;
private ListView listview;
private DisplayMetrics metrics;
String[] values = new String[] { "American","Algerian ", "Andhra", "Anhui", "Arabian", "Asian",
"Assamese", "Awadhi", "Bakery", "Beijing", "Bengali",
"Beverages" , "Bhojpuri", "Biryani", "Cafe", "Cameroonian",
"canadian", "Cantonese", "Centrafrican", "Chinese", "Congolese",
"Continental", "Desserts", "EastAfrican", "Egyptian", "European",
"Fast food", "Finger food", "French", "Fujian", "Gaon", "German",
"Ghanaian", "Greek", "Gujrati", "Hakka ", "Healthy food", "HongKong",
"Hyderabadi", "Icecream", "Indian", "Indonesian", "Italian", "Ivorian",
"Jain ", "Japanese", "Juices", "Kashmiri", "Kenyan", "Kerala",
"Konkan", "Lucknowi", "Maasai", "Maharashtrian", "Malaysian",
"Malwani", "Mangalorean", "mexican", "Middleeastern", "Nepalese",
"Northeastern", "NorthIndian", "Oriya", "Parsi", "Pizza", "Punjabi ",
"Rajasthani ", "Rajasthani", "Sea food", "Shanghai", "Sushi", "Thai",
"Tibetan", "Tunisian ", "Udupi", "Zhejiang", "Zimbabwean "
};
private int mode = 1;
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
overridePendingTransition (R.anim.slide_in_left, R.anim.slide_out_right);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.cusinetype_activity);
setContentView(R.layout.cusinetype_activity);
//Layout.setBackgroundResource(R.drawable.cusine_new);
//setContentView(Layout); //you might be forgetting this
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
listview = new ListView(this);
listview = (ListView) findViewById(R.id.listView1);
listview.setFadingEdgeLength(0);
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0;i<values.length; i++) {
temp=values[i];
strings.add(temp);
}
final MainAdapter mAdapter = new MainAdapter(this, strings, metrics);
listview.setAdapter(mAdapter);
//enables filtering for the contents of the given ListView
listview.setTextFilterEnabled(true);
//Pass results to ListViewAdapter Class
EditText myFilter = (EditText) findViewById(R.id.editText1);
myFilter.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mAdapter.getFilter().filter(s);
mAdapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
//listview.setBackgroundResource(R.drawable.cusine_new);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listview.getItemAtPosition(position);
Intent in = new Intent(getApplicationContext(),nearby_mainactivity3.class);
in.putExtra("Value",itemValue );
startActivity(in);
// Show Alert
// Toast.makeText(getApplicationContext(),
// "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
// . show();
}
});
}
public class MainAdapter extends ArrayAdapter<String> {
private Context context;
private LayoutInflater mInflater;
private ArrayList<String> strings;
private DisplayMetrics metrics_;
private class Holder {
public TextView textview;
}
public MainAdapter(Context context, ArrayList<String> strings,
DisplayMetrics metrics) {
super(context, 0, strings);
this.context = context;
this.mInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.strings = strings;
this.metrics_ = metrics;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final String str = this.strings.get(position);
Log.d("timepass", str);
final Holder holder;
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.list_item2,null);
holder = new Holder();
holder.textview = (TextView) convertView
.findViewById(R.id.name);
holder.textview.setTextColor(0xFFFFFFFF);
// holder.textview.setBackgroundResource(R.drawable.back);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.textview.setText(str);
Animation animation = null;
animation = AnimationUtils.loadAnimation(context, R.anim.slide_in_top);
animation.setDuration(800);
convertView.startAnimation(animation);
animation = null;
return convertView;
}
}
}
Upvotes: 0
Views: 415
Reputation: 53
Please go through this link this will help you in sorting out your problem.
The API is provided to give you the direct functionality to filter data yourself. I guess system wont do it for you.
Upvotes: 1