mercredi 29 juillet 2015

I have no idea, why SQLite does not want to show

My data must show in new activity. I had a code that works, when I add and see what I added in one activity. It is my database class

    package com.nurislomturaev.sqlitetutorial;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by nurislom on 7/24/15.
 */
public class MyDatabase extends SQLiteOpenHelper{

    SQLiteDatabase db;
    public MyDatabase(Context context) {
        super(context, "tasks", null, 1);
        db = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE tasks(taskdb TEXT, datedb TEXT)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insert(String taskdb, String datedb){
        String sql = "INSERT INTO tasks(taskdb, datedb) VALUES('"+taskdb+"','"+datedb+"')";
        db.execSQL(sql);
    }

    public Cursor select(){
        String sql = "SELECT * FROM tasks";
        Cursor c =db.rawQuery(sql, null);
        return c;
    }

}

It is my activity, where I must show data (xml)

    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.nurislomturaev.sqlitetutorial.Showtasks">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showTasks"/>

</RelativeLayout>

It is java class

   package com.nurislomturaev.sqlitetutorial;

import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class Showtasks extends ActionBarActivity {

    final MyDatabase db = new MyDatabase(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showtasks);

        {
            Cursor cur = db.select();
            StringBuilder sb = new StringBuilder();
            while (cur.moveToNext()) {
                String taskdb = cur.getString(cur.getColumnIndex("taskdb"));
                String datedb = cur.getString(cur.getColumnIndex("datedb"));
                sb.append("Task: " + taskdb);
                sb.append(" | ");
                sb.append("Date: " + datedb);
                sb.append(" | ");
            }

            TextView showTasks = (TextView) findViewById(R.id.showTasks);
            showTasks.setText(sb.toString());
        }
    }

}

And how can I show it on the listview, instead of textview?

Aucun commentaire:

Enregistrer un commentaire