简体   繁体   English

在Android应用中存储大量字符串的有效方法

[英]Efficient way to store lots of strings in android app

In my app, I have a listview which contains hundreds or rows. 在我的应用程序中,我有一个包含数百个或行的列表视图。 It is a custom listview with two textviews (title & description). 这是一个具有两个文本视图(标题和描述)的自定义列表视图。

What I have currently done is that I have two string arrays in strings.xml. 我目前所做的是在strings.xml中有两个字符串数组。 One that stores all the headings for the listview and other array that stores all the descriptions for the lsitview. 一个用于存储listview的所有标题,另一个用于存储lsitview的所有描述的数组。 When the listview is created, I populate the listviews with the heading and the description from each of the string arrays. 创建listview后,我将使用每个字符串数组的标题和描述填充listview。

The problem is that it is getting difficult to manage so many strings. 问题在于管理如此多的字符串变得越来越困难。

It would be great if anyone can suggest any other efficient way to achieve this. 如果有人可以提出任何其他有效的方法来实现这一目标,那就太好了。

The reason I am using strings is that we have very similar other projects to do further, where even some non-programmer can replace the string array values for us. 我使用字符串的原因是,我们还有其他非常相似的项目需要做进一步的工作,甚至一些非程序员都可以为我们替换字符串数组值。 That is why we dont use sqlite. 这就是为什么我们不使用sqlite。

<string-array name="Headings">
    <item>Heading1</item>
    <item>Heading2</item>
    .
    .
</string-array>

<string-array name="Desc">
    <item>Desc1</item>
    <item>Desc2</item>
    .
    .
</string-array>



.java file

List<RowItem> rowItems= new ArrayList<RowItem>();

titles=getResources().getStringArray(R.array.Headings);
descriptions=getResources().getStringArray(R.array.Desc);

for (int j = 0; j < titles.length; j++) {
  RowItem item = new RowItem(titles[j], descriptions[j]);
  rowItems.add(item);
}


ListView lv=(ListView) rootView.findViewById(R.id.listView);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getActivity(),R.layout.list_item,rowItems);
lv.setAdapter(adapter);

class MySimpleArrayAdapter extends ArrayAdapter<RowItem> {
    Context context;

    public MySimpleArrayAdapter(Context context, int resourceId, List<RowItem> items) {
      super(context, resourceId, items);
      this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        RowItem rowItem = getItem(position);
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);

        TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
        TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine);
        textView1.setText(rowItem.getTitle());
        textView2.setText(rowItem.getDesc());
        return rowView;
    }
}


public class RowItem {
    private String title;
    private String desc;

    public RowItem(String title, String desc) {
        this.title = title;
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return title + "\n" + desc;
    }
}
 ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();


        HashMap<String, String> data1 = new HashMap<String, String>();
         data1.put("title","title string");
         data1.put("detail","detail paragraph");


        HashMap<String, String> data2 = new HashMap<String, String>();
        data1.put("title","title string");
         data1.put("detail","detail paragraph");


         myList.add(data1);
         myList.add(data2);

I did something similar, but my data was not static. 我做了类似的事情,但是我的数据不是静态的。 It was being fetched from the web. 它是从网络上获取的。 I used JSON to handle that data. 我使用JSON处理该数据。

I'm now sure how efficient or helpful it will be in your case. 我现在确定在您的情况下这将是多么有效或有帮助。

Store data something like this: 存储数据是这样的:

JSONArray arrayOfJSONObjects = new JSONArray()
                .put(new JSONObject().put("title", "myTitle1").put("details", "myDetails1"))
                .put(new JSONObject().put("title", "myTitle2").put("details", "myDetails2"))
                .put(new JSONObject().put("title", "myTitle3").put("details", "myDetails3"));

To retrieve data use a simple for loop(this will we used in your adapter class): 要检索数据,请使用简单的for循环(这将在您的适配器类中使用):

for(int i=0;i<arrayOfJSONObjects.length();i++)
{
arrayOfJSONObjects.getJSONObject(i).getString("title");
arrayOfJSONObjects.getJSONObject(i).getString("details");
}

Let me say again I'm not sure if this is efficient or helpful, but this is the most common practise while fetching data from URL. 让我再说一遍,我不确定这是否有效或有用,但这是从URL提取数据时最常见的做法。

Any non-programmer can copy paste or even edit the data. 任何非程序员都可以复制粘贴甚至编辑数据。 And u can put thousands of JSONObject, they will never ever slow u down. 而且您可以放入数千个JSONObject,它们永远不会降低速度。

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

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