检索用户在列表视图上单击的元素的ID(在数据库中)

时间:2012-12-20 10:49:12

标签: android sqlite android-intent android-activity listviewitem

这就是我所做的,检索id,但它说光标中没有定义getIndexColumn ......我做错了什么?

protected void onListItemClick(ListView l, View v, int position, long id) {
          Cursor data = (Cursor)l.getItemAtPosition(position);
          String cat = Cursor.getString(Cursor.getIndexColumn(MySQLiteHelper.COLUMN_ID));

          Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
          myIntent.putExtra("categoriaId", cat);
          MainActivity.this.startActivity(myIntent);

        }

这是类别类:

public class categorie {
      private long id;
      private String nome;
      private long preferita;

      public long getId() {
        return id;
      }

      public void setId(long id) {
        this.id = id;
      }

      public String getNome() {
        return nome;
      }

      public void setNome(String nome) {
        this.nome = nome;
      }



      public long getPreferita() {
        return preferita;
    }

    public void setPreferita(long preferita) {
        this.preferita = preferita;
    }

    // Will be used by the ArrayAdapter in the ListView
      @Override
      public String toString() {
        return nome;
      }
    } 

,这是数据源:

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


}

主要活动:

public class MainActivity extends ListActivity {
     private pollDataSource datasource;
    @Override
    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) {

        switch (view.getId()) {
        case R.id.add:      
        datasource.createCategoria("peppe");          


        break;          

        }





      }

      @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {


          Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
          myIntent.putExtra("categoriaId", id);
          MainActivity.this.startActivity(myIntent);
         //Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
        }   




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

3 个答案:

答案 0 :(得分:2)

我假设您有一个包含类别列表的活动,并且您要启动具有该项详细信息的新活动的特定项目的onClick。 我建议你在启动listScreen,查询所有/一些项目并维护一个项目的数组列表并将其保存在某个单例类中,然后onClick一个特定的项目通过intent.putExtra将该索引传递给详细信息屏幕(“index”,position )和详细信息屏幕通过getIntent()获取该索引.getIntExtra(“index”, - 1)。现在从单个类中保存的arraylist获取该特定索引的详细信息。 这种方法可以降低每次从数据库查询的成本,并且可以轻松获得数据。

答案 1 :(得分:1)

更改

 Cursor data = (Cursor)l.getItemAtPosition(position);
 String cat = Cursor.getString(Cursor.getIndexColumn(MySQLiteHelper.COLUMN_ID));

 Cursor data = (Cursor)l.getItemAtPosition(position);
 Long clid = data.getLong(data.getIndexColumn(MySQLiteHelper.COLUMN_ID));
 String cat=Long.toString(clid);

答案 2 :(得分:1)

这两行:

      Cursor data = (Cursor)l.getItemAtPosition(position);
      String cat = Cursor.getString(Cursor.getIndexColumn(MySQLiteHelper.COLUMN_ID));

完全没有任何意义!如果您正在使用CursorAdapter,为什么要创建一个对象数组?如果您正在使用ArrayAdapter,为什么要从光标中获取数据?

此外,Cursor没有像这样调用的任何静态方法。那甚至不应该编译。

如果您正在使用CursorAdater(或某些扩展它的类),那么您的身份long id protected void onListItemClick(ListView l, View v, int position, long id)会传递给您{{1}}