0

Hello I am trying to implement the search functionality. I have a SearchResultActivity class that extends ListActivity. I have also edited my AndroidManifest file to indicate a new activity. I have two different arrays, with different lengths and more are still coming. I have been surfing the net on how to retrieve the data from ListView but I haven't got the search function running. Please review all my codes below and let me know what I need to do or probably doing wrong. Still very new developer on Android and Java. So a nice and easy solution would be appreciated. Thanks.

public class SearchResultActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);
    }
}
private void showResults(String query) {
    /*SearchResultActivity.this.adapterBike.getFilter().filter(query);*/
    //SearchResultActivity.this.adapterCar.getFilter().filter(query);
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
}

@Override
public void setListAdapter(ListAdapter adapter) {
    super.setListAdapter(adapter);

}
}
public class HomePage extends AppCompatActivity {     
ListView allLists;    
SearchView searchView;    
CarSublist carSublist;
BikeSublist bikeSublist;
ArrayAdapter adapterBike;
ArrayAdapter adapterCar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    adapterBike = ArrayAdapter.createFromResource(this, R.array.BikeList, android.R.layout.simple_list_item_1);
    adapterCar = ArrayAdapter.createFromResource(this, R.array.CarList, android.R.layout.simple_list_item_1);        
    allLists = (ListView)findViewById(R.id.all_list);

}   

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home_page, menu);
    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(
            new ComponentName(getApplicationContext(), SearchResultActivity.class)));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        //decided to work on the firstArray
        @Override
        public boolean onQueryTextSubmit(String query) {
           String[] bikeArray= getResources().getStringArray(R.array.BikeList);
            String[] carArray = getResources().getStringArray(R.array.CarList);

            for (String data : bikeArray){
                if (data.equals(query) ){
                    //do Something
                    bikeSublist = new BikeSublist();
                    bikeSublist.bindData();
                }else{
                 break;
                }
            }

            HomePage.this.adapterBike.getFilter().filter(query);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //decided to work on the firstArray
            String[] bikeArray= getResources().getStringArray(R.array.BikeList);
            for (String data : bikeArray){
                if (data.equals(newText) ){
                    HomePage.this.adapterBike.getFilter().filter(newText);
                    bikeSublist = new BikeSublist();
                    bikeSublist.bindData();
                }else{
                    break;
                }
            }
            return true;

        }
    });

    return true;
}   
}

2 Answers 2

1

You can Create your own Filter for searching in adapter class like below

private List<WorldPopulation> worldpopulationlist = null;
private ArrayList<WorldPopulation> arraylist;

        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            worldpopulationlist.clear();
            if (charText.length() == 0) {
                worldpopulationlist.addAll(arraylist);
            } 
            else 
            {
                for (WorldPopulation wp : arraylist) 
                {
                    if (wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText)) 
                    {
                        worldpopulationlist.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }

and apply it on your edittext:

// Capture Text in EditText
        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString().toLowerCase();
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
        });
    }

Here is link for the same

http://www.androidbegin.com/tutorial/android-search-listview-using-filter/

Sign up to request clarification or add additional context in comments.

2 Comments

No it did not. I have seen this tutorial and tried it before asking my question. But I am very grateful for your response. I was able to implement some of your codes and used it on mine. I will try and post my result when I ma done with it. Thanks Anjali
let me know what an issue that you are facing
0

Yeah this is what I did. And it might be needful for someone later( I guess). On my fragment, I wrote this line of codes

public void bindData(String strSearchText) {
    Object[] arrayItems = getResources().getStringArray(R.array.BikeList);
    if (strSearchText != null && strSearchText.length() > 0) {
        List<Object> lstFilteredItems = new ArrayList<>();
        for (Object item : arrayItems) {
            if (((String)item).contains(strSearchText)) {
                lstFilteredItems.add(item);
            }
        }
        arrayItems = lstFilteredItems.toArray();
    }
    ArrayAdapter<Object> adapter = new ArrayAdapter<>(getActivity(),R.layout.fragment_sub_list,R.id.topText,arrayItems);
    //ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.BikeList, android.R.layout.simple_list_item_1);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);
}

and then on my activity page, this what I did.

public boolean onQueryTextChange(String strSearchText) {

    myFragment.bindData(strSearchText);
    return true;
}

Any idea on how to implement this in multiple fragment is appreciated. Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.