【问题标题】:update the listview after inserting a new record with simpleCursorAdapter - requery()使用 simpleCursorAdapter 插入新记录后更新列表视图 - requery()
【发布时间】:2012-12-19 13:11:16
【问题描述】:

我了解requery() 机制,但我不了解实现:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        datasource = new pollDataSource(this);
        datasource.open();

        Cursor values = datasource.getAllCategorie();

        String[] categorieColumns =
            {
                MySQLiteHelper.COLUMN_NOME   // Contract class constant containing the word column name

            };


            int[] mWordListItems = { R.id.categoria_label };


        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(),               // The application's Context object
                R.layout.single_list_item,             // A layout in XML for one row in the ListView
                values,                                // The result from the query
                categorieColumns,                      // A string array of column names in the cursor
                mWordListItems,                        // An integer array of view IDs in the row layout
                0);                                    // Flags (usually none are needed)


        setListAdapter(adapter);
    }




      public void onClick(View view) {
          categorie categoria = null;
        switch (view.getId()) {
        case R.id.add:

            categoria = datasource.createCategoria("pluto") ;
          break;


        case R.id.categoria_label:


          break;

        }

      }

categoria = datasource.createCategoria("pluto"); 之后我必须定义另一个:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                    getApplicationContext(),               // The application's Context object
                    R.layout.single_list_item,             // A layout in XML for one row in the ListView
                    values,                                // The result from the query
                    categorieColumns,                      // A string array of column names in the cursor
                    mWordListItems,                        // An integer array of view IDs in the row layout
                    0);                                    // Flags (usually none are needed)


            setListAdapter(adapter);

投票数据源

public class pollDataSource {
    // Database fields
      private SQLiteDatabase database;
      private MySQLiteHelper dbHelper;
      private String[] allCategorieColumns = { MySQLiteHelper.COLUMN_ID,
          MySQLiteHelper.COLUMN_PREF, MySQLiteHelper.COLUMN_NOME };   
      private String[] allSondaggiColumns = { MySQLiteHelper.COLUMN_ID,
              MySQLiteHelper.COLUMN_CATID, MySQLiteHelper.COLUMN_DOMANDA };   
      private String[] allRisposteColumns = { MySQLiteHelper.COLUMN_ID, 
              MySQLiteHelper.COLUMN_SONDID, MySQLiteHelper.COLUMN_RISPOSTA, 
              MySQLiteHelper.COLUMN_SELEZIONATA };


      public pollDataSource(Context context) {
            dbHelper = new MySQLiteHelper(context);
          }

      public void open() throws SQLException {
            database = dbHelper.getWritableDatabase();
          }

      public void close() {
            dbHelper.close();
          }

      public categorie createCategoria(String categoria) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_NOME, categoria);
            values.put(MySQLiteHelper.COLUMN_PREF, 0);
            long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null,
                values);
            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
            cursor.moveToFirst();
            categorie newCategoria = cursorToCategorie(cursor);
            cursor.close();
            return newCategoria;
          } 

      public void deleteCategoria(categorie categoria) {
            long id = categoria.getId();
            System.out.println("Categoria cancellata, id: " + id);
            database.delete(MySQLiteHelper.TABLE_CATEGORIE, MySQLiteHelper.COLUMN_ID
                + " = " + id, null);
          }

      public sondaggi createSondaggio(String domanda, int catid) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_DOMANDA, domanda);
            values.put(MySQLiteHelper.COLUMN_CATID, catid);
            long insertId = database.insert(MySQLiteHelper.TABLE_SONDAGGI, null,
                values);
            Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
                allSondaggiColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
            cursor.moveToFirst();
            sondaggi newSondaggio = cursorToSondaggi(cursor);
            cursor.close();
            return newSondaggio;
          }


      public void deleteSondaggio(sondaggi sondaggio) {
            long id = sondaggio.getId();
            System.out.println("Sondaggio cancellato, id: " + id);
            database.delete(MySQLiteHelper.TABLE_SONDAGGI, MySQLiteHelper.COLUMN_ID
                + " = " + id, null);
          } 

      public Cursor getAllCategorie() {
            List<categorie> categorie = new ArrayList<categorie>();

            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, null, null, null, null, null);

            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
              categorie categoria = cursorToCategorie(cursor);
              categorie.add(categoria);
              cursor.moveToNext();
            }
            // Make sure to close the cursor
           // cursor.close();
            return cursor;
          }


      private categorie cursorToCategorie(Cursor cursor) {
          categorie categorie = new categorie();
          categorie.setId(cursor.getLong(0));
          categorie.setPreferita(cursor.getLong(1));
          categorie.setNome(cursor.getString(2));
            return categorie;
          }
      private sondaggi cursorToSondaggi(Cursor cursor) {
          sondaggi sondaggi = new sondaggi();
          sondaggi.setId(cursor.getLong(0));
          sondaggi.setDomanda(cursor.getString(1));
          sondaggi.setCatid(cursor.getLong(2));
          return sondaggi;
          }


}

使用相同的输入??所以基本上我将在同一个类的两个不同地方有相同的确切代码......我可以定义一个......“程序”或一个类吗?对不起,如果我太天真了,我是一个真正的菜鸟:D

【问题讨论】:

  • 同时添加 pollDataSource 类。
  • 你想从 ListView 显示数据添加/删除吗?
  • 是的,实际上,当我按下“添加”按钮时,它会添加类别,但我只能通过重新启动活动来查看它们,因为列表视图仅在 onCreate 中更新。

标签: android sqlite listview


【解决方案1】:

requery 游标方法已弃用。但你是对的,你必须重新加载光标,然后确保你的新光标呈现给你的ListView。对于快速'n 肮脏的解决方案,我建议您将适配器设置为字段。然后使用以下方法:

private void requery() {
    Cursor values = datasource.getAllCategorie();
    adapter.changeCursor(values);
}

它所做的是重新制作光标。然后通过调用changeCursor 将旧光标换成新光标,旧光标会自动关闭。至少,您可以避免制作新的适配器。我想对所有修剪和修复的更复杂的方法是实现一个CursorLoader,它会在数据库内容发生更改时自动执行该过程。

【讨论】:

  • 谢谢,我现在试试这个,实际上,我已经在 onclick 类中复制了 simpleadapter 部分...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多