gpt4 book ai didi

android - 在 Android 应用程序中显示大量数据(moSTLy 文本)

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:54 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,我有相对大量的数据(主要是文本形式和键-> 字符串对)我想在我的应用程序中显示。问题是我不想在我的应用程序代码中输入数据,这意味着用关联的数据手动感受每个 TextView。我觉得那不对!我希望我的应用程序从文件(可能是 JSON?)中读取这些数据,然后将每个键-> 字符串对与相关的 TexView 相关联。更清楚地说,我需要从准备好的文本文件中读取并在我的应用程序中使用该文本,并且我想离线执行此操作,我不想使用任何网络服务我应该如何做到这一点?我应该使用数据库还是房间?那么我应该如何在不使用应用程序内插入代码的情况下手动感受数据库?

最佳答案

使用 Volley 进行 JSON 解析

private static final String url ="your url";

创建方法

final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait, while we load the data");
progressDialog.show();

*make a String or JSON request*

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
newurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("response", response.toString());

Log.i("updated Url",newurl);

progressDialog.dismiss();

*then move into the json and create JSON object or JSON Array to get their String values*

try {
JSONObject data = response.getJSONObject("data");
JSONArray onwardflights = data.getJSONArray("data_key");

for (int i = 0; i < onwardflights.length(); i++) {
JSONObject onwardFlightsChild = onwardflights.getJSONObject(i);


String arrival,deparutre,duration,flightno,grossamount,image,direct;
JSONObject object = onwardFlightsChild.getJSONObject("data_key");
Onwardflights onwardflights1 = new Onwardflights(

arrival = onwardFlightsChild.getString("data_key"),
deparutre = onwardFlightsChild.getString("data_key"),
duration = onwardFlightsChild.getString("data_key"),
flightno = onwardFlightsChild.getString("data_key"),
direct = onwardFlightsChild.getString("data_key"),
origin = onwardFlightsChild.getString("data_key"));

Log.i("Origin",origin);

Fare fare = new Fare(
grossamount = object.getString("data_key")
);


onwardflights1.setFare(fare);
suggestionmodel.add(onwardflights1);
}

*Create an instance of adapter and pass the parameters*



RecyclerAdapter recyclerAdapter = new RecyclerAdapter(suggestionmodel,getApplicationContext());
recyclerView.setAdapter(recyclerAdapter);
} catch (JSONException e) {
e.printStackTrace();

}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("THIS", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();

}
});

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

requestQueue.add(jsonObjReq);

}

关于android - 在 Android 应用程序中显示大量数据(moSTLy 文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51337606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com