2

I wonder if someone could show me the error of my ways--I've been struggling with this issue for two days, and realize it must be a fundamental error of initializing variables, but...that reflects the level of my java knowledge.

I'm getting a database result on a delimited string wherein each of the segments has "null" appended to it. It seems that no matter how I change the initialization...well, two days!

I'm declaring the following in the class heading area:

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>();

Then concatenating my items in

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 + "|~|";

...
}}}

I'm using the following bare-bones 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 ...
}
}

To insert the values as so:

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(){}
//

Then, when I retrieve from another activity:

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();

...

I can see in the toast message that each item from the delimited string has "null" appended to the front of it...the other values are fine. I hope this hasn't been too verbose, but...any recommendations? Thanks!

1 Answer 1

2

To me it looks like you may have simply not initialised the String strListContent before you first append to it with:

strListContent += newListItem + "|~|";

When you do that, you'll get a "null" prefixed in front of the value you are trying to append, just as you observe.

Perhaps you can just initialise in the declaration:

private String strListContent = "";
Sign up to request clarification or add additional context in comments.

2 Comments

I realize it sounds ridiculous, but could you show me how and where I should do that? I've tried many ways and none seems to work...pretend I don't know anything about java, which isn't too far off base! Thanks
Omg...I could swear I had it that way at one point...what can I say? Thanks for the help, that was all it was! Where are the emoticons to show red cheeks?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.