N
N
Neo_hacker2015-07-30 16:32:47
Android
Neo_hacker, 2015-07-30 16:32:47

I'm trying to show my information with SQLite in a new activity. But what is wrong?

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;
    }

}

This is an activi-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 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>

This is the activity 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());
        }
    }

}

Thank you. Can you suggest how to convert it from Textview to Listview.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2015-07-30
@onepavel

0. Place ListView instead of TextView
1. create a class for data from the database
2. create an adapter for ListView inheriting from ArrayAdapter
3. create a list before going to
the database 4. push them into the list creating objects p.1
5 in the data reading cycle from the database . pass the list to the adapter
6. call notifyDataSetChanged() on the adapter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question