2014-01-24 34 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,它們永遠不會減慢你的速度。

相關問題