package com.mydatabase;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class TippodraddActivity extends Activity implements OnClickListener
{
EditText tippodraddedtxt1 = null;
Button tippodraddbtn1 = null;
Button tippodraddbtn2 = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tippodradd);
tippodraddedtxt1 = (EditText)findViewById(R.id.tippodraddedtxt1);
tippodraddbtn1 = (Button)findViewById(R.id.tippodraddbtn1);
tippodraddbtn1.setOnClickListener(this);
tippodraddbtn2 = (Button)findViewById(R.id.tippodraddbtn2);
tippodraddbtn2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.tippodraddbtn1: {
DbOpenHelper dbOpenHelper = new DbOpenHelper(TippodraddActivity.this);
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DbOpenHelper.TIPPODR_NAZV, tippodraddedtxt1.getText().toString());
db.insert(DbOpenHelper.DATABASE_TABLE1,null,cv);
db.close();
tippodraddedtxt1.setText("");
}break;
case R.id.tippodraddbtn2: {
Intent intent = new Intent();
intent.setClass(this, TippodrrdActivity.class);
startActivity(intent);
}break;
default:
break;
}
}
}
Здравствуйте! Подскажите пожалуйста, как в моём варианте, без переделки кода, реализовать вывод. И лучше в виде таблицы, с русскими названиями колонок. Возможно ListView + TextView’ы.
Код:
package com.mydatabase;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbOpenHelper extends SQLiteOpenHelper
{
//Baza i tablici
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "TEACHERBASE";
public static final String DATABASE_TABLE1 = "Tippodrazdel";
public static final String KEY_ID = "_id";
//Polja dlja tablici Tippodrazdel
public static final String TIPPODR_NAZV = "nazv";
public DbOpenHelper(Context context)
{
super(context, DATABASE_NAME, null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//Sozdanie tablici Tippodrazdel
db.execSQL("CREATE TABLE " + DATABASE_TABLE1 + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TIPPODR_NAZV + " TEXT " +
");"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
onCreate(db);
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbOpenHelper extends SQLiteOpenHelper
{
//Baza i tablici
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "TEACHERBASE";
public static final String DATABASE_TABLE1 = "Tippodrazdel";
public static final String KEY_ID = "_id";
//Polja dlja tablici Tippodrazdel
public static final String TIPPODR_NAZV = "nazv";
public DbOpenHelper(Context context)
{
super(context, DATABASE_NAME, null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//Sozdanie tablici Tippodrazdel
db.execSQL("CREATE TABLE " + DATABASE_TABLE1 + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TIPPODR_NAZV + " TEXT " +
");"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
onCreate(db);
}
}
Код: