sqlitedatabase中的问题和数据显示为cardview

时间:2016-06-28 17:56:15

标签: java android sqlite android-recyclerview

当我点击显示联系人按钮时,首先联系显示,下一个联系人无法显示它显示异常如何解决此异常任何人都可以解决此错误,如果它在opeNDatabase方法中显示错误?

public class ShowContacts extends Activity
{
private SQLiteDatabase db;
DbOperations doo;
private List<Contacts> contactsList;
private RecyclerView rv;
private Cursor c;
String names,email,address;
int phone;
String read_query = "select * from "+ ContactsTask.ContactsEntry.TABLE_NAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycle_layout);
    doo = new DbOperations(this);
    openDatabase();
    rv = (RecyclerView)findViewById(R.id.recyclerview);
    initializeData();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    rv.setLayoutManager(linearLayoutManager);
    rv.setHasFixedSize(true);
    ContactAdapter cc = new ContactAdapter(contactsList);
    rv.setAdapter(cc);
}
public  void initializeData() {
    try {
        contactsList = new ArrayList<>();
        try {
            db = doo.getReadableDatabase();
            c = db.rawQuery(read_query, null);
            c.moveToFirst();
            while (!c.isLast()) {
                names = c.getString(0);
                phone = c.getInt(1);
                email = c.getString(2);
                address = c.getString(3);
                contactsList.add(new Contacts(names, phone, email, address));
            }
            c.isLast();
            names = c.getString(0);
            phone = c.getInt(1);
            email = c.getString(2);
            address = c.getString(3);
            contactsList.add(new Contacts(names, phone, email, address));
        } catch (Exception e) {
            //exception handling
        } finally {
            if (c != null) {
                c.close();
            }
        }
    /*BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute("get_info");*/
    }catch (Exception e)
    {

    }
}

private void openDatabase() {
    db = openOrCreateDatabase("contactDB", Context.MODE_PRIVATE,null);
}

}

logcat错误

06-28 23:22:34.885 20407-20418/com.example.anilkumar.contactstask    E/SQLiteDatabase: close() was never explicitly called on database '/data/data/com.example.anilkumar.contactstask/databases/contactDB' 
                                                                                android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
                                                                                    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
                                                                                    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1043)
                                                                                    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1036)
                                                                                    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:761)
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:215)
                                                                                    at com.example.anilkumar.contactstask.ShowContacts.openDatabase(ShowContacts.java:77)
                                                                                    at com.example.anilkumar.contactstask.ShowContacts.onCreate(ShowContacts.java:32)
                                                                                    at android.app.Activity.performCreate(Activity.java:4466)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                                                    at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:137)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                                    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

添加

db.close();

db = doo.getReadableDatabase();
c = db.rawQuery(read_query, null);