retriveing​​从exisint SQLite数据库数据 [英] retriveing data from exisint sqlite database

查看:134
本文介绍了retriveing​​从exisint SQLite数据库数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将其存储在数组列表,然后在ListView中显示出来。有什么解决办法呢?我该如何设置适配器?我怎么能连接这两和使用ArrayList中显示它变成一个ListView?

  DBhelper.java公众的ArrayList<串GT; getDataarray(){
    SQLiteQueryBuilder的QueryBuilder =新SQLiteQueryBuilder();
    querybuilder.setTables(DATABASE_TABLES);  的String [] =列新的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
            };    光标C = ourdatabase.query(DATABASE_TABLES,列,NULL,NULL,NULL,NULL,NULL);
    字符串结果=;    ArrayList的<串GT; resultarray =新的ArrayList<串GT;();
    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分贝= this.getReadableDatabase();
        光标光标= db.rawQuery(SELECT价值FROM+ DATABASE_TABLES,NULL);        如果(cursor.moveToFirst()){
            做{
                resultarray.add(cursor.getString(cursor.getColumnIndex(值)));
            }而(cursor.moveToNext());
        }        cursor.close();
        db.close();
        返回resultarray;}

ProfileFragment.java这是我想在ListView中检索出的网页我怎么可以从数据库中,而不是数组检索

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){    LayoutInflater吹气=(LayoutInflater)context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
    查看排= inflater.inflate(R.layout.profile_list,父母,假);    TextView的我的配置文件=(TextView中)row.findViewById(R.id.Profile);
    TextView的myCondition =(TextView中)row.findViewById(R.id.condition);    myProfile.setText(ProfileArray [位置]);
    myCondition.setText(resultarray [位置]);
    返回行; 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    查看rootview = inflater.inflate(R.layout.user,集装箱,FALSE);
    资源解析度= getResources();
    外形= res.getStringArray(R.array.Profile);
    //我如何改变该线下方,让它从数据库中获取数组中的数据?或者还有什么其他的方法?
    条件= res.getStringArray(R.array.Profile_values​​);
    list.setAdapter(适配器);    名单=(ListView控件)rootview.findViewById(R.id.listView1);     返回rootview;    }


解决方案

是否要显示所有数据?

考虑使用 CursorLoader SimpleCursorAdapter 。一旦活动在后台创建它会加载数据。而在你的活动类只实现接口 LoaderManager.LoaderCallback ,而在的onCreate 您只需用<$ C $初始化加载器C> getSupportLoaderManager()。initLoader(LOADER_ID,空,这一点)
下面是一个很好的例子链接 http://www.androiddesignpatterns.com/2012/ 07 /理解,loadermanager.html

编辑:

在此的onCreate code使用

  //初始化数据库
    DB =新的DB(本);
    db.open();    //从数据库形成列的意见
    的String [] =由新的String [] {KEY_ROWID,KEY_USERNAME,KEY_AGE ...}; //从数据库列数组
    INT []为= INT新[] {R.id.textviewId,R.id.textviewUserName,R.id.textviewAge}; //的视图组件阵列    //创建适配器
    scAdapter =新SimpleCursorAdapter(这一点,R.layout.item,空,发件人,收件人,0);
    ListView控件lvData =(ListView控件)findViewById(R.id.lvData); //要显示的数据列表视图
    lvData.setAdapter(scAdapter);
//初始化加载器
getSupportLoaderManager()initLoader(0,null,则此)。//实现这些mothods带接口`LoaderManager.LoaderCallback&LT;&光标GT;`    @覆盖
      公共装载机&LT;&光标GT; onCreateLoader(INT ID,捆绑BNDL){
        返回新MyCursorLoader(这一点,DB);
      }      @覆盖
      公共无效onLoadFinished(装载机&LT;&光标GT;装载机,光标光标){
        scAdapter.swapCursor(光标);
      }      @覆盖
      公共无效onLoaderReset(装载机&LT;&光标GT;装载机){
      }

这code必须为你的作品。

EDIT2

 静态类MyCursorLoader扩展CursorLoader {DB数据库;公共MyCursorLoader(上下文的背景下,DB DB){
  超级(上下文);
  this.db = DB;
}@覆盖
公共光标loadInBackground(){
  返回db.getAllData();
}

}

有一个细节补充。这是你从数据库下载数据并返回填充光标的类。

I wish to store it in arraylist and then display it out in listView. Is there any solution to it ? how do I set the adapter? how can I link this two up and display it into a listview using arrayList?

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 this is the page where i want to retrieve out at the listView how can i retrieve it from the database instead of array

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;

    }

解决方案

Do you want to display all of data?

Think about using CursorLoader with SimpleCursorAdapter. It will load your data once activity is created in background. And in your activity class just implement interface LoaderManager.LoaderCallback, and in onCreate you simply initialize Loader with getSupportLoaderManager().initLoader(LOADER_ID, null, this) Here is the link with good example http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html

EDIT:

This code use in 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) {
      }

This code must works for you.

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

}

One detail added. This is a class where you download your data from db and return filled cursor.

这篇关于retriveing​​从exisint SQLite数据库数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