草庐IT

Android:带 CheckBox 的 ListView,从 SQLite 数据库填充不太工作

coder 2023-07-20 原文

与此处的其他几篇文章一样,我正在尝试创建一个包含每行复选框的 ListView,并使用 SQLite 数据库来存储选择的当前状态。

http://appfulcrum.com/?p=351 的例子开始, 它并没有按原样工作,我创建了一个简单的应用程序来创建数据库,用 20 个项目填充它,并显示列表。

它成功地检索了状态并存储了选择的状态。

但是,如果我更改它、滚动到列表的另一端并向后滚动,它不会正确显示 CheckBox 状态。例如如果我选择第一个 CheckBox,滚动到底部,然后返回顶部,则不再设置 CheckBox。这是在 Android 2.1 三星手机上运行的。

如果我返回主屏幕,返回列表,CheckBox 设置正确,因此数据库确实已更新。

该示例扩展了 SimpleCursorAdapter,并且 getView() 根据表中选择列的值调用 setChecked() 并根据表中的选择列的值酌情设置为 true 或 false。

以下是所有来源。

如果有人告诉我,“呃,这是你的问题......”,我当然会很高兴

CustomListViewDB.java

// src/CustomListViewDB.java
package com.appfulcrum.blog.examples.listviewcustomdb;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class CustomListViewDB extends ListActivity {

    private ListView mainListView = null;
    CustomSqlCursorAdapter adapter = null;
    private SqlHelper dbHelper = null;
    private Cursor currentCursor = null;

    private ListView listView = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple);

        if (this.dbHelper == null) {
            this.dbHelper = new SqlHelper(this);

        }

        listView = getListView();
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //listView.setClickable(true);

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

            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),
                        " You clicked Clear button", Toast.LENGTH_SHORT).show();
                ClearDBSelections();
            }
        });

        new SelectDataTask().execute();

        this.mainListView = getListView();

        mainListView.setCacheColorHint(0);

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        new SelectDataTask().execute();
    }

    @Override
    protected void onPause() {

        super.onPause();
        this.dbHelper.close();
    }

    protected void ClearDBSelections() {

        this.adapter.ClearSelections();

    }

    private class SelectDataTask extends AsyncTask<Void, Void, String> {

        protected String doInBackground(Void... params) {

            try {

                CustomListViewDB.this.dbHelper.createDatabase(dbHelper.dbSqlite);
                CustomListViewDB.this.dbHelper.openDataBase();

                CustomListViewDB.this.currentCursor = CustomListViewDB.this.dbHelper
                        .getCursor();

            } catch (SQLException sqle) {

                throw sqle;

            }
            return null;
        }

        // can use UI thread here
        protected void onPostExecute(final String result) {

            startManagingCursor(CustomListViewDB.this.currentCursor);
            int[] listFields = new int[] { R.id.txtTitle };
            String[] dbColumns = new String[] { SqlHelper.COLUMN_TITLE };

            CustomListViewDB.this.adapter = new CustomSqlCursorAdapter(
                    CustomListViewDB.this, R.layout.single_item,
                    CustomListViewDB.this.currentCursor, dbColumns, listFields,
                    CustomListViewDB.this.dbHelper);
            setListAdapter(CustomListViewDB.this.adapter);

        }
    }

}

CustomSqlCursorAdapter.java

// src/CustomSqlCursorAdapter.java

