我是android的新手,我遇到了问题
这是我的主要类,只需插入数据并在Logcat中显示数据。我想在新活动中显示ListView中的数据。现在我没有得到如何从光标获取数据并在ListView上显示它。我做了view.xml
& view.java
显示数据,但我不知道如何显示该活动的数据。我应该制作适配器类,cursoradapter或flater等(我想知道接下来的步骤)。
提前致谢:)
主要类
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
database_delete();
database_add();
}
public void database_add() {
try{
Database_handler db = new Database_handler(this);
Log.d("Insert: ", "Inserting ..");
db.addData(new get_set(131,"A-Lucy")) ;
db.addData(new get_set(132,"A-kk")) ;
db.addData(new get_set(133,"A_Tweety")) ;
db.addData(new get_set(134,"A-Naruto")) ;
Log.d("Reading: ", "Reading all contacts..");
List<get_set> getSetList = db.getalldata();
for(get_set set : getSetList){
String log = "CODEE: " + set.getCode() + ", Description: " + set.getDescr();
Log.d("CODE: ", log);
}
这是我的数据库处理程序类的一些代码,具有插入,检索数据的功能。
数据库处理程序类
public class Database_handler extends SQLiteOpenHelper {
private static final int DATABASE_Version = 1;
private static final String DATABASE_Name = "android_db";
private static final String TABLE_Name = "all_data";
private static final String Column_Name_code = "code";
private static final String Column_name_desc = "description";
private SQLiteDatabase db;
Database_handler(Context cont){
super(cont,DATABASE_Name,null,DATABASE_Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_ANDRO = " CREATE TABLE " + TABLE_Name + "(" + Column_Name_code + " INTEGER PRIMARY KEY , "
+ Column_name_desc + " TEXT )";
db.execSQL(CREATE_TABLE_ANDRO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int Older_version, int new_version) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name );
onCreate(db);
}
// Inserting record
public void addData (get_set getSet){
SQLiteDatabase db = this.getReadableDatabase();
ContentValues cv = new ContentValues();
cv.put (Column_Name_code, getSet.getCode());
cv.put(Column_name_desc, getSet.getDescr());
db.insert(TABLE_Name,null,cv);
db.close();
}
Retrieving data
public List<get_set> getalldata(){
List<get_set> data_list = new ArrayList<get_set>();
String select_query = "SELECT * FROM " + TABLE_Name;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery(select_query,null);
if (cur.moveToFirst()){
do {
get_set getsetObj = new get_set();
getsetObj.setCode(Integer.parseInt(cur.getString(0)));
getsetObj.setDescr(cur.getString(1));
data_list.add(getsetObj);
} while (cur.moveToNext());
}
return data_list;
}
答案 0 :(得分:1)
在database_add()函数中,将数据插入数据库后,将一个intent调用到下一个Activity。在该新活动中,调用getalldata()函数。如果您已经通过列表返回,请使用数组适配器列表视图,使用ArrayList的String类型,遵循以下教程。它会像魅力一样工作。
http://www.vogella.com/articles/AndroidListView/article.html
而且,我建议你在AsyncTask中包含db.getalldata()函数,因为你不想在主线程中进行繁重的数据库事务。