2

我想知道是否有人可以告诉我我的方式的错误——我已经为这个问题苦苦挣扎了两天,并意识到这一定是初始化变量的基本错误,但是……这反映了我的 java 知识水平.

我在一个分隔字符串上得到一个数据库结果,其中每个段都附加了“null”。看来无论我如何更改初始化......好吧,两天!

我在类标题区域声明以下内容:

private String strListContent;
private SQLiteDatabase database;
private DatabaseHelper helper2 = new DatabaseHelper(this);
private static final String fields[] = { "_id", "listTitle", "listType",
        "listContent", "dateCreated", "dateModified" };
private ArrayList<String> textArray = new ArrayList<String>();
private ArrayList<Integer> imageArray = new ArrayList<Integer>();

然后将我的项目连接起来

final ImageButton addItem = (ImageButton) findViewById(R.id.btnToAddItem);
addItem.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
    itemEdit = (EditText) findViewById(R.id.editTextItem);

    if (itemEdit.getText().toString().equals("")) {
        showToastMessage("Please enter an item to add...");

    } else {

        String newListItem = itemEdit.getText().toString();
        strListContent += newListItem + "|~|";

...
}}}

我正在使用以下基本 SQLiteOpenHelper:

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String KEY_ID = "_id";

public DatabaseHelper(Context context) {
    super(context, "Cursor", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS list_data ("
            + KEY_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, listTitle TEXT, listType TEXT,    listContent TEXT, dateCreated TEXT, dateModified TEXT)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Steps to upgrade the database for the new version ...
}
}

要这样插入值:

ImageButton saveAndBack = (ImageButton) findViewById(R.id.btnSaveBack);
saveAndBack.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        String title = null;
        String listContent = null;
        Calendar javaCalendar = null;

        title = titleEdit.getText().toString();
        title = (title=="" || title==null)?"Untitled List":title;
        strListContent = (strListContent=="" || strListContent==null)?"No Items|~|":strListContent;
        listContent = strListContent;
        String type = "R"; //"Regular List"

        javaCalendar = Calendar.getInstance();
        String currentDate = javaCalendar.get(Calendar.MONTH) + "/" +   (javaCalendar.get(Calendar.DATE) + 1) + "/" + javaCalendar.get(Calendar.YEAR);

        database = helper2.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("listTitle", title);
        values.put("listType", type);
        values.put("listContent", listContent);
        values.put("dateCreated", currentDate);
        values.put("dateModified", currentDate);

        database.insert("list_data", null, values);

        Intent i = new Intent(RegularList.this, ActivityMain.class);
        startActivity(i);

    }
});


}
//
//End of OnCreate(){}
//

然后,当我从另一个活动中检索时:

DatabaseHelper helper = new DatabaseHelper(this);
database = helper.getWritableDatabase();

    Cursor data = database.query("list_data", fields, null, null, null,
            null, null);
    Integer tindex = data.getColumnIndex("listTitle");
    Integer iindex = data.getColumnIndex("listType");
    Integer cindex = data.getColumnIndex("listContent");

    itemCount = 0;
    for (data.moveToFirst(); !data.isAfterLast(); data.moveToNext()) {
        showToastMessage(data.getString(cindex));
        titleArrayList.add(data.getString(tindex));

        if (data.getString(iindex) == "R") {
            imageArrayList.add(R.drawable.listview_regular);
        } else if (data.getString(iindex) == "L") {
            imageArrayList.add(R.drawable.listview_location);
        } else {
            imageArrayList.add(R.drawable.listview_regular);
        }

        itemCount++;

    }

    data.close();

...

我可以在 toast 消息中看到分隔字符串中的每个项目的前面都附加了“null”......其他值都很好。我希望这不是太冗长,但是……有什么建议吗?谢谢!

4

1 回答 1

2

在我看来,您可能只是strListContent在首次附加到字符串之前没有初始化字符串:

strListContent += newListItem + "|~|";

当你这样做时,你会在你试图附加的值前面得到一个“null”前缀,就像你观察到的那样。

也许你可以在声明中初始化:

private String strListContent = "";
于 2013-03-26T23:54:18.110 回答