2

I'm a beginner in android programming and I have a problem I can't seem to find a solution for. I searched stackoverflow for hours but I still don't quite get it how should I do this stuff:

I created a slim api with php which communicates with a local xampp mysql server, and I can do basic CRUD operations on it. I GET the data with the api from the server, which returns all "items" in the database to a List, and I can use this in a listView. Every list item has 6 TextViews so I created a custom ArrayAdapter but I don't know to search for specific list items.

My Item class:

  public class Item {

        private String mName;
        private String mQuantity;
        private String mDate_added;
        private String mBarcode;
        private String mPrice;
        private String mWarranty;
        private int mId;

        public Item(String mName, String mQuantity, String mDate_added, String mBarcode, String mPrice, String mWarranty,int mId) {
            this.mName = mName;
            this.mQuantity = mQuantity;
            this.mDate_added = mDate_added;
            this.mBarcode = mBarcode;
            this.mPrice = mPrice;
            this.mWarranty = mWarranty;
            this.mId = mId;
        }

        public int getmId(){return mId; }

        public String getmName() {
            return mName;
        }

        public String getmQuantity() {
            return mQuantity;
        }

        public String getmDate_added() {
            return mDate_added;
        }

        public String getmBarcode() {
            return mBarcode;
        }

        public String getmPrice() {
            return mPrice;
        }

        public String getmWarranty() {
            return mWarranty;
        }
    }

My adapter

public class ItemAdapter extends ArrayAdapter<Item>  {


    public ItemAdapter(Context context, ArrayList<Item> items) {
        super(context,0, items);

    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View ListItemView= convertView;
        if (ListItemView == null) {



            ListItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);

           Item currentItem = getItem(position);


            TextView nameText = (TextView) ListItemView.findViewById(R.id.name);
            TextView quantityText = (TextView) ListItemView.findViewById(R.id.quantity);
            TextView date_addedText = (TextView) ListItemView.findViewById(R.id.date_added);
            TextView barcodeText = (TextView) ListItemView.findViewById(R.id.barcode);
            TextView priceText = (TextView) ListItemView.findViewById(R.id.price);
            TextView warrantyText = (TextView) ListItemView.findViewById(R.id.warranty);


            nameText.setText(currentItem.getmName());
            quantityText.setText(currentItem.getmQuantity() +" e");
            date_addedText.setText(currentItem.getmDate_added());
            barcodeText.setText(currentItem.getmBarcode());
            priceText.setText(currentItem.getmPrice()+ " pr/e");
            warrantyText.setText(currentItem.getmWarranty());

        }

        return ListItemView;
    }

}

I get the data with httpurlconnection get method, and I use an AsyncTaskLoader for this. this returns the data to a List from a given url.

public class ItemLoader extends AsyncTaskLoader<List<Item>> {

    private String mUrl;

    public ItemLoader(Context context, String url) {
        super(context);
        mUrl = url;
    }


    @Override
    protected void onStartLoading() {



        forceLoad();
    }



    @Override
    public List<Item> loadInBackground() {


        if (mUrl == null) {
            return null;
        }

        // Perform the network request, parse the response, and extract a list of items.
        List<Item> items = QueryUtils.fetchItemData(mUrl);
        return items;
    }

}

Next up, In my main activity I implement loader callbacks.

private ItemAdapter mAdapter;

    @Override
        public android.content.Loader<List<Item>> onCreateLoader(int i, Bundle bundle) {

            return new ItemLoader(this,API_REQUEST_URL);
        }


     @Override
        public void onLoadFinished(android.content.Loader<List<Item>> loader, List<Item> items) {
            mAdapter.clear();

            if (items != null && !items.isEmpty()) {


                mAdapter.addAll(items);
            }

        }

@Override
    public void onLoaderReset(android.content.Loader<List<Item>> loader) {
        Log.e("init loader","Log");

        mAdapter.clear();
    }

In onCreate

 android.app.LoaderManager loaderManager = getLoaderManager();
            loaderManager.initLoader(0, null, MainActivity.this);

listView.setAdapter(mAdapter);

I want to search for the names of the items, and return the corresponding in the ListView. I created a search view for it with a setOnQueryTextListener:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);

        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {


                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {

                return false;
            }

        });

        return true;
    }

aaand...now I'm kinda stuck. I know I should implement 'Filterable' on the adapter class, override some methods, create a filter, etc, and it's not that hard if I have a hardcoded List, which I just need to change, but these network requests, background threads, get data from the server stuff confused me so much I don't know what to do.

Halp! :c

2 Answers 2

2

You can override getFilter() method in your adapter and return an implementation of Filter that filters your data set according to the input.

And in your search callback you can call

adapter.getFilter().filter(queryString);

Here's an example

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

Comments

0

Its not that much difficult. From your code i can say you are getting data using Loaders in your activity. And Loaders will provide that data to adapter.

Activity -> Loader -> Fetch Data -> Pass To Adapter -> Display data in list

So now if you want to implement filterable you can implement and get filtered data from available list. If you are providing all data search or filter you can again call loader to get data from db or web service and then display data using new data in adapter.

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.