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

Java Android,从sqlite检索数据并显示在自定义列表中的正确方法,java,android,sqlite,listview,listactivity,Java,Android,Sqlite,Listview,Listactivity,编辑:((ArrayAdapter)getListAdapter()).notifyDataSetChanged();不起作用-我想这是一个问题,我需要刷新整个列表,而不是仅仅添加新条目,但我不确定-如果是这样,我该怎么做? 从sqlite数据库检索数据并在listactivity中显示数据的正确方法是什么?下面是我的代码,它可以正常工作(有点),但在第一次运行后不能正确刷新列表数据。我需要做的是: 要加载并显示数据库内容的ListActivity(在从我也编写的RESTAPI获取之后) 用户

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

从sqlite数据库检索数据并在listactivity中显示数据的正确方法是什么?下面是我的代码,它可以正常工作(有点),但在第一次运行后不能正确刷新列表数据。我需要做的是:

  • 要加载并显示数据库内容的ListActivity(在从我也编写的RESTAPI获取之后)
  • 用户按下“刷新”按钮,新故事将显示在列表顶部,而用户不会丢失其在列表中的当前位置
任何帮助都将不胜感激

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);
}
最后一个故事项:

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);
}
public void populateStoryList(){
SetProgressBarInDeterminateVibility(真);
if(this.adapter==null){
//第一次,因为adapter=null
List stories=datasource.getAllStoryItems();
this.adapter=新故事适配器(this,stories);
setListAdapter(this.adapter);
}否则{
List newStories=datasource.getOnlyNewStoryItems();
用于(故事项故事:新闻故事){
this.adapter.insert(故事,0);
}
//当然,您可以清除所有项并完全替换它们,但这对性能不好,因此我不建议使用注释代码
//List stories=datasource.getAllStoryItems();
//this.adapter.clear();
//用于(故事项故事:故事){
//this.adapter.add(故事);
//}
}
SetProgressBarInDeterminateVibility(假);
}
您必须编写方法
getOnlyNewStoryItems


显然,
notifyDataSetChanged
方法将不起作用,您在这里不需要它。它仅用于在不更改现有项的情况下重绘现有项,而您希望添加新项并更改列表的结构。

我尝试了此操作,但出现了以下错误:类型ListAdapter的notifyDataSetChanged()方法未定义-我的类定义是:公共类ActivityStoryList扩展ListActivity{我添加了一个演员阵容(ArrayAdapter)它可以工作-谢谢!您还可以将适配器类扩展到BaseAdapter而不是ArrayAdapter。很高兴看到您找到了解决方案。我想我找到了,但在我重新打开活动之前它似乎不会刷新数据-我有点过早…我想这是因为我没有添加到现有列表中(在线阅读文档),我将完全替换它-不知道如何只添加新项目?感谢vorrtex,这似乎可以实现这个技巧-关于notifyDataSetChanged方法的良好信息。
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;
    }
}
else {
   getListAdapter().notifyDataSetChanged();
}
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);
}