从android中的mysql数据库中搜索多个数据时遇到麻烦

时间:2015-11-21 13:24:26

标签: android mysql android-fragments android-search

我正在构建一个应用程序,其中包含多个选项卡的视图。每个标签都包含一个RecyclerView,该MySQL来自Activity中的数据。因此,ViewPager包含Fragment,然后每个标签都有自己的Interface。因此,我使用Fragment将数据从Activity传递到List,以便能够搜索。问题是如果我不滚动选项卡,回收器public interface CoffeeCommunicator { void sendCoffeeListData(String name, String image, String price); } 为空,所以我无法搜索。我的问题是,如果我直接在数据库中搜索还是有其他方法可以做到这一点会更好吗?

我的应用程序看起来像这样:

App

传递数据的代码是:

接口

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

片段:

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

活动:

coffeeList

然后我有"index_files": false可供搜索。

任何想法都会有所帮助。

3 个答案:

答案 0 :(得分:1)

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

活动

boolean testRunning = true;
JButton buttonQuit;

@Test
public void start() {
    MainFrame.getInstance().setVisible(true);
    if (showHelpDialog) {
        HelpDialog.getInstance().setVisible(true);
    }
    while(testRunning) {

    }
}

<强>片段

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

注意: 基本上,您将数据填充到Activity中的public void setList(final List<ProductList> arrayList) { { customList = arrayList; } 。然后将List传递给Fragment。我通过调用Fragment中的方法coffeesList来传递List对象,因为它更容易。

我希望这很清楚。这种方法有问题吗?

答案 1 :(得分:0)

在进行显式搜索时查询数据库是有意义的。这样可以避免您目前对尚未填充数据的问题。

答案 2 :(得分:0)

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

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

注意:

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

这是你的想法吗?