0

I have to show saved sqlite data on a listview. But only one column's data is enough to show.Here is my codes. I want to get "tarih" column's data.

public class TarihDataSource {

private SQLiteDatabase database;
private DatabaseOlusturma dbOlustur;
private String []allColumns = {"tarih","gunluknotu","resim"};

public TarihDataSource(Context context) {
    dbOlustur = new DatabaseOlusturma(context);
}

public void open() throws SQLException {
    database = dbOlustur.getReadableDatabase();
}

public void close() {
    dbOlustur.close();
}

public List<TarihListHelper> getAllTarih() {
    List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
    Cursor cursor = database.query("gunluker", allColumns, null, null,
            null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        TarihListHelper comment = cursorToComment(cursor);
        tarihlistesi.add(comment);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return tarihlistesi;
}

private TarihListHelper cursorToComment(Cursor cursor) {
    TarihListHelper comment = new TarihListHelper();
    comment.setTarih(cursor.getString(1));
    return comment;
  }

}

What is the wrong in this codes?

3 Answers 3

1

This code should help you.

public List<TarihListHelper> getAllTarih()
{       
    List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();

    Cursor cursor = database.query(DB_NAME, columns, null, null, null, null, null);

    while(cursor.moveToNext())
    {
        TarihListHelper tarihlistesi= cursorToComment(cursor);
        tarihlistesi.add(comment);


    }

    cursor.close();
    return tarihlistesi;
}

Where columns is the String[] which contains the names of all the columns in your database table.

This way you will get all the columns, then you can select from which column do want to read data.

Hope this helps.

Sign up to request clarification or add additional context in comments.

8 Comments

TarihListHelper data = cursor.getString(c.getColumnIndex(DB_COMMENT_COL)); comments.add(data); // the List you will pass to ListView for displaying data I did'nt understand this row.cursor.getString returns String type.But data's type is TarihListeHelper
TarihListHelper data = cursor.getType(cursor.getColumnIndex("tarih")); This is giving error.
Would you care to post the logcat?
No no.it is not accepted. cursor.getType(...) returns int. But We have TarihListHelper type.
type cast the data returned by getType to TarihListHelper
|
0

You simply need an Adapter to your list. In your case i think that a SimpleCursorAdapter is the best.

Check this official page and this example.

Comments

0
public class TarihDataSource {

private SQLiteDatabase database;
private DatabaseOlusturma dbOlustur;
private final Context context;
private String []allColumns = {"tarih","gunluknotu","resim"};

public TarihDataSource(Context _context) {
    context = _context;
    dbOlustur = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

public void open() throws SQLException {
    database = dbOlustur.getReadableDatabase();
}

public void close() {
    dbOlustur.close();
}

public List<TarihListHelper> getAllTarih() {
    List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
    Cursor cursor = database.query("gunluker", allColumns, null, null,
            null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        TarihListHelper comment = cursorToComment(cursor);
        tarihlistesi.add(comment);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return tarihlistesi;
}

private TarihListHelper cursorToComment(Cursor cursor) {
    TarihListHelper comment = new TarihListHelper();
    comment.setTarih(cursor.getString(1));
    return comment;
  }

}

Try this.

Comments

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.