1

I am building an app in which I have a view with multiple tabs. Each tab contains a RecyclerView which gets populated from data in MySQL. So there is the Activity with the ViewPager and then each tab has its own Fragment. So I use the Interface to pass data from Fragment to Activity in order to be able to search for. The thing is that if I do not scroll over the tabs the recycler List is null so I cannot search. My question is would it be better if I search directly in the database or is there any other way to do this?

My App looks like this:

App

The code for passing data is:

Interface:

public interface CoffeeCommunicator {
    void sendCoffeeListData(String name, String image, String price);
}

Fragment:

@Override
        protected List<ProductList> doInBackground(String... params) {
            try {
                url = new URL(params[0]);
                urlConnection =(HttpURLConnection) url.openConnection();
                urlConnection.connect();
                urlConnection.setConnectTimeout(5000);
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                jsonResult = StringGenerator.inputStreamToString(in, getActivity());
                customList = new ArrayList<>();

                jsonResponse = new JSONObject(jsonResult.toString());
                jsonMainNode = jsonResponse.optJSONArray(AppConstant.COFFEE_JSON_ARRAY);
                for (int i = 0; i < jsonMainNode.length(); i++) {
                    jsonChildNode = jsonMainNode.getJSONObject(i);
                    name = jsonChildNode.optString("name");
                    price = jsonChildNode.optString("price");
                    image = jsonChildNode.optString("image");
                    customList.add(new ProductList(image, name, price));
                    coffeeCommunicator.sendCoffeeListData(name, image, price);

                }
            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }
            return customList;
        }

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        coffeeCommunicator = (CoffeeCommunicator)activity;
    }

Activity:

@Override
    public void sendCoffeeListData(String name, String image, String price) {
        coffeesList.add(new ProductList(image, name, price));
    }

So then i have coffeeList available to search for.

Any ideas would be helpfull.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Kostas Drak
  • 3,222
  • 6
  • 28
  • 60

3 Answers3

1

I can give code sample but you know your code better. Here's a sample:

In Activity:

populateList();

coffeeFragment = CoffeeFragment.newInstance();
coffeeFragment.setList(coffeesList);

void populateList() {
    jsonResponse = new JSONObject(jsonResult.toString());
    jsonMainNode = jsonResponse.optJSONArray(AppConstant.COFFEE_JSON_ARRAY);
    for (int i = 0; i < jsonMainNode.length(); i++) {
        jsonChildNode = jsonMainNode.getJSONObject(i);
        name = jsonChildNode.optString("name");
        price = jsonChildNode.optString("price");
        image = jsonChildNode.optString("image");
        coffeesList.add(new ProductList(image, name, price));
    }
}

Fragment:

public void setList(final List<ProductList> arrayList) {
{
    customList = arrayList;
}

Note: Basically you fill data onto coffeesList in Activity. And then pass the List onto the Fragment. I pass a List object by calling a method setList in the Fragment because it is easier.

I hope that is clear enough. Is there a problem with this approach?

The Original Android
  • 6,147
  • 3
  • 26
  • 31
0

It would make sense to query the database when doing an explicit search. That will avoid the problem you're currently having of the data not yet being populated.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

If I understand correctly you want to populate data onto RecyclerView. In that case, your method is not the proper way. Here is one Android web page showing data population. Below is a code sample from that web page:

// specify an adapter (see also next example)
    mAdapter = new MyAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);

Notes:

  • In this code example, myDataset is simply an array of strings; this is the data. It can be any object actually. MyAdapter is a class that you create.
  • RecyclerView uses the idea of Adapter, like in ListView.
  • This method populates data immediately after the view is created. It uses virtual caching.

Is this what you had in mind?

The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • i have data in recyclerview...the problemm is that they are not available for search if i dont scroll the tabs so they can get populated – Kostas Drak Dec 03 '15 at 13:27
  • 1
    @helldawg13, In your case, one easy technique is to populate the List coffeesList even before the tab is in view. With your requirement, you cannot fill the data coffeesList in method doInBackground(). Do you need code sample for it for clarity? – The Original Android Dec 03 '15 at 18:37
  • I provide a sample code in another post because it has a different code design and approach. – The Original Android Dec 04 '15 at 08:19