【问题标题】:populate listview from database of a separate class从单独类的数据库中填充列表视图
【发布时间】:2012-09-17 06:30:06
【问题描述】:

我很难找到从数据库中填充列表视图的方法。

这是我的代码:

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;
    }

}

这是我的列表视图类,基本上这是列表视图与数据库分开的类。

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);
    }

}

我想要做的是填充列表视图并在从列表中选择一个项目时执行特定任务。

【问题讨论】:

    标签: android android-listview android-sqlite


    【解决方案1】:

    使用 CursorAdapter 来执行此操作。点击查看 CursorAdapter 的使用 http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/

    【讨论】:

      【解决方案2】:

      好的,一种方法是定义用于存储数据库数据的变量,并在将数据填充到列表视图时使用此变量。这里我正在读取数据库并将值存储在其他变量中。

                   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());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多