這是我SQLBlog類getDataFunc就是我從數據庫foreach循環打破數據
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLBlog {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_BLOG = "persons_blog";
private static final String DATABASE_NAME = "blog";
private static final String DATABASE_TABLE = "admin_blog";
private static final int DATABASE_VERSION = 1;
private DBhelper ourhelper;
private final Context ourcontext;
private SQLiteDatabase ourdatabase;
private static class DBhelper extends SQLiteOpenHelper{
public DBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table "+ DATABASE_TABLE +" (" + KEY_ROWID + " Integer primary key autoincrement, "+
KEY_NAME +" text not null, "+
KEY_BLOG +" text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists "+ DATABASE_TABLE);
onCreate(db);
}
}
public SQLBlog(Context c){
ourcontext=c;
}
public SQLBlog open() throws SQLException{
ourhelper= new DBhelper(ourcontext);
ourdatabase= ourhelper.getWritableDatabase();
return this;
}
public void close(){
ourhelper.close();
}
public long createEntry(String name, String blog) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_BLOG, blog);
return ourdatabase.insert(DATABASE_TABLE, null, cv);
}
public String getDataFunc() {
// TODO Auto-generated method stub
String columns[]= new String[]{KEY_ROWID, KEY_NAME, KEY_BLOG};
Cursor c = ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result= "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iBlog = c.getColumnIndex(KEY_BLOG);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iBlog) + "\n";
}
return result;
}
}
我在這裏使用結果表明博客,我的用戶得到的結果的功能
public class SQLLiteView extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv =(TextView)findViewById(R.id.tvSQLinfo);
SQLBlog getdata = new SQLBlog(this);
getdata.open();
String data = getdata.getDataFunc();
getdata.close();
tv.setText(data);
}
}
但問題是我得到的數據作爲一個字符串,但我想更多的像動態顯示數據...
嗨你是如何:發佈傑克
和//意見將在這裏給...
如何從我的數據庫中的數據,並把它放在一個ListView是可點擊:發佈者妮可
//評論到這裏
我怎麼能實現這一點...需要幫助...我想我正在尋找一些類型的foreach循環像PHP ...它可以打破數據..但因爲我使用字符串作爲返回類型我不能得到它..
請提供有關的文章和評論你的數據庫結構,我認爲這將有助於。但很少有建議:你應該創建一個代表你的表的類,並使用列表視圖的自定義適配器來映射數據(更多關於適配器和列表視圖的信息:http://www.vogella.com/articles/AndroidListView/article.html) – NeeL
我發佈全班..檢查出來.. @ NeeL –