Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

android - retriveing data from exisint sqlite database

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;

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...