0

固特异给大家。

好吧,这就是问题所在,我想了解列表视图的逻辑,以及如何检索其中单击的项目的信息。

我已经完成并定制了一堆教程来做这个应用程序,现在我试图弄清楚我在这个丑陋和肮脏的代码中做了什么。

我有一个活动,它列出了一系列问题,在您选择一个类别后,选择一个问题,将您带到下一个活动,其中列出了一些答案,用户可以选择一个。我只想列出用户必须回答的那些问题,而不是全部,所以我在类别表中添加了一个名为“fatta”的新列 - 英文中的“done”,我想在何时更改为 1用户单击该问题的答案。

所以这个想法是传递问题的 id,所以在我列出所有答案的活动中,我可以更新答案和问题表,将“完成字段”设置为 1。

但出了点问题。这是代码:

活动:

public class sondaggioActivity extends ListActivity{
    private static final String strdomanda = null;
    private pollDataSource datasource;

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

        long domanda = getIntent().getExtras().getLong("domanda"); 
        TextView text = (TextView) findViewById(R.id.domanda);
        String strdomanda = Long.toString(domanda);
    //   text.setText(strcategory);

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


        Cursor values = datasource.getTestoRisposte(strdomanda);
    // data?    if (values.moveToFirst()) 

    //       System.out.println(values.getString(values.getColumnIndex("sondid"))); 



        String[] categorieColumns =
            {
                MySQLiteHelper.COLUMN_RISPOSTA   // 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);
    //values.close(); 
    }

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

    datasource.setRispostaSelezionata(Long.toString(id));

    datasource.setDomandaFatta(strdomanda);
    datasource.close();

    finish();



    }

}

正如您在最后看到的那样onListItemClick,我使用strdomanda我在顶部定义private static final String strdomanda = null;数据源是这样的:

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_WEBID,MySQLiteHelper.COLUMN_FATTA, MySQLiteHelper.COLUMN_DOMANDA };   
      private String[] allRisposteColumns = { MySQLiteHelper.COLUMN_ID, 
              MySQLiteHelper.COLUMN_SONDID,MySQLiteHelper.COLUMN_WEBID, 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, int webid) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_DOMANDA, domanda);
            values.put(MySQLiteHelper.COLUMN_CATID, catid);
            values.put(MySQLiteHelper.COLUMN_WEBID, webid);
            values.put(MySQLiteHelper.COLUMN_FATTA, "0");
            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;
          }

      public Cursor getDomanda(String id) {
            List<sondaggi> domande = new ArrayList<sondaggi>();


            Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
                    allSondaggiColumns,
                    MySQLiteHelper.COLUMN_ID + "=?",
                    new String[] { id }, null, null, null);

            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
              sondaggi sondaggio = cursorToSondaggi(cursor);
              domande.add(sondaggio);
              cursor.moveToNext();
            }

            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));
          sondaggi.setwebid(cursor.getLong(3));
          return sondaggi;
          }

    public Cursor getAllDomandeCategoria(String catid) {
        List<sondaggi> domande = new ArrayList<sondaggi>();


        Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
                allSondaggiColumns,
                MySQLiteHelper.COLUMN_CATID + "=?",
                new String[] { catid }, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
          sondaggi sondaggio = cursorToSondaggi(cursor);
          domande.add(sondaggio);
          cursor.moveToNext();
        }

        return cursor;
    }

    public Cursor getTestoRisposte(String strdomanda) {

        List<testo_risposte> domande = new ArrayList<testo_risposte>();    
        Cursor cursor = database.query(MySQLiteHelper.TABLE_TESTORISPOSTE,
                allRisposteColumns,
                MySQLiteHelper.COLUMN_SONDID + "=?",
                new String[] { strdomanda }, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
         testo_risposte sondaggio = cursorToTestoRisposte(cursor);
          domande.add(sondaggio);
          cursor.moveToNext();
        }

        return cursor;
    }

    private testo_risposte cursorToTestoRisposte(Cursor cursor) {
        testo_risposte testo_risposte = new testo_risposte();
        testo_risposte.setId(cursor.getLong(0));
        testo_risposte.setCatid(cursor.getLong(1));
        testo_risposte.setWebid(cursor.getLong(4));
        testo_risposte.Setselezionata(cursor.getLong(2));
        testo_risposte.setDomanda(cursor.getString(3));
     return testo_risposte;
    }

    public void setRispostaSelezionata(String id) {     
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_SELEZIONATA, "1");
        database.update(MySQLiteHelper.TABLE_TESTORISPOSTE,values,MySQLiteHelper.COLUMN_ID + "=?", new String[] { id });    
    }

    public void setDomandaFatta(String strdomanda) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_FATTA, "1");
        database.update(MySQLiteHelper.TABLE_SONDAGGI,values,MySQLiteHelper.COLUMN_ID + "=?", new String[] { strdomanda });

    }


}

但是当我运行活动时,只有第一个更新很好,另一个,我想“停用”问题的那个......什么都不做......我想我通过 strdomanda 的方式不是好方法。有什么帮助吗?建议?提前致谢。

4

1 回答 1

0

已解决:我弄错了这一行:

private static String strdomanda = null;

我已经删除了该Final属性,并且我已经删除了同一变量的另一个声明......现在它可以工作了。

于 2013-01-02T13:03:34.480 回答