2014-01-24 116 views
1

在我的应用程序中,我有一个包含数百或行的列表视图。这是一个包含两个文本视图的自定义列表视图(标题&描述)。在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; 
    } 
} 
+0

请发布代码,以便我们可以很好地理解 – LMK

+2

@latifmohammadkhan代码在这里不相关。 – alfasin

+0

你应该去的sqlite数据库..制作一个sqlite数据库文件,并将其放置到资产文件夹,并在您的代码中访问它。 –

回答

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); 
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,它们永远不会减慢你的速度。

相关问题