0

这是我的数据库代码,我想从我的数据库中检索名称并将其显示在 listview 中,该名称在下面发布为 profilelist.jar 的另一个 java 文件中。但我无法做到,请告诉我这段代码是否正确?

package a.vaccination;

import android.annotation.TargetApi;
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.os.Build;
import android.util.Log;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
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 if not exists"
            + DATABASE_TABLE
            + "(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();
                Log.i(DATABASE_NAME, "create");
            } 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);
        Log.i(DATABASE_NAME, "values added");
        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;
    }

    public void getReadableDatabase() {
        // TODO Auto-generated method stub
        Cursor cur = db
                .rawQuery("select rowid _id,* from DATABASE_TABLE", null);
    }
}

profilelist.java

package a.vaccination;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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);
    SimpleCursorAdapter adapter = null;
    Cursor c;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child);

        Button add = (Button) findViewById(R.id.button1);
        add.setOnClickListener(new OnClickListener() {

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

            }
        });


        db.open();
        Cursor c = db.getAllTitles();
        if (c.moveToFirst()) {
            do {
                DisplayRecord(c);
            } while (c.moveToNext());
        }
        db.close();

        // ---get a Record---
        db.open();
        c = db.getTitle(2);
        if (c.moveToFirst())
            DisplayRecord(c);
        else
            Toast.makeText(this, "No Assignments found", Toast.LENGTH_LONG)
                    .show();
        db.close();

    }

    public void DisplayRecord(Cursor c) {
        // TODO Auto-generated method stub
        Toast.makeText(
                this,
                "id: " + c.getString(0) + "\n" + "name: " + c.getString(1)
                        + "\n" + " dob: " + c.getString(2) + 
"contact"
                        + c.getString(3) + "email" + 
c.getString(4),

                Toast.LENGTH_SHORT).show();
        String[] columns = new String[] { datahandler.KEY_name };
        int[] to = new int[] { R.id.name };
        adapter = new SimpleCursorAdapter(this, R.layout.child, c, columns,
                to, 0);
        ListView names = (ListView) findViewById(R.id.listView1);
        names.setAdapter(adapter);
        names.setOnItemClickListener(this);

    }

我在 a.vaccination.datahandler.getTitle(datahandler.java:115) 和 a.vaccination.profilelist.onCreate(profilelist.java:67) java.lang.NoSuchMethodError: android.database.sqlite.SQLiteDatabaseb.query我在上面提供了 profilelist.java 和 datahandler.java

请帮助我,我严重卡在这里 @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO 自动生成的方法存根

        Intent i = new Intent(profilelist.this, vaccination.class);
        startActivity(i);
    }


}
4

4 回答 4

0

您想使用像SimpleCursorAdapter这样的 CursorAdapter 。您将列映射传递给视图 ID 以及它处理的绑定等。

于 2013-02-28T18:06:25.100 回答
0

我最近实现了这种类型的功能。基本上,您必须创建一个自定义 BaseAdapter 才能在 ListView 中显示您的数据。在适配器构造函数中,您传入包含数据的对象数组。我过去的教程是: http ://www.vogella.com/articles/AndroidListView/article.html 。希望这可以帮助!

于 2013-02-28T17:35:46.420 回答
0

要回答您的第一个和第二个问题:“不,此代码确实不好”。你真的必须考虑使用加载器。虽然您所写的内容可能在大多数情况下都有效,但您很可能会产生 Application Not Responding 错误,这些错误会突然终止您的应用程序。不要在 UI 线程上做这些事情。

有许多令人惊讶的 Android 编程。我真的建议你在把自己画到一个角落之前,先阅读一本关于如何做到这一点的标准书籍。

于 2013-02-28T17:57:50.983 回答
0

您需要创建 SimpleCursorAdapter 并将其附加到从您的数据库自定义查询选择返回的光标。然后将从光标返回的数据列映射到您的自定义视图以进行显示。要了解更多信息,请单击 此处,这为您的问题提供了正确的实施

于 2016-10-03T19:18:29.493 回答