java - Android,从sqlite检索数据并显示在自定义列表中的正确方法

标签 java android sqlite listview listactivity

编辑:((ArrayAdapter) getListAdapter()).notifyDataSetChanged();不起作用 - 我认为这是一个与我刷新整个列表而不是仅添加新条目有关的问题,但我不确定 - 如果是这样我该怎么做?

从我的 sqlite 数据库检索数据并将其显示在列表 Activity 中的正确方法是什么?我的代码可以(在某种程度上)工作,但在第一次运行后无法正确刷新列表数据,如下所示。我需要发生的是:

  • 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;
}

还有故事适配器:

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;
    }
}

最佳答案

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 方法将不起作用,并且您在这里不需要它。它仅用于重绘现有项目而不更改它们,而您想要添加新项目并更改列表的结构。

关于java - Android,从sqlite检索数据并显示在自定义列表中的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14710983/

相关文章:

javascript - 需要用JS检查SQlite中是否存在表

c# - system.data.sqlite 的内存数据库包装器

java - 将新值放置到哈希表中的现有键中

java - LocalDateTime 解析短年份格式 dd-MMM-YY HH :mm using DateTimeFormatterBuilder

java - 为 JToolbar 创建复合 Swing 组件

android - 是不是所有安卓设备都兼容dji SDK mobile?

android - 现有 NFC 读取器基础设施的应用程序 ID

java - Jackson XmlMapper.config() 不起作用

c# - 如何将 Android.Graphics 的 GetPixel 给出的 Int 转换为 Color?

javascript - 返回重复条目