从sqlite数据库检索特定属性到listview

时间:2013-02-27 09:41:37

标签: android

我有这个数据库文件,如下所示,还有活动文件,我想在其中显示存储的名称

package a.vaccination;

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;
import android.util.Log;

public class datahandler {

public static final String KEY_ROWID = "id";
public static final String KEY_name = "name";
public static final String KEY_dob = "dob";
public static final String KEY_contact = "contact";
public static final String KEY_email = "email";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "profile";
private static final String DATABASE_TABLE = "baby";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE = "create table baby(id integer 
primary key autoincrement, "
        + "name VARCHAR not null, dob date, contact VARCHAR, email  
VARCHAR);";
private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public datahandler(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

protected static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            db.execSQL(DATABASE_CREATE);
            // db = DBHelper.getWritableDatabase();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old  
data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}

// ---opens the database---
public datahandler open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}

// ---insert a record into the database---
public long insertRecord(String name, String dob, String contact,
        String email) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_name, name);
    initialValues.put(KEY_dob, dob);
    initialValues.put(KEY_contact, contact);
    initialValues.put(KEY_email, email);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// ---updates a record---
public boolean updateRecord(long rowId, String name, String dob,
        String contact, String email) {
    ContentValues args = new ContentValues();
    args.put(KEY_name, name);
    args.put(KEY_dob, dob);
    args.put(KEY_contact, contact);
    args.put(KEY_email, email);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_name,
KEY_dob,
KEY_contact,
KEY_email},
null,
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_name,
KEY_email,
KEY_dob,KEY_contact
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}

和活动文件,我想要检索名称表单数据库并在列表视图中显示我做了一些编码,但我卡住了PLZ帮助我

package a.vaccination;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

//this shows profies of baby and option of add new one
public class profilelist extends Activity implements OnItemClickListener {

datahandler db = new datahandler(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.child);
    String nbaby[] = null;

    display();

    Button add = (Button) findViewById(R.id.button1);
    ListView names = (ListView) findViewById(R.id.listView1);

    ArrayAdapter<String> ad = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, nbaby);

    names.setAdapter(ad);
    names.setOnItemClickListener(this);

    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // navigate to create profile
            Intent it = new Intent(profilelist.this, 
createprofile.class);
            startActivity(it);

        }
    });

}

private void display() {
    // TODO Auto-generated method stub
    db.open();

    try {
        Cursor c = db.getAllTitles();
        if (c.moveToFirst()) {
            do {
                // System.out.println("bool2");
                DisplayTitle(c);
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        // System.out.println(e);
    }
    db.close();
}

private void DisplayTitle(Cursor c) {
    // TODO Auto-generated method stub
//  System.out.println("bool");
    /*Toast.makeText(this,
    "id: " + c.getString(0) + "\n" +
    "ISBN: " + c.getString(1) + "\n" +
    "TITLE: " + c.getString(2) + "\n" +
    "PUBLISHER: " + c.getString(3),
    Toast.LENGTH_LONG).show();*/    

    nbaby[]=c.getString(0);


}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}

}

2 个答案:

答案 0 :(得分:0)

试试这个,

if(cursor != null){
    cursor.moveToFirst();
    String label = cursor.getString(1);
}

答案 1 :(得分:0)

尝试以下代码: 如果尚未将任何数据插入数据库,则首先插入数据,然后将其绑定到列表视图中。为了方便起见,我试图在onCreate中插入一些记录。如果您已插入记录,则从onCreate()中删除插入代码。

我已将光标的所有值存储到ArrayList<String>并将其绑定到列表视图中。您可以在下面的代码中查看。它的工作完美。

  public class profilelist extends Activity implements OnItemClickListener
    {
datahandler db = new datahandler(this);
String nbaby[] = null;
ArrayList<String> m_arr=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

           db.open();
    db.insertRecord("Texst", "10-1-1989","gaskudfgaskf", "teste@yahoo.com");
    db.close(); 

    display();
    Button add = (Button) findViewById(R.id.button1);
    ListView names = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> ad =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_arr);
    names.setAdapter(ad);
    names.setOnItemClickListener(this);
    add.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // navigate to create profile
        Intent it = new Intent(profilelist.this, createprofile.class);
            startActivity(it);
        }
    });
      }
private void display()
{
    // TODO Auto-generated method stub
    db.open();
    m_arr=new ArrayList<String>();
    try
    {
        Cursor c = db.getAllTitles();
            c.moveToFirst();
            do
            {
                m_arr.add(c.getString(1));
                 System.out.println("bool2"+c.getString(1).toString());
                } while (c.moveToNext());
    }
    catch (Exception e)
    {
        // System.out.println(e);
    }
    db.close();
}