package com.appfulcrum.blog.examples.listviewcustomdb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CustomSqlCursorAdapter extends SimpleCursorAdapter {
    private Context mContext;

    private SqlHelper mDbHelper;
    private Cursor mCurrentCursor;

    public CustomSqlCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, SqlHelper dbHelper) {
        super(context, layout, c, from, to);
        this.mCurrentCursor = c;
        this.mContext = context;
        this.mDbHelper = dbHelper;

    }

    public View getView(int pos, View inView, ViewGroup parent) {
        View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.single_item, null);
        }

        if (!this.mCurrentCursor.moveToPosition(pos)) {
            throw new SQLException("CustomSqlCursorAdapter.getView: Unable to move to position: "+pos);
        }

        CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);

        // save the row's _id value in the checkbox's tag for retrieval later
        cBox.setTag(Integer.valueOf(this.mCurrentCursor.getInt(0)));

        if (this.mCurrentCursor.getInt(SqlHelper.COLUMN_SELECTED_idx) != 0) {
            cBox.setChecked(true);
            Log.w("SqlHelper", "CheckBox true for pos "+pos+", id="+this.mCurrentCursor.getInt(0));
        } else {
            cBox.setChecked(false);
            Log.w("SqlHelper", "CheckBox false for pos "+pos+", id="+this.mCurrentCursor.getInt(0));
        }
        //cBox.setOnClickListener(this);
        cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                Log.w("SqlHelper", "Selected a CheckBox and in onCheckedChanged: "+isChecked);

                Integer _id = (Integer) buttonView.getTag();
                ContentValues values = new ContentValues();
                values.put(SqlHelper.COLUMN_SELECTED, 
                        isChecked ? Integer.valueOf(1) : Integer.valueOf(0));
                mDbHelper.dbSqlite.beginTransaction();
                try {
                    if (mDbHelper.dbSqlite.update(SqlHelper.TABLE_NAME, values, "_id=?",
                            new String[] { Integer.toString(_id) }) != 1) {
                        throw new SQLException("onCheckedChanged failed to update _id="+_id);
                    }
                    mDbHelper.dbSqlite.setTransactionSuccessful();
                } finally {
                    mDbHelper.dbSqlite.endTransaction();
                }

                Log.w("SqlHelper", "-- _id="+_id+", isChecked="+isChecked);
            }
        });

        TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
        txtTitle.setText(this.mCurrentCursor.getString(this.mCurrentCursor
                .getColumnIndex(SqlHelper.COLUMN_TITLE)));

        return (v);
    }

    public void ClearSelections() {
        this.mDbHelper.clearSelections();
        this.mCurrentCursor.requery();

    }
}

ListViewWithDBActivity.java

package com.appfulcrum.blog.examples.listviewcustomdb;

import android.app.Activity;
import android.os.Bundle;

public class ListViewWithDBActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

SQLHelper

// SqlHelper.java

package com.appfulcrum.blog.examples.listviewcustomdb;

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.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class SqlHelper extends SQLiteOpenHelper {
    private static final String DATABASE_PATH = "/data/data/com.appfulcrum.blog.examples.listviewcustomdb/databases/";

    public static final String DATABASE_NAME = "TODOList";

    public static final String TABLE_NAME = "ToDoItems";
    public static final int ToDoItems_VERSION = 1;

    public static final String COLUMN_ID = "_id";               // 0
    public static final String COLUMN_TITLE = "title";          // 1
    public static final String COLUMN_NAME_DESC = "description";// 2
    public static final String COLUMN_SELECTED = "selected";    // 3
    public static final int    COLUMN_SELECTED_idx = 3;

    public SQLiteDatabase dbSqlite;
    private Context mContext;

    public SqlHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createDB(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("SqlHelper", "Upgrading database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data");

        db.execSQL("DROP TABLE IF EXISTS ToDoItems;");

        createDB(db);
    }

    public void createDatabase(SQLiteDatabase db) {
        createDB(db);
    }

    private void createDB(SQLiteDatabase db) {
        if (db == null) {
            db = mContext.openOrCreateDatabase(DATABASE_NAME, 0, null);
        }

        db.execSQL("CREATE TABLE IF NOT EXISTS ToDoItems (_id INTEGER PRIMARY KEY, title TEXT, "
                +" description TEXT, selected INTEGER);");
        db.setVersion(ToDoItems_VERSION);

        //
        // Generate a few rows for an example
        //
        // find out how many rows already exist, and make sure there's some minimum
        SQLiteStatement s = db.compileStatement("select count(*) from ToDoItems;");

        long count = s.simpleQueryForLong();
        for (int i = 0; i < 20-count; i++) {
            db.execSQL("INSERT INTO ToDoItems VALUES(NULL,'Task #"+i+"','Description #"+i+"',0);");
        }
    }

    public void openDataBase() throws SQLException {
        String myPath = DATABASE_PATH + DATABASE_NAME;

        dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close() {
        if (dbSqlite != null)
            dbSqlite.close();

        super.close();
    }

    public Cursor getCursor() {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        queryBuilder.setTables(TABLE_NAME);

        String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE,
                COLUMN_NAME_DESC, COLUMN_SELECTED };

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, COLUMN_ID+" ASC");

        return mCursor;
    }

    public void clearSelections() {
        ContentValues values = new ContentValues();
        values.put(COLUMN_SELECTED, 0);
        this.dbSqlite.update(SqlHelper.TABLE_NAME, values, null, null);
    }
}

