问题描述
我目前的code是:
content = ((MainActivity)getActivity()).connectDB(MY_DB, TABLE, ID, FIELD, ID, null);
listView.setAdapter(new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, content));
我想第一项手动添加到ListView和然后填充从数据库中的所有项目旁边
I want to add first item to listView manually and then populate all the next items from database.
我试图让 content.add(0字符串);
调用connectDB但在此之前它会导致一个错误
I have tried to make content.add(0,"String");
before calling the connectDB but it results to an error
推荐答案
根据您的code是pretty简单。下面是一些硬codeD的东西,你会做什么的例子。
Based on your code it is pretty simple. Here's some hardcoded stuff as an example of what you might do.
public List<String> getMockDataFromDatabase() {
List<String> itemList = new ArrayList<String>();
itemList.add("Item 1");
itemList.add("Item 2");
itemList.add("Item 3");
itemList.add("Item 4");
return itemList;
}
public void setupListAdapter() {
List<String> itemList = new ArrayList<String>();
itemList.add("New First Item");
itemList.addAll(getMockDataFromDatabase());
listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemList));
}
我之所以重新创建列表是,如果你只是将其添加为标题查看它不会被保留,当活动进入一段时间,而在ListView将在内部保留适配器,因此在它的项目。
The reason why I re-create the list is that if you just add it as a header view it will not be retained when the activity goes awhile, while the ListView will internally retain the Adapter and thus the items in it.
这篇关于首先填写列表视图项静态和所有其他的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!