i am using the ListView
method to search in a custom listview
. I have made an image and textview
as elements to search. However, the edittext
in which i used as the search bar cant properly filter my items but when tested using only text it works. Is there a way that my codes could be corrected? it doesn't show any errors. The Image Shown shows that when i try to filter T it doesn't show temasek ;instead it shows only the first two elements.
MainActivity.java:
public class MainActivity extends Activity {
String[] schools = { "Elias Park Primary",
"Anderson Secondary",
"Temasek JC",
"Republic Poly",
"Nanyang Poly" ,
"Meridian JC",
"Temasek Poly",
"Ngee Ann Poly",
"Anglican High"
};
Integer[] imageId = {
R.drawable.eliaspark_pri,
R.drawable.anderson_sec,
R.drawable.temasek_jc,
R.drawable.republic_poly,
R.drawable.nyp,
R.drawable.mjc,
R.drawable.tp,
R.drawable.ngee_ann,
R.drawable.anglican_high
};
ListView list;
custom adapter1;
EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter1 = new custom(MainActivity.this, schools, imageId);
list.setAdapter(adapter1);
inputSearch.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
Log.i("Char Sqequence", ""+s);
MainActivity.this.adapter1.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
Custom Listview :
public class custom extends ArrayAdapter<String>{
private final Activity context ;
private final String[] schools ;
private final Integer[] imageId;
public custom(Activity context, String[] schools, Integer[] imageId){
super(context, R.layout.list1,schools);
this.context = context;
this.schools = schools;
this.imageId = imageId;
}
public View getView(int position, View view, ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list1,null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(schools[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}}
XML Listview :
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
Custom ListView :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="match_parent"
/>