启动.java

//src/Start.java
package com.appfulcrum.blog.examples.listviewcustomdb;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Start extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

            public void onClick(View v) {

                Toast.makeText(getApplicationContext(),
                        " You clicked ListView From DB button", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(v.getContext(), CustomListViewDB.class);
                startActivityForResult(intent, 0);
            }
        });

    }
}

layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonlayout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="left|top" android:paddingTop="2dp"
    android:paddingBottom="2dp">

    <TextView android:id="@+id/txtTest" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:textStyle="bold"
        android:text="@string/app_name" android:textSize="15sp"
        android:textColor="#FF0000" android:gravity="center_vertical"
        android:paddingLeft="5dp">
    </TextView>

    <Button android:id="@+id/btnSimple" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:textSize="15sp"
        android:text="Listview from DB"
        android:textColor="#000000"
        >
    </Button>

</LinearLayout>

layout/simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:id="@+id/buttonlayout"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:height="32dp"
        android:gravity="left|top" android:paddingTop="2dp"
        android:paddingBottom="2dp">

        <LinearLayout android:id="@+id/buttonlayout2"
            android:orientation="horizontal" android:layout_height="wrap_content"
            android:gravity="left|center_vertical" android:layout_width="wrap_content"
            android:layout_gravity="left|center_vertical">

            <TextView android:id="@+id/txtTest"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:textStyle="bold"
                android:text="@string/list_header" android:textSize="15sp"
                android:gravity="center_vertical" android:paddingLeft="5dp">
            </TextView>

            <Button android:id="@+id/btnClear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Clear"
                android:textSize="15sp" android:layout_marginLeft="10px"
                android:layout_marginRight="10px"
                android:layout_marginBottom="2px"
                android:layout_marginTop="2px" android:height="15dp"
                android:width="70dp"></Button>
        </LinearLayout>
    </LinearLayout>

    <TableLayout android:id="@+id/TableLayout01"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:stretchColumns="*">
        <TableRow>
            <ListView android:id="@android:id/list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"></ListView>
        </TableRow>

    </TableLayout>

</LinearLayout>

layout/single_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:gravity="center_vertical">

    <CheckBox android:id="@+id/bcheck"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent" />

        <TextView android:id="@+id/txtTitle"
            android:layout_width="wrap_content" android:gravity="left|center_vertical"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:layout_alignParentLeft="true" 
            android:textSize="20sp" android:text="Test"
            android:textStyle="bold" android:paddingLeft="5dp"
            android:paddingRight="2dp" android:focusable="false"
            android:focusableInTouchMode="false"></TextView>
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="horizontal"
            android:gravity="right|center_vertical">
        </LinearLayout>

</LinearLayout>

values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ListViewWithDBActivity!</string>
    <string name="app_name">ListViewWithDB</string>
    <string name="list_header">List Headers</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1" android:versionName="1.0"    
    package="com.appfulcrum.blog.examples.listviewcustomdb">

    <application android:icon="@drawable/icon"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar">
        >
        <activity android:name=".Start" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CustomListViewDB"></activity>
    </application>

    <uses-sdk android:minSdkVersion="7" /> <!-- android 1.6 -->
</manifest>

如果你想构建,将一些任意的 icon.png 放入 drawable 中。

提前致谢。

最佳答案

我在场外找到了最完整的问题解决方案。

Android ListView with CheckBox : Retain State

感谢帮助过的人。

关于Android:带 CheckBox 的 ListView,从 SQLite 数据库填充不太工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7231559/

有关Android:带 CheckBox 的 ListView,从 SQLite 数据库填充不太工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  6. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  7. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  8. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  9. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  10. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

随机推荐