jeudi 18 février 2016

Multiple Items selected of ListView when Click on Single Item of Listview in android

I have multiple items in ListView and list of item is larger than the screen size.To view all items I scroll down to the ListView. ListView contain one CheckBox and Two TextView.

enter image description here

When I select first item of the ListView other item is Also selected which is down in Scroll(which is not showing on screen and will show after scrolling).
It work properly when I removed some items from the ListView. Remaining items fit on mobile screen.The problem comes only when scrolling is added. I am stuck on that problem from yesterday.

 ListView lv=findViewById(R.id.listOfStudents);
 Cursor cursor = dbHelper.fetchAllData();
 final String[] columns = new String[]{CountriesDbAdapter.KEY_NAME, CountriesDbAdapter.KEY_CONTACT};
 int[] to = new int[]{R.id.name, R.id.phone};
 dataAdapter = new SimpleCursorAdapter(this, R.layout.student_info, cursor, columns, to, 0);
 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 lv.setAdapter(dataAdapter);
 lv.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
         CheckBox checkBox = (CheckBox) listView.getAdapter().getView(position,view , null).findViewById(R.id.checkBox);
         if (checkBox.isChecked()) {
             checkBox.setChecked(false);
         } else {
             checkBox.setChecked(true);
         }
     }
 });


code for adapter. CountriesDbAdapter.java

package com.example.usmanasghar.studentattendancefromdatabase;

/**
 * Created by Usman Asghar on 09/02/2016.
 */

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 CountriesDbAdapter {

        public static final String KEY_ROWID = "_id";
        public static final String KEY_NAME = "name";
        public static final String KEY_CONTACT = "contact";
        public static final String KEY_PRESENT = "Present";
        private static final String TAG = "CountriesDbAdapter";
        private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

        private static final String DATABASE_NAME = "ClassRoom";
        private static final String SQLITE_TABLE = "StudentInfo";
        private static final int DATABASE_VERSION = 1;

        private final Context mCtx;

        private static final String DATABASE_CREATE =
                "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
                        KEY_ROWID + " integer PRIMARY KEY autoincrement," +
                        KEY_NAME + "," +
                        KEY_CONTACT +
                        ");";


        private static class DatabaseHelper extends SQLiteOpenHelper {

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


            @Override
            public void onCreate(SQLiteDatabase db) {
                Log.w(TAG, DATABASE_CREATE);
                db.execSQL(DATABASE_CREATE);
            }

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

        public CountriesDbAdapter(Context ctx) {
            this.mCtx = ctx;
        }

        public CountriesDbAdapter open() throws SQLException {
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            if (mDbHelper != null) {
                mDbHelper.close();
            }
        }

        public long createCountry(String name,
                                  String contact) {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_NAME, name);
            initialValues.put(KEY_CONTACT, contact);
            return mDb.insert(SQLITE_TABLE, null, initialValues);
        }

        public int deleteStudentWithId(String id) {
            int doneDelete;
            doneDelete = mDb.delete(SQLITE_TABLE, "_id= ?", new String[]{id});//Delete from table where _id=?
            return doneDelete;
        }
       public Cursor fetchContactsWithId(String id){
           Cursor mCursor = null;
           if (id == null || id.length() == 0) {
               mCursor = mDb.query(SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                       null, null, null, null, null);
           } else {
               mCursor = mDb.query(true, SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME,KEY_CONTACT},
                       KEY_ROWID + " like '%" + id + "%'", null,
                       null, null, null, null);
           }
           if (mCursor != null) {
               mCursor.moveToFirst();
           }
           return mCursor;
        }
        public void UpdateData(String Name, String PhoneNumber, String id) {
            ContentValues cv = new ContentValues();
            cv.put(KEY_NAME, Name);
            cv.put(KEY_CONTACT, PhoneNumber);
            mDb.update(SQLITE_TABLE, cv, "_id = ?", new String[]{id});
        }

        public Cursor fetchCountriesByName(String inputText) throws SQLException {
            Cursor mCursor = null;
            if (inputText == null || inputText.length() == 0) {
                mCursor = mDb.query(SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                        null, null, null, null, null);
            } else {
                mCursor = mDb.query(true, SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                        KEY_NAME + " like '%" + inputText + "%'", null,
                        null, null, null, null);
            }
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;

        }

        public Cursor fetchAllData() {
            Cursor mCursor = mDb.query(SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                    null, null, null, null, null);

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }
        public Cursor fetchOnlyContacts() {
            Cursor mCursor = mDb.query(SQLITE_TABLE, new String[]{ KEY_CONTACT},
                    null, null, null, null, null);

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        public void insertSomeCountries() {
            createCountry("Usman Asghar", "03025800077");
            createCountry("Umer Zubair", "03116111474");
            createCountry("Rizwan Watto", "03321625972");
            createCountry("Waqas Ahmed", "03078434574");
            createCountry("Badi Ul Zaman", "03400714500");


        }

    }




Aucun commentaire:

Enregistrer un commentaire