我很难找到从数据库填充列表视图的方法。
这是我的代码:
SQLDatabase.java
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.SQLiteOpenHelper;
public class SQLDatabase {
public static final String KEY_MOVENAME = "movename";
public static final String KEY_MOVEID = "_moveid";
public static final String KEY_MOVEDATE = "movedate";
private static final String DATABASE_NAME = "mymovingfriend";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "movingname";
public static final String CREATE_TABLE = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_MOVEID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_MOVEDATE + " TEXT NOT NULL, " + KEY_MOVENAME + " TEXT NOT NULL);";
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);
}
@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 SQLDatabase(Context c){
ourContext = c;
}
public SQLDatabase open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createMove(String smovename){
ContentValues cv = new ContentValues();
cv.put(KEY_MOVENAME, smovename);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getMove(){
String[] column = new String[]{KEY_MOVENAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, column, null, null, null, null, null);
String result = "";
int iMove = c.getColumnIndex(KEY_MOVENAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iMove) + "\n";
}
c.close();
return result;
}
}
这里是我的listview类,基本上这就是listview它与数据库的单独类。
ListMovingNames.java
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class ListMovingNames extends ListActivity {
ListView MoveList;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selectorcreatemove);
MoveList = (ListView) findViewById(R.id.lvMoveItems);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
}
我想要做的是填充列表视图,并在从列表中选择一个项目时执行特定任务。
答案 0 :(得分:1)
使用CursorAdapter执行此操作。单击以查看CursorAdapter用法 http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
答案 1 :(得分:0)
public List<App_List> getAppList(String appclass){
List<App_List> App_ListView = new ArrayList<App_List>();
String AppTable_Name="favorite_apps";
String AppList_Query="SELECT * FROM " + AppTable_Name +" WHERE appclass = "+ "'"+appclass+"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(AppList_Query, null);
if (cursor.moveToFirst()){
do{
App_List mAppList = new App_List();
if(cursor.getString(7).equalsIgnoreCase("local")){
// if application is from local server also take the version
mAppList.setVersion(cursor.getString(0));
}
mAppList.setApp_Name(cursor.getString(3));
mAppList.setApp_Description(cursor.getString(4));
mAppList.setApp_Link(cursor.getString(6));
mAppList.setPlace(cursor.getString(7));//place is added here
mAppList.setApp_Pkg(cursor.getString(1));
App_ListView.add(mAppList);
}while (cursor.moveToNext());
}
db.close();
return App_ListView;
我的班级存储值
public class App_List {
private String App_Name;
private String App_Description;
private String App_Link;
private String App_Pkg;
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
private String Version;
private String place;
public String getVersion() {
return Version;
}
public void setVersion(String version) {
Version = version;
}
public String getApp_Name() {
return App_Name;
}
public void setApp_Name(String app_Name) {
App_Name = app_Name;
}
public String getApp_Description() {
return App_Description;
}
public void setApp_Description(String app_Description) {
App_Description = app_Description;
}
public String getApp_Link() {
return App_Link;
}
public void setApp_Link(String app_Link) {
App_Link = app_Link;
}
public String getApp_Pkg() {
return App_Pkg;
}
public void setApp_Pkg(String app_Pkg) {
App_Pkg = app_Pkg;
}
这里我将数据填充到列表视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());