Hi,
i have a edittext and a listview ... I want to do this....when the users
type on a edittext this will filter a listview....
But...the problem is the listview returns nothing when I type someword that
i have in the list.
Can anyone help me ? or there any another simple way to do this....
Here is my code...
public class Restaurants extends Activity {
private ListView restaurantListView;
private ArrayList<RestaurantsInfo> mRestaurants;
private ListAdapter lstAdapter;
EditText edt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.restaurants);
mRestaurants = getRestaurants();
restaurantListView = (ListView)
findViewById(R.id.lvRestaurant);
lstAdapter = new ListAdapter(this,
R.id.lvRestaurant, mRestaurants);
restaurantListView.setAdapter(lstAdapter);
restaurantListView.setTextFilterEnabled(true);
restaurantListView.setOnItemClickListener(new
ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0,
View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent its = new
Intent(getBaseContext(),RestaurantsDetails.class);
Bundle b = new Bundle();
b.putLong("key",arg3);
its.putExtras(b);
startActivity(its);
}
});
edt = (EditText)findViewById(R.id.editText);
edt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged( CharSequence
arg0, int arg1, int arg2, int arg3)
{ // TODO Auto-generated method
stub
Restaurants.this.lstAdapter.getFilter().filter(arg0);
}
@Override
public void afterTextChanged(Editable arg0)
{ // TODO Auto-generated method
stub
}
@Override
public void beforeTextChanged(CharSequence
arg0, int arg1,
int arg2, int arg3)
{
// TODO Auto-generated
method stub
}
});
} catch (Exception e) {
// TODO: handle exception
Log.e("oncreate resta",e.getMessage());
}
}
public ArrayList<RestaurantsInfo> getRestaurants(){
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
try {
dbAdapter.createDataBase();
} catch (IOException e) {
Log.i("*** select ",e.getMessage());
}
dbAdapter.openDataBase();
String query="SELECT * FROM restaurants;";
ArrayList<ArrayList<String>> stringList =
dbAdapter.selectRecordsFromDBList(query, null);
dbAdapter.close();
ArrayList<RestaurantsInfo> restaurantsList = new
ArrayList<RestaurantsInfo>();
for (int i = 0; i < stringList.size(); i++) {
ArrayList<String> list = stringList.get(i);
RestaurantsInfo restaurant = new RestaurantsInfo();
try {
restaurant.id =
Integer.parseInt(list.get(0));
restaurant.res_name = list.get(1);
restaurant.res_city = list.get(2);
} catch (Exception e) {
Log.i("***" + Restaurants.class.toString(),
e.getMessage());
}
restaurantsList.add(restaurant);
}
return restaurantsList;
}
private class ListAdapter extends ArrayAdapter<RestaurantsInfo>{
private ArrayList<RestaurantsInfo> mList;
private Context mContext;
public ListAdapter(Context context, int
textViewResourceId,ArrayList<RestaurantsInfo> list) { //
--CloneChangeRequired
super(context, textViewResourceId, list);
this.mList = list;
this.mContext = context;
}
public View getView(int position, View convertView,
ViewGroup parent){
View view = convertView;
try{
if (view == null) {
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view =
vi.inflate(R.layout.restaurant_row, null); //
--CloneChangeRequired(list_item)
}
final RestaurantsInfo listItem =
mList.get(position); // --CloneChangeRequired
if (listItem != null) {
// setting list_item views
( (TextView)
view.findViewById(R.id._id) ).setText( listItem.getId()+"");
( (TextView)
view.findViewById(R.id.res_name) ).setText( listItem.getRes_name());
( (TextView)
view.findViewById(R.id.res_city) ).setText( listItem.getRes_city()+"" );
}}catch(Exception e){
Log.i(Restaurants.ListAdapter.class.toString(), e.getMessage());
}
return view;
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en