我创建了一个数据库并运行了一个查询,该查询向结果中添加了一些元素。我希望我拥有的按钮能够在单击时做出响应并将结果显示给新的黑色活动。我将发布我当前的课程。
对不起,我对 android 编程很陌生。:)
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;
}
}