0

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

2 に答える 2

1

firstRun変数の目的は何ですか? 削除する必要があると確信しています。そうすれば、すべてが正常に機能します。

また、私はあなたの方法を次のように書き直します:

public void populateStoryList() {
    setProgressBarIndeterminateVisibility(true);

    if (this.adapter == null) {
        // first time, because adapter = null
        List<StoryItem> stories = datasource.getAllStoryItems();  
        this.adapter = new StoryAdapter(this, stories);
        setListAdapter(this.adapter);
    } else {
        List<StoryItem> newStories = datasource.getOnlyNewStoryItems(); 
        for (StoryItem story : newStories) {
            this.adapter.insert(story, 0);
        }
        // of course you can clear all items and replace them entirely, but it is not good for performance, so I don't recommend to use the commented code
        //List<StoryItem> stories = datasource.getAllStoryItems();  
        //this.adapter.clear();
        //for (StoryItem story : stories) {
        //    this.adapter.add(story);
        //}
    }

    setProgressBarIndeterminateVisibility(false);
}

メソッドを記述する必要がありますgetOnlyNewStoryItems

そして、notifyDataSetChangedメソッドが機能しないことは明らかであり、ここでは必要ありません。新しいアイテムを追加してリストの構造を変更したいのに対し、既存のアイテムを変更せずに再描画するためだけに使用されます。

于 2013-02-05T18:59:18.133 に答える
0
else {
   getListAdapter().notifyDataSetChanged();
}
于 2013-02-05T15:32:34.457 に答える