Класс базы данных-
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;
}
}
Это активити-
<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>
Это java-класс активити -
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());
}
}
}
Спасибо. Можете подсказать как его перевести с Textview в Listview.