如何设置 ListView 以使用游标反映 SQLiteDatabase(需要代码示例)

发布于 2024-10-14 05:55:01 字数 5049 浏览 2 评论 0原文

我需要一个代码示例;我经验不是很丰富。

我需要一个 ListView 来反映使用游标的 SQLiteDatabase。这是我的 main.java:

package com.shoppinglist;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ShoppingList extends ListActivity {
    private SQLiteDatabase db;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            additem.setContentView(R.layout.maindialog);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.findViewById(R.id.ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String text = et.getText().toString();
                    additem.dismiss();
                    et.setText("");
                }
            });
       }
        });
    }
}

我的 DataHelper.java:

package com.shoppinglist;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

    private static final String DATABASE_NAME = "items.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "table1";

    private Context context;
    private SQLiteDatabase db;

    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into "
        + TABLE_NAME + "(name) values (?)";

    public DataHelper(Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase();
        this.insertStmt = this.db.compileStatement(INSERT);
        }

        public long insert(String name) {
            this.insertStmt.bindString(1, name);
            return this.insertStmt.executeInsert();
        }

        public void deleteAll() {
            this.db.delete(TABLE_NAME, null, null);
        }

        public List<String> selectAll() {
            List<String> list = new ArrayList<String>();
            Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
                    null, null, null, null, "name desc");
            if (cursor.moveToFirst()) {
                do {
                    list.add(cursor.getString(0));
                } while (cursor.moveToNext());
            }
            if (cursor !=null && !cursor.isClosed()) {
                cursor.close();
            }
            return list;
        }

        private static class OpenHelper extends SQLiteOpenHelper {

            OpenHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
              public void onCreate(SQLiteDatabase db) {
                db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT)");
              }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Log.w("Example", "Upgrading database, this will drop tables and recreate.");
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                onCreate(db);
            }

            @SuppressWarnings("unused")
            public void doInsert(String name) {
                final SQLiteDatabase db = getWritableDatabase();
                SQLiteStatement insert_stmt = null;
                try {
                    insert_stmt = db.compileStatement(INSERT);
                    insert_stmt.bindString(0, name);
                    insert_stmt.executeInsert();
                }
                finally {
                    if (insert_stmt !=null) insert_stmt.close();
                }
            }
        }
}

I will need a code example; I'm not very experienced.

I need a ListView to reflect an SQLiteDatabase using a cursor. Here's my main.java:

package com.shoppinglist;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ShoppingList extends ListActivity {
    private SQLiteDatabase db;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            additem.setContentView(R.layout.maindialog);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.findViewById(R.id.ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String text = et.getText().toString();
                    additem.dismiss();
                    et.setText("");
                }
            });
       }
        });
    }
}

My DataHelper.java:

package com.shoppinglist;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

    private static final String DATABASE_NAME = "items.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "table1";

    private Context context;
    private SQLiteDatabase db;

    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into "
        + TABLE_NAME + "(name) values (?)";

    public DataHelper(Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase();
        this.insertStmt = this.db.compileStatement(INSERT);
        }

        public long insert(String name) {
            this.insertStmt.bindString(1, name);
            return this.insertStmt.executeInsert();
        }

        public void deleteAll() {
            this.db.delete(TABLE_NAME, null, null);
        }

        public List<String> selectAll() {
            List<String> list = new ArrayList<String>();
            Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
                    null, null, null, null, "name desc");
            if (cursor.moveToFirst()) {
                do {
                    list.add(cursor.getString(0));
                } while (cursor.moveToNext());
            }
            if (cursor !=null && !cursor.isClosed()) {
                cursor.close();
            }
            return list;
        }

        private static class OpenHelper extends SQLiteOpenHelper {

            OpenHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
              public void onCreate(SQLiteDatabase db) {
                db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT)");
              }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Log.w("Example", "Upgrading database, this will drop tables and recreate.");
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                onCreate(db);
            }

            @SuppressWarnings("unused")
            public void doInsert(String name) {
                final SQLiteDatabase db = getWritableDatabase();
                SQLiteStatement insert_stmt = null;
                try {
                    insert_stmt = db.compileStatement(INSERT);
                    insert_stmt.bindString(0, name);
                    insert_stmt.executeInsert();
                }
                finally {
                    if (insert_stmt !=null) insert_stmt.close();
                }
            }
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-10-21 05:55:01

您需要一个游标数据适配器来填充数据。 开发人员网站有一些您应该开始的内容。

此外,我的 Android-Binding 支持此类映射,无需显式实现一个适配器。

You need a cursor data adapter to fill the data. The developer's site has something you should start with.

Moreover, my Android-Binding supports such kind of mapping, without explicitly implementing an adapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文