0

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"
    />

clement
  • 1
  • 2
  • So sorry the picture cant be post because i dun have 10 reputation but just to explain that as i search for a letter it doesn't filter. – clement Sep 29 '15 at 11:13
  • In your `custom` class, you will have to override the `getFilter()` method and create a filter which will filter the list based on your criteria. – Titus Sep 29 '15 at 11:25
  • Here is an example on how to do that: http://stackoverflow.com/a/19301216/1552587 – Titus Sep 29 '15 at 11:26
  • hey thanks for the help. ya i saw that post but i still dun get it. This was the closes i can go. Any way that it could be the removal or arrangement of codes? – clement Sep 29 '15 at 11:45
  • The problem is that the default filter doesn't know how to interpret the data, you will have to create your own filter. – Titus Sep 29 '15 at 11:55
  • ah i see so what i need to do is to find out about this filter method and then implement it? – clement Sep 29 '15 at 12:02

0 Answers0