简体   繁体   中英

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. One that stores all the headings for the listview and other array that stores all the descriptions for the lsitview. When the listview is created, I populate the listviews with the heading and the description from each of the string arrays.

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.

<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.

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(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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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