Android: Problem with Listview and CursorAdapter

3

I am testing the loading of a database (SQLite) to a ListView using a CursorAdapter.

From MainActivity I can access the data of the database without problems. The problem is that when you call the adapter the application hangs.

Here I leave the code:

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layFondo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context="com.example.milista4.MainActivity" >

    <TextView
        android:id="@+id/txtTituloSupreior"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="33sp" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

list_item.xml:

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:src="@drawable/ic_launcher" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView 
            android:id="@+id/txtNombre"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="17sp"
            android:text="Nombre" />

        <TextView 
            android:id="@+id/txtApellido"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="17sp"
            android:text="Apellido" />"

    </LinearLayout>

    <Button 
        android:id="@+id/btnBoton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:focusable="false"
        android:text="B" />"


</LinearLayout>

Adapter.java:

    package com.example.milista5;

    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.CursorAdapter;
    import android.widget.TextView;

    public class Adaptador extends CursorAdapter {

        public Adaptador(Activity context, Cursor cursor) {

            super(context, cursor, 0);


        }


        @Override

        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);

        }

      @Override

        public void bindView(View view, Context context, Cursor cursor) {



          // Find fields to populate in inflated template

          TextView tNombre = (TextView) view.findViewById(R.id.txtNombre);

          TextView tApellido = (TextView) view.findViewById(R.id.txtApellido);

          tNombre.setText(cursor.getString(cursor.getColumnIndexOrThrow("Nombre")));

          tApellido.setText(cursor.getString(cursor.getColumnIndexOrThrow("Apellido")));

    }
}

MainActivity.java:

package com.example.milista5;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView txtSup = (TextView) findViewById(R.id.txtTituloSupreior);

        ListView lista = (ListView) findViewById(R.id.listView1);



        SQLiteDatabase db;

        db = this.openOrCreateDatabase("Mi_Database", MODE_PRIVATE, null);

        /*db.execSQL("CREATE TABLE IF NOT EXISTS Personas (Nombre VARCHAR, Apellido VARCHAR)");

        db.execSQL("INSERT INTO Personas (Nombre, Apellido) VALUES ('Francisco', 'Profiti')");*/

        Cursor miCursor;

        miCursor = db.rawQuery("SELECT * FROM Personas" , null);

        miCursor.moveToFirst();

        Adaptador miAdaptador = new Adaptador(this, miCursor);

        lista.setAdapter(miAdaptador);

        db.close();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

The Logcat:

10-19 00:02:22.751: I/SqliteDatabaseCpp(531): sqlite returned: error code = 1, msg = no such table: Personas, db=/data/data/com.example.milista5/databases/Mi_Database
10-19 00:02:22.751: D/AndroidRuntime(531): Shutting down VM
10-19 00:02:22.761: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
10-19 00:02:22.770: E/AndroidRuntime(531): FATAL EXCEPTION: main
10-19 00:02:22.770: E/AndroidRuntime(531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.milista5/com.example.milista5.MainActivity}: android.database.sqlite.SQLiteException: no such table: Personas: , while compiling: SELECT * FROM Personas
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:137)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-19 00:02:22.770: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
10-19 00:02:22.770: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:511)
10-19 00:02:22.770: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-19 00:02:22.770: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-19 00:02:22.770: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
10-19 00:02:22.770: E/AndroidRuntime(531): Caused by: android.database.sqlite.SQLiteException: no such table: Personas: , while compiling: SELECT * FROM Personas
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
10-19 00:02:22.770: E/AndroidRuntime(531):  at com.example.milista5.MainActivity.onCreate(MainActivity.java:37)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.Activity.performCreate(Activity.java:4465)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-19 00:02:22.770: E/AndroidRuntime(531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-19 00:02:22.770: E/AndroidRuntime(531):  ... 11 more
    
asked by JuCaMa 19.10.2016 в 00:48
source

1 answer

3

JuCaMa., thanks for adding your LogCat message, based on this we determined the problem:

  

android.database.sqlite.SQLiteException: no such table: People:,   while compiling: SELECT * FROM People 10-19 00: 02: 22.770:   E / AndroidRuntime (531): at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1955)

You are making a query:

miCursor = db.rawQuery("SELECT * FROM Personas" , null);

but the Personas table does not exist!

The lines you commented on are those that make the creation of the table in the database.

db.execSQL("CREATE TABLE IF NOT EXISTS Personas (Nombre VARCHAR, Apellido VARCHAR)");

db.execSQL("INSERT INTO Personas (Nombre, Apellido) VALUES ('Francisco', 'Profiti')");
    
answered by 19.10.2016 в 03:17