[我修复它,如果有任何需要的代码]任何人都可以帮我用SQLite填充android我的ListView?我无法解决它

时间:2014-12-07 15:59:15

标签: sqlite listview simplecursoradapter

m having some problems to populate my list view and i don´t know what i做错了。我m reading end watching a lot of tutorials and can解决它,我希望有人可以帮助我,因为我自己学习,没有人帮助我。 这是我的数据库管理器。我认为错误发生在loadCursorList()方法中。

package com.piedrosoft.shoppinglisttodo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import android.widget.Toast;


public class DataBaseManager {
    // Variables con el nombre de la tabla lists y sus campos
    public static final String LS_TABLE = "lists";

    public static final String LS_ID = "lists_id";
    public static final String LS_NAME = "lists_name";
    public static final String LS_DATE = "lists_date";
    //Sentencia SQL para crear la tabla lists
    public static final String CREATE_TABLE_LISTS ="create table " +LS_TABLE+ " ("
            +LS_ID+ " integer primary key autoincrement,"
            +LS_NAME+ " text not null,"
            +LS_DATE+ " text);";

    // Variables con el nombre de la tabla cat_prods y sus campos
    public static final String CP_TABLE = "cat_prods";

    public static final String CP_ID = "cat_prods_id";
    public static final String CP_NAME = "cat_prods_name";
    //Sentencia SQL para crear la tabla cat_prods
    public static final String CREATE_TABLE_CP ="create table " +CP_TABLE+ " ("
            +CP_ID+ " integer primary key autoincrement,"
            +CP_NAME+ " text not null);";

    // Variables con el nombre de la tabla prods_type y sus campos
    public static final String PT_TABLE = "prods_type";

    public static final String PT_ID = "prods_type_id";
    public static final String PT_NAME = "prods_type_name";
    //Sentencia SQL para crear la tabla prods_type
    public static final String CREATE_TABLE_PT ="create table " +PT_TABLE+ " ("
            +PT_ID+ " integer primary key autoincrement,"
            +PT_NAME+ " text not null);";

    // Variables con el nombre de la tabla products y sus campos
    public static final String PROD_TABLE = "products";

    public static final String PROD_ID = "prod_id";
    public static final String PROD_NAME = "prod_name";
    public static final String PROD_UNIT = "prod_unit";
    public static final String PROD_TYPE = "prod_type";
    public static final String PROD_CAT = "prod_cat";
    public static final String PROD_LIST = "prod_list";
    //Sentencia SQL para crear la tabla products
    public static final String CREATE_TABLE_PROD ="create table " +PROD_TABLE+ " ("
            +PROD_ID+ " integer primary key autoincrement,"
            +PROD_NAME+ " text not null,"
            +PROD_UNIT+ " real,"
            +PROD_TYPE+ " integer,"
            +PROD_CAT+ " integer,"
            +PROD_LIST+ " integer," +
            "foreign key("+PROD_TYPE+") references "+PT_TABLE+"("+PT_ID+")," +
            "foreign key("+PROD_CAT+") references "+CP_TABLE+"("+CP_ID+")," +
            "foreign key("+PROD_LIST+") references "+LS_TABLE+"("+LS_ID+")" +
            ");";

    private SQLiteDatabase db;

    // Create DataBase
    public DataBaseManager(Context context) {
        DbHelper helper = new DbHelper(context);
        db = helper.getWritableDatabase();
        // Activar las FOREIGN KEYS de la BBDD
        if (!db.isReadOnly()) {
            db.execSQL("PRAGMA foreign_keys = ON;");
        }
    }

    public Cursor loadCursorList(){
        String[] columns = new String[]{LS_ID,LS_NAME,LS_DATE};
        return db.query(LS_TABLE,columns,null,null,null,null,null);
    } 
}

这是我的DbHelper。我认为这没关系

