Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:ListView带有复选框,由SQLite数据库填充,不太正常_Android_Sqlite_Listview_Checkbox - Fatal编程技术网

Android:ListView带有复选框,由SQLite数据库填充,不太正常

Android:ListView带有复选框,由SQLite数据库填充,不太正常,android,sqlite,listview,checkbox,Android,Sqlite,Listview,Checkbox,与这里的其他几篇文章一样,我试图创建一个包含每行复选框的ListView,并使用SQLite数据库存储选择的当前状态 从at的示例开始,它并没有像现在这样工作,我创建了一个简单的应用程序,它创建数据库,用20个项目填充数据库,并显示列表 它成功检索状态并存储选择的状态 但是,如果我更改复选框状态,滚动到列表的另一端并向后滚动,它将不会正确显示复选框状态。e、 g.如果我选中第一个复选框,滚动到底部,然后返回顶部,复选框将不再设置。这是在安卓2.1三星手机上运行的 如果我返回主屏幕,回到列表中,复

与这里的其他几篇文章一样,我试图创建一个包含每行复选框的ListView,并使用SQLite数据库存储选择的当前状态

从at的示例开始,它并没有像现在这样工作,我创建了一个简单的应用程序,它创建数据库,用20个项目填充数据库,并显示列表

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

但是,如果我更改复选框状态,滚动到列表的另一端并向后滚动,它将不会正确显示复选框状态。e、 g.如果我选中第一个复选框,滚动到底部,然后返回顶部,复选框将不再设置。这是在安卓2.1三星手机上运行的

如果我返回主屏幕,回到列表中,复选框设置正确,因此数据库确实已更新

该示例扩展了SimpleCursorAdapter,getView()根据表中选择列的值,使用true或false调用setChecked()

以下是所有来源

我当然很感激有人告诉我,“嗯,这是你的问题……”

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);

        }
    }

}
// 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();

    }
}
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);
    }
}
//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);
            }
        });

    }
}
ListViewWithDBActivity.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);

        }
    }

}
// 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();

    }
}
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);
    }
}
//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);
            }
        });

    }
}
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);
    }
}
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>
<?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>
<?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>
<?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>
<?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>

layout/simple.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>
<?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>
<?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>
<?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>
<?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>

layout/single_item.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>
<?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>
<?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>
<?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>
<?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>

values/strings.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>
<?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>
<?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>
<?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>
<?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>

你好,世界,ListViewWithDBActivity!
ListViewWithDB
列表标题
AndroidManifest.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>
<?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>
<?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>
<?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>
<?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>

>
如果您想构建,可以在drawable中添加一些任意的icon.png


提前感谢。

列表视图中的视图是循环使用的,这听起来像是一个问题。您可能需要在checkedChangedListener上使
无效,以便在执行
setChecked()
时,它不会无意中调用上一个侦听器。回收也可能有其他后果,所以请记住这一点

因此,请尝试:

cBox.setOnCheckedChangeListener(null);
...
cBox.setChecked();
...
cBox.setOnCheckedChangeListner(<real listener);
cBox.setOnCheckedChangeListener(空);
...
cBox.setChecked();
...

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


感激帮助者。尝试/尝试。没有改变行为。用调试器确保新代码在设备上。感觉它已经缓存了一排DB,直到我离开列表为止。但是这就是为什么我添加了所有的事务代码。TL;博士。真的,尽量更简洁。其他问题像这样。更简洁,但仍然让人困惑。我想提供所需的更多信息。正如我在补充回答中所指出的,另一个网站为确切的问题提供了解决方案。干杯。