编辑: ((ArrayAdapter) getListAdapter()).notifyDataSetChanged(); 不起作用-我认为刷新整个列表而不是仅添加新条目是一个问题,但我不确定-如果是这样,我该怎么做?
从我的 sqlite 数据库中检索数据并将其显示在 listactivity 中的正确方法是什么?我的代码(有点)但在第一次运行后没有正确刷新列表数据如下。我需要发生的是:
- ListActivity 加载显示的数据库内容(在从我也编写的 REST API 获取之后)
- 用户按下刷新按钮,新故事会出现在列表顶部,而用户不会丢失其在列表中的当前位置
任何帮助将不胜感激!
public void populateStoryList() {
List<StoryItem> stories = datasource.getAllStoryItems();
if ( firstrun == true ) {
adapter = new StoryAdapter(this, stories);
setListAdapter(adapter);
firstrun = false;
} else {
// DON'T KNOW WHAT TO DO HERE
}
setProgressBarIndeterminateVisibility(false);
}
而getAllStoryItems()函数如下:
public List<StoryItem> getAllStoryItems() {
List<StoryItem> storyList = new ArrayList<StoryItem>();
Cursor cursor = database.query(StoriesTable.TABLE_STORIES,
allColumns, null, null, null, null, StoriesTable.COLUMN_DATE_APPROVED+" DESC");
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
StoryItem story = cursorToStoryItem(cursor);
storyList.add(story);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return storyList;
}
和 StoryAdapter:
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
@SuppressWarnings("rawtypes")
public class StoryAdapter extends ArrayAdapter {
private final Activity activity;
private final List stories;
@SuppressWarnings("unchecked")
public StoryAdapter(Activity activity, List objects) {
super(activity, R.layout.story_row , objects);
this.activity = activity;
this.stories = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
StoryAdapterView sView = null;
if(rowView == null)
{
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.story_row, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sView = new StoryAdapterView();
sView.title = (TextView) rowView.findViewById(R.id.title);
sView.story = (TextView) rowView.findViewById(R.id.story);
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sView);
} else {
sView = (StoryAdapterView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
StoryItem currentStock = (StoryItem) stories.get(position);
sView.title.setText(currentStock.getStoryTitle());
sView.story.setText(currentStock.getStoryText());
return rowView;
}
protected static class StoryAdapterView {
protected TextView title;
protected TextView story;
}
}
最后是 StoryItem:
public class StoryItem {
private String dateApproved;
private String storyTitle;
private String storyText;
private long storyId;
private long storyOnlineId;
public StoryItem() {}
public StoryItem(String storyTitle, String storyText, String dateApproved, long storyId, long storyOnlineId) {
this.storyTitle = storyTitle;
this.storyText = storyText;
this.dateApproved = dateApproved;
this.storyId = storyId;
this.storyOnlineId = storyOnlineId;
}
public long getId() {
return storyId;
}
public void setId(long id) {
this.storyId = id;
}
public long getOnlineId() {
return storyOnlineId;
}
public void setOnlineId(long online_id) {
this.storyOnlineId = online_id;
}
public void setStoryTitle(String storyTitle) {
this.storyTitle = storyTitle;
}
public String getStoryTitle() {
return storyTitle;
}
public void setStoryText(String storyText) {
this.storyText = storyText;
}
public String getStoryText() {
return storyText;
}
public void setDateApproved(String dateApproved) {
this.dateApproved = dateApproved;
}
public String getDateApproved() {
return dateApproved;
}
}