So i'm trying to make a searchable list view using a custom array adapter.
I finally got the search working but the results are not exactly accurate.
Its actually a hit and miss kinda thing e.g Searching for Drupal gets me Google Plus, you knw what I saying?
So here's my adapter
public class CustomListAdapter extends ArrayAdapter<String> implements Filterable {
private final Activity context;
private final String[] web;
private final String[] imageIdUrl;
public CustomListAdapter(Activity context, String[] web, String[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageIdUrl = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
//find row view
View rowView= inflater.inflate(R.layout.list_single, null, true);
//find text
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
//find image
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
//set text
txtTitle.setText(web[position]);
//set image traditional
//imageView.setImageResource(imageIdUrl[position]);
//set image with picasso
Picasso.with(this.context).load(imageIdUrl[position]).into(imageView);
//return row
return rowView;
}
}
And heres my Main Activity class
public class MainActivity extends ActionBarActivity {
ListView list;
EditText editsearch;
String[] web = {
"Google Plus",
"Twitter",
"Windows",
"Bing",
"Itunes",
"Wordpress",
"Drupal"} ;
String[] imageIdUrl = {
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg" ,
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CustomListAdapter adapter = new CustomListAdapter(MainActivity.this, web, imageIdUrl);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
}
});
// Locate the EditText in listview_main.xml
editsearch = (EditText) findViewById(R.id.editText);
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// 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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I'll be grately thankful for any help. This is proving to be a major headache
Change the code in onTextChanged() method to afterTextChanged() method as below:
@Override
public void afterTextChanged(Editable s) {
String str=s.toString();
getSearchItems(str);
}
In getSearchItems use the filter properly. I used other way to filter as,
public void getSearchItems(String searchText){
List<Employee> searchList=new ArrayList<Employee>();
List<Employee> employeeList=getEmployeeList();
if(employeeList2!=null){
for(Employee e:employeeList){
if(e.name.toLowerCase().contains(searchText.toLowerCase())){
searchList.add(e);
}
}
}
adapter.refresh(searchList);
}
Then In adapter use another method refresh() as below:
public void refresh(List<Employee> employeeList){
this.employeeList=employeeList;
notifyDataSetChanged();
}
Actually I used Employee class which contains his name and address, and I made search using employee name. So u change the above code which suits your search.