【问题标题】:Items are duplicating each time I start the app in a ListView with SimpleCursorAdapter每次我使用 SimpleCursorAdapter 在 ListView 中启动应用程序时,项目都会重复
【发布时间】:2011-12-03 06:03:45
【问题描述】:

ListView 正在填充来自 Cursor 的项目,我在 SimpleCursorAdapter 中传递这些项目,但每次打开应用程序时,这些项目都是重新添加到列表视图中,不断增加它。当我使用 SimpleAdapter 时,我会这样做:

static final ArrayList<HashMap<String, String>> foobar = new 
                                          ArrayList<HashMap<String,String>>();
   SimpleAdapter adapter = new SimpleAdapter(this, foobar, R.layout.list_item, String[] from, int[] to);
   setListAdapter(adapter);

下一步,解决我的问题:

    @Override
    public void onDestroy(){
    super.onDestroy();
       foobar.removeAll(foobar);
    }

但是现在我不想删除数据库内容,如果我有一个SimpleCursorAdapter怎么解决呢?喜欢这个:

> SimpleCursorAdapter myadapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, String[] from, int[] to);

我尝试过 setListAdapter(null)cursor.close() 以及许多其他方法,但没有效果...

现在,当我使用设备的“后退”按钮退出应用程序时,这些会发生。如果我按“主页”按钮,当我回来时,我有相同数量的项目。所以每次我用“返回”按钮退出时,列表都会重复。

感谢 Kaediil 的回答。评论的行是我改进的地方。孔类:

public class DataBaseActivity extends ListActivity {

 DataBaseMethods dbmet; //is the class that handle database and auxiliar methods working on it

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    dbmet = new DataBaseMethods(this);
    Cursor mycursor= dbmet.getItems(); // 1. add new cursor


    try{
            if(!mycursor.moveToFirst()){   //2.check if there are items in the database
        dbmet.addItems("Daniel","Son","Karate-kid");
        dbmet.addItems("Silv", "Stalone", "Terminator");
        dbmet.addItems("foo", "bar", "buz");
            } //
            showDatabaseContent();
    }

    finally{
        dbmet.close();
    }
}


public void showDatabaseContent(){

     DataBaseMethods dbmet = new DataBaseMethods(this);
    try{

        Cursor cursor = dbmet.getItems();
        SimpleCursorAdapter myadapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, dbmet.FROM, TO);
        setListAdapter(myadapter);
    }
    finally{
        dbmet.close();
    }

}

【问题讨论】:

  • 您提供的代码中不存在您的问题。我猜您的数据是“硬编码”的,您可能只是每次都将其保存到数据库中。查看您的 db 助手类或在 onCreate 下调用其方法的位置。如果找不到,请发布代码。

标签: android android-listview simplecursoradapter


【解决方案1】:

嗯,这条线很可疑:

如果有关系,我将在此活动的 onCreate() 中填充数据库,但我也尝试从其他活动中填充它,并且列表视图的行为相同。

在填充数据库调用时,是否检查数据库是否已填充?

听起来您只是不断向光标指向的数据库添加更多副本。

【讨论】:

  • 不,我不...。看到您的回复,我尝试使用带有 `if cursor.isNull(0)` 的东西,但每次都会生成索引异常,无论我放什么.请问有什么建议吗?
  • 好的,查看已编辑的代码,您肯定会继续添加到数据库中。你(虽然它有点低效)这样做:Cursor cursor = dbmet.getItems(); if(!cur.moveToFirst()) 如果 moveToFirst 为空,则该调用将返回 false。
  • 它有效 :)) 老实说,我想早点尝试,但 cur.isNull(0) 让我灰心丧气,我不再尝试,认为它几乎是一样的。谢谢!我将用一些 cmets 更新问题,使解决方案更加可见。
  • 酷,很高兴我能帮上忙,别忘了把问题也标记为已回答,谢谢。
【解决方案2】:
public void showDatabaseContent(){

     DataBaseMethods dbmet = new DataBaseMethods(this);
    try{

        Cursor cursor = dbmet.getItems();
        SimpleCursorAdapter myadapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, dbmet.FROM, TO);
        setListAdapter(myadapter);
       -->> myadapter.notifyDataSetChanged(); <<----
    }
    finally{
        dbmet.close();
    }


}

您需要调用 notifyDataSetChanged() 来指示列表仅使用新数据使其自身无效。

NOTE: 如果问题来自数据库类。确保每次启动应用程序时都不会在数据库中重新插入项目。我可能会混淆您的问题,因为列表视图或数据库中的项目重复。我非常确定您每次运行应用程序时都会重新插入数据。

【讨论】:

  • 不,不能解决它。我也试图从这个方法中删除dbmet = new DataBaseMethods(this),认为它被调用了两次。如果我每次启动应用程序时都插入,请确保我不想这样做,但我不知道该怎么做。我唯一插入的地方是这里onCreate(),如果我不添加这3行代码我将没有数据,我测试了它。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
相关资源
最近更新 更多