我正在构建一个应用程序,其中包含多个选项卡的视图。每个标签都包含一个RecyclerView
,该MySQL
来自Activity
中的数据。因此,ViewPager
包含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;
}
片段:
@Override
public void sendCoffeeListData(String name, String image, String price) {
coffeesList.add(new ProductList(image, name, price));
}
活动:
coffeeList
然后我有"index_files": false
可供搜索。
任何想法都会有所帮助。
答案 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
是您创建的类。这是你的想法吗?