在 android 中从 mysql 数据库搜索多个数据时出现问题

Trouble when searching multiple data from mysql database in android

我正在构建一个应用程序,其中有一个包含多个选项卡的视图。每个选项卡都包含一个 RecyclerView,它是从 MySQL 中的数据填充的。所以有 ActivityViewPager,然后每个选项卡都有自己的 Fragment。所以我使用 Interface 将数据从 Fragment 传递到 Activity 以便能够搜索。问题是,如果我不滚动选项卡,回收站 List 为空,因此我无法搜索。我的问题是如果我直接在数据库中搜索会更好还是有其他方法可以做到这一点?

我的应用程序如下所示:

传递数据的代码为:

接口:

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

片段:

@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));
    }

那么我有 coffeeList 可供搜索。

任何想法都会有所帮助。

在进行显式搜索时查询数据库是有意义的。这将避免您当前遇到的数据尚未填充的问题。

如果我理解正确的话,你想将数据填充到 RecyclerView。在那种情况下,您的方法不是正确的方法。这是一个显示数据填充的 Android 网页。以下是该网页的代码示例:

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

备注:

  • 在此代码示例中,myDataset 只是一个字符串数组;这是数据。它实际上可以是任何对象。 MyAdapter 是您创建的 class。
  • RecyclerView 使用了 Adapter 的思想,就像在 ListView 中一样。
  • 此方法在创建视图后立即填充数据。它使用虚拟缓存。

这是你的想法吗?

我可以提供代码示例,但您更了解您的代码。这是一个示例:

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));
    }
}

片段:

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

注意: 基本上你将数据填充到 Activity 中的 coffeesList 上。然后将列表传递到片段上。我通过调用 Fragment 中的方法 setList 来传递 List 对象,因为它更容易。

我希望你已经足够清楚了。这种做法有问题吗?