从现有的sqlite数据库中检索数据

时间:2015-01-13 01:54:14

标签: android sqlite listview

我希望将它存储在arraylist中,然后在listView中显示出来。有什么解决方案吗?我该如何设置适配器?如何将这两个链接起来并使用arrayList将其显示在listview中?

DBhelper.java

public ArrayList<String> getDataarray(){


    SQLiteQueryBuilder querybuilder=new SQLiteQueryBuilder();
    querybuilder.setTables(DATABASE_TABLES);

  String [] columns= new String[]{ KEY_ROWID,KEY_USERNAME,KEY_AGE,KEY_ClINIC,KEY_PHONE,KEY_EMAIL,KEY_DATESIGNUP
            ,KEY_CONDITIONID,KEY_DOCTORID,KEY_LOGINID,KEY_ACTIVITYNAME,KEY_NOTIFICATIONNAME,KEY_GROUPNAME,KEY_APPROVED
            };

    Cursor c= ourdatabase.query(DATABASE_TABLES, columns, null,null, null, null, null);
    String result="";

    ArrayList<String> resultarray = new ArrayList<String>();


    int iRow=c.getColumnIndex(KEY_ROWID);
    int iUserName=c.getColumnIndex(KEY_USERNAME);
    int iAge=c.getColumnIndex(KEY_AGE);
    int iClinic=c.getColumnIndex(KEY_ClINIC);
    int iPhone=c.getColumnIndex(KEY_PHONE);
    int iEmail=c.getColumnIndex(KEY_EMAIL);
    int iDateSignup=c.getColumnIndex(KEY_DATESIGNUP);
    int iConditionID=c.getColumnIndex(KEY_CONDITIONID);
    int iDoctorID=c.getColumnIndex(KEY_DOCTORID);
    int iLoginID=c.getColumnIndex(KEY_LOGINID);
    int iActivityName=c.getColumnIndex(KEY_ACTIVITYNAME);
    int iNotificationName=c.getColumnIndex(KEY_NOTIFICATIONNAME);
    int iGroupName=c.getColumnIndex(KEY_GROUPNAME);
    int iApproved=c.getColumnIndex(KEY_APPROVED);


     SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT value FROM " + DATABASE_TABLES, null);

        if(cursor.moveToFirst()) {
            do {
                resultarray.add(cursor.getString(cursor.getColumnIndex("value")));
            }while(cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return resultarray;

}

ProfileFragment.java这是我要在listView中检索出来的页面如何从数据库而不是数组中检索它

public View getView( int position ,View convertView,ViewGroup parent){

    LayoutInflater inflater= (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
    View row =inflater.inflate(R.layout.profile_list,parent,false);

    TextView myProfile=(TextView)row.findViewById(R.id.Profile);
    TextView myCondition=(TextView)row.findViewById(R.id.condition);

    myProfile.setText(ProfileArray[position]);
    myCondition.setText(resultarray[position]);
    return row;

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootview = inflater.inflate(R.layout.user,container,false);
    Resources res=getResources();
    Profile=res.getStringArray(R.array.Profile);
    //how can i change the line below to let it get the data from the database array? or is there any other method?
    condition=res.getStringArray(R.array.Profile_values);
    list.setAdapter(adapter);

    list=(ListView)rootview.findViewById(R.id.listView1);

     return  rootview;

    }

1 个答案:

答案 0 :(得分:0)

您想显示所有数据吗?

考虑将CursorLoaderSimpleCursorAdapter一起使用。一旦在后台创建活动,它将加载您的数据。在您的活动类中,只需实现接口LoaderManager.LoaderCallback,在onCreate中,只需使用getSupportLoaderManager().initLoader(LOADER_ID, null, this)初始化Loader即可 以下是具有良好示例http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html

的链接

修改

此代码用于onCreate

    // init DB
    db = new DB(this);
    db.open();

    // forming columns from DB to views
    String[] from = new String[] { KEY_ROWID, KEY_USERNAME, KEY_AGE...}; //array of columns from DB
    int[] to = new int[] { R.id.textviewId, R.id.textviewUserName, R.id.textviewAge }; //Array of of view components

    // create adapter
    scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
    ListView lvData = (ListView) findViewById(R.id.lvData); // listview where you want to display data
    lvData.setAdapter(scAdapter);
//init loader
getSupportLoaderManager().initLoader(0, null, this);

//implement these mothods with interface `LoaderManager.LoaderCallback<Cursor>`

    @Override
      public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
        return new MyCursorLoader(this, db);
      }

      @Override
      public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        scAdapter.swapCursor(cursor);
      }

      @Override
      public void onLoaderReset(Loader<Cursor> loader) {
      }

此代码必须适合您。

<强> EDIT2

static class MyCursorLoader extends CursorLoader {

DB db;

public MyCursorLoader(Context context, DB db) {
  super(context);
  this.db = db;
}

@Override
public Cursor loadInBackground() {
  return db.getAllData();
}

}

添加了一个细节。这是一个从db下载数据并返回填充游标的类。