user7031762 user7031762 - 1 month ago 11
Java Question

listview search function + and open activity

I have a problem that my search function doesn't work. I want to make a search function on list view that when I type some text the items are filtered, and when I click on some item it goes to another activity.

My code:

public class Search extends AppCompatActivity {
private EditText search_box;
private ArrayAdapter < String > listAdapter;
ArrayList < HashMap < String, String >> productList;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
search_box = (EditText) findViewById(R.id.search_box);
final ListView list_view = (ListView) findViewById(R.id.list_view);
String products[] = getResources().getStringArray(R.array.search);
listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, products);
list_view.setAdapter(listAdapter);
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView < ? > adapterView, View view, int position, long id) {
String item = list_view.getItemAtPosition(position).toString();
TextView subject_name = (TextView) view.findViewById(R.id.subject_name);
String sText = subject_name.getText().toString().trim();
int resID = getResources().getIdentifier(String.valueOf(subject_name), "string", getPackageName());
String subject = getResources().getString(resID);
if (item.equals(subject)) {
String StringClass = "com.shady.letsdoit." + String.valueOf(item);
Class < ? > c = null;
if (StringClass != null) {
try {
c = Class.forName(StringClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(Search.this, c);
startActivity(intent);
}
}
});
search_box.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
Search.this.listAdapter.getFilter().filter(cs);
}
@Override public void afterTextChanged(Editable editable) {}
});
}

Answer

For your searching in the listview you can call the method::

private void setFilter(CharSequence cs) {
    String filterProducts[] = new String[]{};
    if (!TextUtils.isEmpty(cs)) {
        int j = 0;
        for (int i = 0; i < products.length; i++) {
            if (products[i].contains(cs)) {
                filterProducts[j] = cs.toString();
                j++;
            }
        }
        listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, filterProducts);
        list_view.setAdapter(listAdapter);

    }else{
        listAdapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.subject_name, products);
        list_view.setAdapter(listAdapter);
    }
}

Call it from here ::

 @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
          setFilter(cs);
 }

For that redirecting to next activity..You have to explain what you want to do and what is the problem you facing..because your code is not that much clear indication about it.