2014-01-22 53 views
0

Problem of screenshot http://i43.tinypic.com/2h5k60i.pngAndroid ListView不显示数据库信息

上面附加的是该问题的屏幕截图。会不会与数据库中的信息更新:

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/catChooser" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Spinner 
     android:id="@+id/categoryChoose" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:entries="@array/catergory_arrays" 
     android:gravity="right" /> 

    <ListView 
     android:id="@+id/dbListView" 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:layout_marginTop="10dip" > 

    </ListView> 

    <Button 
     android:id="@+id/scanCurrent" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_marginTop="10dip" 
     android:text="@string/scan" /> 

    <Button 
     android:id="@+id/editItemCurrent" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_marginBottom="10dp" 
     android:text="@string/editItem" /> 

</LinearLayout> 

CurrentItems - 应显示当前数据库行

package com.example.fooditemmonitor; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

public class CurrentItems extends Activity { 

    ItemDatabase db; 
    Context context; 
    Button addButton, editButton; 
    ListView listView; 

    // the table that displays the data 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.current_inventory); 

     db = new ItemDatabase(this); 

     // create references and listeners for the GUI interface 
     setupViews(); 

     // make the buttons clicks perform actions 
     addButtonListeners(); 

     displaySearch(); 

    } 

    private void setupViews() { 
     // bring up current database items 
     listView = (ListView) findViewById(R.id.dbListView); 

     // THE BUTTONS 
     addButton = (Button) findViewById(R.id.scanCurrent); 
     editButton = (Button) findViewById(R.id.editItemCurrent); 
    } 

    private void addButtonListeners() { 

     addButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(CurrentItems.this, AddItem.class)); 

      } 
     }); 

     editButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(CurrentItems.this, EditItems.class)); 

      } 
     }); 
    } 

    private void displaySearch() { 
     // TODO Auto-generated method stub 
     ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays(); 
     final ArrayList<String> items = new ArrayList<String>(); 
     final ArrayAdapter<String> aa; 
     aa = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, items); 
     for (int position = 0; position < data.size(); position++) { 
      ArrayList<Object> row = data.get(position); 
      items.add("\nDate:" + row.get(0).toString() + " Title:" 
        + row.get(1).toString() + "\n" + "Quantity:" 
        + Integer.parseInt(row.get(2).toString()) + "\n"); 
      aa.notifyDataSetChanged(); 
     } 
     listView.setAdapter(aa); 
    } 

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

ItemsDatabase

package com.example.fooditemmonitor; 

import java.util.ArrayList; 

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 final class ItemDatabase { 

    // the Activity or Application that is creating an object from this class. 
    Context context; 

    // a reference to the database used by this application/object 
    private SQLiteDatabase db; 

    // These constants are specific to the database. 
    private final String DATABASE_NAME = "ItemDatabase.sqlite"; 
    private final int DATABASE_VERSION = 3; 

    // These constants are specific to the database table. 
    private final String TABLE_NAME = "foodItems"; 
    private final String COLUMN_NAME_ENTRY_ID = "entryid"; 
    private final String COLUMN_NAME_BARCODE = "barcode"; 
    private final String COLUMN_NAME_TITLE = "title"; 
    private final String COLUMN_NAME_QUANTITY = "quantity"; 
    private final String COLUMN_NAME_DATE = "date"; 
    String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME; 
    String SQL_CREATE_TABLE = "create table " + TABLE_NAME + " (" 
      + COLUMN_NAME_ENTRY_ID 
      + " integer primary key autoincrement not null," + COLUMN_NAME_DATE 
      + " date," + COLUMN_NAME_BARCODE + " text," + COLUMN_NAME_TITLE 
      + " text," + COLUMN_NAME_QUANTITY + " int" + ");"; 

    public ItemDatabase(Context context) { 
     this.context = context; 

     // create or open the database 
     ItemDatabaseHelper helper = new ItemDatabaseHelper(context); 
     this.db = helper.getWritableDatabase(); 
    } 

    public void addRow(String rowStringOne, String rowStringTwo, 
      String rowStringThree, int rowIntFour) { 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_NAME_DATE, rowStringOne); 
     values.put(COLUMN_NAME_BARCODE, rowStringTwo); 
     values.put(COLUMN_NAME_TITLE, rowStringThree); 
     values.put(COLUMN_NAME_QUANTITY, rowIntFour); 

     // ask the database object to insert the new data 
     try { 
      db.insert(TABLE_NAME, null, values); 
     } catch (Exception e) { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public void updateRow(long rowID, String rowStringOne, String rowStringTwo, 
      String rowStringThree, int rowIntFour) { 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_NAME_DATE, rowStringOne); 
     values.put(COLUMN_NAME_BARCODE, rowStringTwo); 
     values.put(COLUMN_NAME_TITLE, rowStringThree); 
     values.put(COLUMN_NAME_QUANTITY, rowIntFour); 

     // ask the database object to update the database row of given rowID 
     try { 
      db.update(TABLE_NAME, values, COLUMN_NAME_ENTRY_ID + "=" + rowID, 
        null); 
     } catch (Exception e) { 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public void deleteRow(long rowID) { 
     // ask the database manager to delete the row of given id 
     try { 
      db.delete(TABLE_NAME, COLUMN_NAME_ENTRY_ID + "=" + rowID, null); 
     } catch (Exception e) { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public ArrayList<ArrayList<Object>> getAllRowsAsArrays() { 
     // create an ArrayList that will hold all of the data collected from 
     // the database. 
     ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>(); 
     // this is a database call that creates a "cursor" object. 
     // the cursor object store the information collected from the 
     // database and is used to iterate through the data. 
     Cursor cursor; 

     try { 
      // ask the database object to create the cursor. 

      cursor = db.query(TABLE_NAME, new String[] { COLUMN_NAME_DATE, 
        COLUMN_NAME_TITLE, COLUMN_NAME_QUANTITY }, null, null, 
        null, null, COLUMN_NAME_TITLE + " DESC"); 

      // move the cursor's pointer to position zero. 
      cursor.moveToFirst(); 

      // if there is data after the current cursor position, add it to the 
      // ArrayList. 
      while (cursor.moveToNext()) { 
       // your content 

       ArrayList<Object> dataList = new ArrayList<Object>(); 

       dataList.add(cursor.getColumnIndex(COLUMN_NAME_DATE)); 
       dataList.add(cursor.getColumnIndex(COLUMN_NAME_TITLE)); 
       dataList.add(cursor.getColumnIndex(COLUMN_NAME_QUANTITY)); 

       dataArrays.add(dataList); 
      } 
     } catch (SQLException e) { 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 
     // return the ArrayList that holds the data collected from the database. 
     return dataArrays; 
    } 

    public class ItemDatabaseHelper extends SQLiteOpenHelper { 
     public ItemDatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     public void onCreate(SQLiteDatabase db) { 
      // execute the query string to the database. 
      db.execSQL(SQL_CREATE_TABLE); 
     } 

     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // This database is only a cache for online data, so its upgrade 
      // policy is to simply to discard the data and start over 
      db.execSQL(SQL_DELETE_ENTRIES); 
      onCreate(db); 
     } 
    } 
} 

logcat中没有引发错误。应按照日期,标题和数量的顺序显示数据。

所有帮助将不胜感激。

回答

2
cursor.getColumnIndex 

按名称返回列的索引。所以你有索引作为结果。你应该这样做:

  dataList.add(cursor.getLong(cursor.getColumnIndex(COLUMN_NAME_DATE))); 
      dataList.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME_TITLE))); 
      dataList.add(cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_QUANTITY)));