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

时间:2014-01-24 04:49:10

标签: android

在我的应用中,我有一个包含数百或行的列表视图。这是一个自定义列表视图,包含两个文本视图(标题和说明)。

我目前所做的是在strings.xml中有两个字符串数组。一个存储listview的所有标题,另一个存储lsitview的所有描述。创建列表视图时,我使用每个字符串数组中的标题和描述填充列表视图。

问题在于管理这么多字符串变得越来越困难。

如果有人能提出任何其他有效的方法来实现这一点,那就太好了。

我使用字符串的原因是我们有非常类似的其他项目要做进一步的工作,甚至一些非程序员也可以为我们替换字符串数组值。这就是为什么我们不使用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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我做了类似的事情,但我的数据不是静态的。它是从网上获取的。我使用JSON来处理这些数据。

我现在确定你的情况会有多么有效或有用。

存储数据如下:

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

要检索数据,请使用简单的for循环(我们将在您的适配器类中使用):

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

我再说一遍,我不确定这是否有效或有用,但这是从URL获取数据时最常见的做法。

任何非程序员都可以复制粘贴甚至编辑数据。你可以放上成千上万的JSONObject,它们永远不会让你失速。

相关问题