首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ListView在安卓应用程序中没有显示任何东西

ListView在安卓应用程序中没有显示任何东西
EN

Stack Overflow用户
提问于 2015-08-10 17:13:53
回答 2查看 97关注 0票数 0

好的,这是整个代码

我是MainActivity.java

代码语言:javascript
运行
复制
package com.gobtron.database_test;

import android.database.Cursor;
import android.database.DatabaseUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class MainActivity extends ActionBarActivity {

    DBAdapter myDb;



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

        openDB();
        populateListView();
    }

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

    private void openDB() {
        myDb = new DBAdapter(this);
        myDb.open();
    }

    public void onClick_ViewData (View v){
        openDB();
        populateListView();
    }

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

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void populateListView() {
        Cursor cursor = myDb.getAllRows();
       // DatabaseUtils.dumpCursor(cursor);
        String[] fromFieldNames = new String[] {DBAdapter.KEY_ROWID, DBAdapter.KEY_NOM};
        int[] toViewIDs = new int[] {R.id.textView2, R.id.textView3};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item_layout, cursor, fromFieldNames, toViewIDs, cursor.getCount());
        ListView myList = (ListView) findViewById(R.id.listView);
        myList.setAdapter(myCursorAdapter);

    }
}

这是DPAdapter类代码:

代码语言:javascript
运行
复制
package com.gobtron.database_test;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {

    private static final String TAG = "DBAdapter"; //used for logging database version changes

    // Field Names:
    public static final String KEY_ROWID = "_id";
    public static final String KEY_MORPH = "'morph'";
    public static final String KEY_LONGFRONDE = "'long_fronde'";
    public static final String KEY_FORMELIMBE = "'forme_limbe'";
    public static final String KEY_DISPOSFRONDE = "'dispos_fronde'";
    public static final String KEY_DESC = "'description'";
    public static final String KEY_DESCHOIX = "'desc_choix'";
    public static final String KEY_DESCHOIX2 = "'desc_choix2'";
    public static final String KEY_NOM = "'nom'";


    public static final String[] ALL_KEYS ={KEY_ROWID, KEY_MORPH, KEY_LONGFRONDE, KEY_FORMELIMBE, KEY_DISPOSFRONDE, KEY_DESC, KEY_DESCHOIX, KEY_DESCHOIX2, KEY_NOM};
    public static final String[] ALL_KEYS2 = {"_id", "'long_fronde'"};

    // Column Numbers for each Field Name:
    public static final int COL_ROWID = 0;
    public static final int COL_MORPH = 1;
    public static final int COL_LONGFRONDE = 2;
    public static final int COL_FORMELIMBE = 3;
    public static final int COL_DISPOSFRONDE = 4;
    public static final int COL_DESC = 5;
    public static final int COL_DESCHOIX = 6;
    public static final int COL_DESCHOIX2 = 7;
    public static final int COL_NOM = 8;

    // DataBase info:
    public static final String DATABASE_NAME = "fougeres_db";
    public static final String DATABASE_TABLE = "mono_dimo";
    public static final int DATABASE_VERSION = 3; // The version number must be incremented each time a change to DB structure occurs.

    //SQL statement to create database
    private static final String DATABASE_CREATE_SQL =
            "CREATE TABLE " + DATABASE_TABLE
            + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_NOM + " TEXT NOT NULL, "
            + KEY_DESC + " TEXT"
            + ");";

    private final Context context;
    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;


    public DBAdapter(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }

    // Open the database connection.
    public DBAdapter open() {

        db = myDBHelper.getWritableDatabase();
        return this;
    }

    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }

    // Add a new set of values to be inserted into the database.
    public long insertRow(String task, String date) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NOM, task);
        initialValues.put(KEY_MORPH, date);

        // Insert the data into the database.
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    // Delete a row from the database, by rowId (primary key)
    public boolean deleteRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        return db.delete(DATABASE_TABLE, where, null) != 0;
    }

    public void deleteAll() {
        Cursor c = getAllRows();
        long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
        if (c.moveToFirst()) {
            do {
                deleteRow(c.getLong((int) rowId));              
            } while (c.moveToNext());
        }
        c.close();
    }

    // Return all data in the database.
    public Cursor getAllRows() {
        String where = null;
        Cursor c =  db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null, null);
        DatabaseUtils.dumpCursor(c);
        if (c != null) {
            c.moveToFirst();
        }
        return c;

    }

    // Get a specific row (by rowId)
    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Change an existing row to be equal to new data.
    public boolean updateRow(long rowId, String task, String date) {
        String where = KEY_ROWID + "=" + rowId;
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_NOM, task);
        newValues.put(KEY_MORPH, date);
        // Insert it into the database.
        return db.update(DATABASE_TABLE, newValues, where, null) != 0;
    }


    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE_SQL);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data!");

            // Destroy old database:
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

            // Recreate new database:
            onCreate(_db);
        }
    }


}

我不明白为什么我的光标是空的。我认为它正确地打开了数据库,也打开了表。

好吧..。我对java很陌生,所以我可能错过了一些愚蠢的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-11 16:36:35

好吧,原来我的数据库放错地方了。我需要打开Android设备监视器,然后转到文件资源管理器,然后将我现有的数据库放到/data/data/my package name/ database /

票数 0
EN

Stack Overflow用户

发布于 2015-08-10 17:55:30

您正在尝试使用键KEY_MORTH插入一个值,但是在表中没有此键的字段如下:

代码语言:javascript
运行
复制
 "CREATE TABLE " + DATABASE_TABLE
            + " (" + KEY_ROWID + " INTEGER PRIMARY KEY , "
            + KEY_NOM + " TEXT NOT NULL, "
            + KEY_DESC + " TEXT"
            + ");";

需要的是

代码语言:javascript
运行
复制
 "CREATE TABLE " + DATABASE_TABLE
            + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_NOM + " TEXT NOT NULL, "
            + KEY_DESC + " TEXT, "
            + KEY_MORTH + " TEXT "
            + ");";

您需要更新db版本或清除数据才能生效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31925420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档