編集: ((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;
}
}