简体   繁体   English

在Android应用中显示大量数据(主要是文本)

[英]Display large amount of data(mostly text) in android app

I'm creating an android app and i have a relatively large amount of data (mostly in text form and as key->string pairs) that i want to display in my app. 我正在创建一个android应用程序,并且我想在我的应用程序中显示相对大量的数据(主要是文本形式以及作为键->字符串对)。 The problem is i don't want to enter the data in my app's code,meaning feel each TextView manually with the associated data.i don't feel that to be right! 问题是我不想在我的应用程序代码中输入数据,这意味着手动感觉每个TextView及其相关数据。我不认为这是正确的! I want my app to read this data from a file(maybe a JSON?) and then associate each key-> string pair with the related TexView. 我希望我的应用程序从文件(可能是JSON?)中读取此数据,然后将每个key->字符串对与相关的TexView关联。 To be more clear, i need to read from a prepared text file and use that text inside my app, AND i want to do this offline,i dont want to use any webservic How should i accomplish this? 更清楚地说,我需要阅读准备好的文本文件,并在我的应用程序中使用该文本,并且我想脱机执行此操作,我不希望使用任何网络服务该怎么办? should i use a database or room?Then how should i feel the database manually without using insert codes inside the app? 我应该使用数据库还是房间?那么我应该如何在不使用应用程序内部插入代码的情况下手动感受数据库?

Use Volley for JSON Parsing 使用Volley进行JSON解析

private static final String url ="your url";

Create a method 创建一个方法

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

 }

You could simply use a JSON file. 您可以简单地使用JSON文件。 Now, the question is: How do you ship a JSON file with your app so that it's available at Runtime? 现在,问题是: 如何将JSON文件与您的应用一起交付,以便在运行时可用? You can use any of these methods: 您可以使用以下任何一种方法:

  • Put it in the directory res/raw/ and read it using the Resources class (it has a openRawResource() method) 将其放在res/raw/目录中,并使用Resources类读取它(它具有openRawResource()方法)

  • Put it in the assets/ directory and read it in using the AssetManager class (it has a open() method that returns an InputStream) 将其放在assets/目录中,并使用AssetManager类读取它(它具有open()方法,该方法返回InputStream)

I hope this helps... Merry coding! 希望对您有所帮助...编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM