无法正确返回arraylist对象

时间:2014-11-10 14:03:36

标签: java android arraylist

这是我的活动课程:

package com.blogspot.joyouslybeingjoy.cybpromises;

    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ListView;

    import java.util.ArrayList;

public class DisplayResult extends Activity {

String selectedCategory;

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

    Bundle b = getIntent().getExtras();
    String selectedCategory = b.getString("ITEMVALUE");

    //Toast.makeText(DisplayResult.this, "Category passed: " + selectedCategory,
    // Toast.LENGTH_LONG).show();

    final ListView lv = (ListView) findViewById(R.id.listViewItems);
    ArrayList<Promise> results = null;
    lv.setAdapter(new MyCustomArrayAdapter(this, results));
} // end onCreate

public ArrayList<Promise> GetPromises() {
    String table = "promises";
    ArrayList<Promise> promises = new ArrayList<Promise>();
    Promise prms = new Promise();
    SQLiteDatabase newDB;
    //SQLiteDatabase db = MyDBHandler.getReadableDatabase();

    ArrayList<Promise> results = null;
    try {
        MyDBHandler dbHandler = new MyDBHandler(this.getApplicationContext());
        newDB = dbHandler.getReadableDatabase();
        Cursor c = newDB.rawQuery("SELECT * FROM " + MyDBHandler.TABLE_NAME +
                " WHERE KEY_CATEGORY = ?", new String[]{selectedCategory});
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    String category = c.getString(c.getColumnIndex("KEY_CATEGORY"));
                    String book = c.getString(c.getColumnIndex("KEY_BOOK"));
                    String chapter = c.getString(c.getColumnIndex("KEY_CHAPTER"));
                    String verse = c.getString(c.getColumnIndex("KEY_VERSE"));
                    String word = c.getString(c.getColumnIndex("KEY_WORD"));
                    assert results != null;
                    results.add(category + book + chapter + verse + word);
                } while (c.moveToNext());
            } // end inner if
        } // end outer if
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(), "Could not create or open the database");
    } finally {

    }
    return results;
}

}

给出错误的行是:

 results.add(category + book + chapter + verse + word);

它表示添加ArrayList不能应用于java.lang.String。好的,我明白了 - 但我该如何解决?我已经尝试过灯泡的建议。我已经研究过,教程和其他SO答案确实使用了这种格式。我该怎么办才能修复它?谢谢!

3 个答案:

答案 0 :(得分:0)

因为您的ArrayList类型为“Promise”,所以您只能向其添加“Promise”类型的对象。试试这个:

results.add(new Promise(category, book, chapter, verse, word));

答案 1 :(得分:0)

结果类型是Promise,但您在列表中添加了String类型。

答案 2 :(得分:0)

在进入while循环创建

之前
  

的ArrayList&LT;无极&GT; results = new ArrayList&lt; Promise&gt;();

内部循环

  

承诺承诺=新承诺()
   promise.add(字符串)的
   results.add(诺)

每次在循环中创建promise = new Promise(),否则数组列表中将出现相同的对象 希望这有帮助