在我的應用程序中,我有一個包含數百或行的列表視圖。這是一個包含兩個文本視圖的自定義列表視圖(標題&描述)。在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;
}
}
請發佈代碼,以便我們可以很好地理解 – LMK
@latifmohammadkhan代碼在這裏不相關。 – alfasin
你應該去的sqlite數據庫..製作一個sqlite數據庫文件,並將其放置到資產文件夾,並在您的代碼中訪問它。 –