package com.piedrosoft.shoppinglisttodo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DbHelper extends SQLiteOpenHelper{

    // Nombre de el archivo de la base de datos que vamos a crear
    private static final String DB_NAME = "shoppinglisttodo.sqlite";
    // Version del esquema de la base de datos, se encarga el metodo onUpgrade()
    private static final int DB_VERSION = 1;


    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        //Sentencias para crear la base de datos
        db.execSQL(DataBaseManager.CREATE_TABLE_LISTS);
        db.execSQL(DataBaseManager.CREATE_TABLE_CP);
        db.execSQL(DataBaseManager.CREATE_TABLE_PT);
        db.execSQL(DataBaseManager.CREATE_TABLE_PROD);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

这是我的MainActivity,当创建适配器时,应用程序崩溃。如果问题存在于该行或被调用的方法中,我现在不知道。

package com.piedrosoft.shoppinglisttodo;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;



public class MainActivity extends Activity {


    private DataBaseManager manager;
    private Cursor cursor;
    private ListView list;
    SimpleCursorAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Call DataBaseManager for create DB
        manager = new DataBaseManager(this);


        list = (ListView)findViewById(R.id.shoppingLists);

        cursor = manager.loadCursorList();

        String[] from = new String[]{manager.LS_NAME,manager.LS_DATE};
        int[] to = new int[] {android.R.id.text1,android.R.id.text2};

        adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from,to,0);
        list.setAdapter(adapter);



        //Create navigation TABS
        TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();
        TabSpec specs = th.newTabSpec("Lists");
        specs.setContent(R.id.tab1);
        specs.setIndicator("Lists");  
        th.addTab(specs);

        specs = th.newTabSpec("To Do");
        specs.setContent(R.id.tab2);
        specs.setIndicator("To Do");
        th.addTab(specs);



        specs = th.newTabSpec("Notes");
        specs.setContent(R.id.tab3);
        specs.setIndicator("Notes");
        th.addTab(specs);



    }





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

这是我的Logcat:

12-07 18:02:32.976 2096-2096 / com.piedrosoft.shoppinglisttodo D / dalvikvm:延迟启用CheckJNI 12-07 18:02:33.024 2096-2096 / com.piedrosoft.shoppinglisttodo D / dalvikvm:GC_FOR_ALLOC释放38K,3%免费3277K / 3372K,暂停4ms,总计4ms 12-07 18:02:33.024 2096-2096 / com.piedrosoft.shoppinglisttodo I / dalvikvm-heap:将堆(frag case)增长到4.324MB,用于1127532字节分配 12-07 18:02:33.032 2096-2105 / com.piedrosoft.shoppinglisttodo D / dalvikvm:GC_FOR_ALLOC释放3K,3%免费4374K / 4476K,暂停9ms,总计9ms 12-07 18:02:33.044 2096-2096 / com.piedrosoft.shoppinglisttodo D / AndroidRuntime:关闭VM 12-07 18:02:33.044 2096-2096 / com.piedrosoft.shoppinglisttodo W / dalvikvm:threadid = 1:线程退出未捕获的异常(group = 0xa4cddb20) 12-07 18:02:33.044 2096-2096 / com.piedrosoft.shoppinglisttodo W / FileUtils:无法解决chmod(/data/data/com.piedrosoft.shoppinglisttodo/databases/shoppinglisttodo.sqlite):libcore.io.ErrnoException:chmod失败:EPERM(不允许操作) 12-07 18:02:33.044 2096-2096 / com.piedrosoft.shoppinglisttodo E / AndroidRuntime:FATAL EXCEPTION:main     过程:com.piedrosoft.shoppinglisttodo,PID:2096     java.lang.RuntimeException:无法启动活动ComponentInfo {com.piedrosoft.shoppinglisttodo / com.piedrosoft.shoppinglisttodo.MainActivity}:java.lang.IllegalArgumentException:column' _id'不存在             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)             在android.app.ActivityThread.access $ 800(ActivityThread.java:135)             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)             在android.os.Handler.dispatchMessage(Handler.java:102)             在android.os.Looper.loop(Looper.java:136)             在android.app.ActivityThread.main(ActivityThread.java:5001)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:515)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)             at dalvik.system.NativeStart.main(Native Method)      引起:java.lang.IllegalArgumentException:column' _id'不存在             在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)             在android.widget.CursorAdapter.init(CursorAdapter.java:172)             在android.widget.CursorAdapter。(CursorAdapter.java:149)             在android.widget.ResourceCursorAdapter。(ResourceCursorAdapter.java:91)             在android.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:104)             在com.piedrosoft.shoppinglisttodo.MainActivity.onCreate(MainActivity.java:38)             在android.app.Activity.performCreate(Activity.java:5231)             在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)             在android.app.ActivityThread.access $ 800(ActivityThread.java:135)             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)             在android.os.Handler.dispatchMessage(Handler.java:102)             在android.os.Looper.loop(Looper.java:136)             在android.app.ActivityThread.main(ActivityThread.java:5001)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:515)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)             在dalvik.system.NativeStart.main(本地方法)

0 个答案:

没有答案