简体   繁体   中英

Android & SQLite : how to display the multiple column value

i hope you guys can help to solve my problem.

my problem is cant retrieve the multiple value of column. In my database contain 4 columns (id, namaStation, ticket, masa). right now the program only displayed value of namaStation. It mybe because of the toString() method in Comment class because its only return 1 value (namaStation). can help me??

This is my program. ##

1. Call the query.

`public void onClick(View view) {

    @SuppressWarnings("unchecked")
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    Comment comment = null;

    switch (view.getId()) {
    case R.id.add:

        List<Comment> values = datasource.query("Kelana Jaya");

        adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

        break;`

2. The query.

    @SuppressWarnings("static-access")
public List<Comment> query(String namaStation){

    //List<Comment> comments = new ArrayList<Comment>();
database = dbHelper.getReadableDatabase();
    List<Comment> comments = new ArrayList<Comment>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns,  MySQLiteHelper.COLUMN_namaStation+"=?",new String[]{namaStation}, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
    //  Comment comment = cursorToComment(cursor);

        Comment comment = new Comment();
        comment.setId(cursor.getLong(0));
        comment.setNamaStation(cursor.getString(1));
        comments.add(comment);
        comment.setTicket(cursor.getString(2));
        comments.add(comment);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return comments;

3. The Comment class

public class Comment {
private long id;
public String namaStation;
public String ticket;
public String masa;
public String comment;
public int x;


public Comment(){



}
public Comment(long id, String namaStation, String ticket, String masa){

    this.id=id;
    this.namaStation=namaStation;
    this.ticket=ticket;
    this.masa=masa;

}
public long getId() {
    return id;
}

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

}

public String getNamaStation() {
    return namaStation;

}

public void setNamaStation(String comment) {
    this.namaStation = comment;


}


public String getTicket() {
    return ticket;
}

public void setTicket(String comment) {
    this.ticket = comment;

}

public String getMasa() {
    return masa;
}

public void setMasa(String comment) {
    this.masa= comment;
}
// Will be used by the ArrayAdapter in the ListView
public String toString() {

    return ticket;


}

}

The display (the layout is not in proper way because im not setup its yet.. :) )

the display

You add the comment twice to the list

    comments.add(comment);
    comment.setTicket(cursor.getString(1));
    comments.add(comment);

sidenote: you can simplify

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    // do stuff
    cursor.moveToNext();
}

to this

while (cursor.moveToNext()) {
    // do stuff
}

And the actual Problem that you see only the name is that you use the wrong Adapter. You need to create your own. Either based on ArrayAdapter if there is a reason to convert your query result into an array - or directly by using a CursorAdapter .

here is an example you can use.

You should do your own ArrayAdapter. You could supply an own layout for your items.

So you could place any column where you want.

Just to join all fields in toString is not "high tech". It may be sufficient in simple cases

May the following link is helpful http://www.vogella.de/articles/AndroidListView/article.html#listview_adapterintro

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM