データベースを作成し、いくつかの要素を結果に追加するクエリを実行しました。私が持っているボタンをクリックして応答し、結果を新しい黒いアクティビティに表示する必要があります。現在のクラスを掲載します。
すべて申し訳ありませんが、私はアンドロイドプログラミングに非常に慣れていません。:)
package com.example.pota;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class createDatabase extends ListActivity{
private final String DATABASE_NAME = "potaDB";
private final String DATABASE_TABLE = "olaPota";
private final String DATABASE_TABLE2 = "favorites";
private final int DATABASE_VERSION = 1;
private SQLiteDatabase potaDB=null;;
//Kaleitai otan dimiourgeitai to activiy
public void onCreate(Bundle icicle, Context ctx){
super.onCreate(icicle);
ArrayList <String> results = new ArrayList<String>();
try {
//Dhmiourgei ti vasi an den uparxei alliws tin anoigei
this.openOrCreateDatabase(DATABASE_NAME, DATABASE_VERSION, null);
//Dhmiourgei ena table sti vasi me ta ekseis paidia
potaDB.execSQL("CREATE TABLE IF NOT EXISTS" + DATABASE_TABLE + "(id INT PRIMARY KEY AUTOINCREMENT, name VARCHAR, category VARCHAR, info TEXT, difficulty INT);", null);
//Vazei eggrafes ston pinaka
potaDB.execSQL("INSERT INTO" + DATABASE_TABLE + "(name, category, info, difficulty)" + "VALUES ('Screwdriver', 'Pota', 'i klassiki vodka lemoni', 5);");
//Ena query pou tha mas gurisei ta stoixeia tou pinaka
Cursor c = potaDB.rawQuery("SELECT *" + "FROM" + DATABASE_TABLE,null);
//Pernoume ta dedomena tou pinaka pou xreiazomaste
int name1=c.getColumnIndex("name");
int category1=c.getColumnIndex("category");
int info1=c.getColumnIndex("info");
int difficulty1=c.getColumnIndex("difficulty");
//Pernei ta dedomena ekei pou deixnei o Cursor
String name = c.getString(name1);
String category = c.getString(category1);
String info = c.getString(info1);
int difficulty = c.getInt(difficulty1);
//Prosthetei ta trexontai dedomena sto results
results.add("Onoma Potou: " + name + "Katigoria Potou: " + category + "Suntagi: "+ info + "Duskolia: " + difficulty);
} catch(Exception e) {
System.out.println(e);
}finally {
if (potaDB != null)
potaDB.close();
}
}
}
そして主な活動:
package com.example.pota;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}