0

我正在尝试从 SQLite 数据库填充列表视图,但有类转换异常。这是我的日志:

05-26 09:03:40.376: E/AndroidRuntime(309): Caused by: java.lang.ClassCastException: java.util.ArrayList
05-26 09:03:40.376: E/AndroidRuntime(309):  at com.example.packinfo.MainActivity.displayListview(MainActivity.java:58)
05-26 09:03:40.376: E/AndroidRuntime(309):  at com.example.packinfo.MainActivity.onCreate(MainActivity.java:27)
05-26 09:03:40.376: E/AndroidRuntime(309):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 09:03:40.376: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

这是我的代码:

MainActivity.java:

数据库处理程序.java:

package com.example.packinfo;

import java.util.ArrayList;
import java.util.List;

import com.example.packinfo.Package;

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

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME="Package_DB";

    private static final String TABLE_PACKAGE = "Package";

    private static final String KEY_PKG_ID= "pkgid";
    private static final String KEY_PKG_TYPE= "pkgtyp";
    private static final String KEY_PKG_NAME ="pkgname";
    private static final String KEY_NO_OF_FREE_ITEMS= "nitems";
    private static final String KEY_DURATION ="dur";
    private static final String KEY_CHARGES ="charge";

    public DatabaseHandler(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db){


        String createPKGTABLE = "CREATE TABLE " + TABLE_PACKAGE + "("
                + KEY_PKG_ID + " TEXT PRIMARY KEY," + KEY_PKG_TYPE + " TEXT,"
                + KEY_PKG_NAME + " TEXT,"  + KEY_NO_OF_FREE_ITEMS + " INTEGER,"
                + KEY_DURATION + " INTEGER,"  + KEY_CHARGES + " INTEGER " + ")";
            db.execSQL(createPKGTABLE);

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

            db.execSQL("DROP TABLE IF EXISTS"+ TABLE_PACKAGE);
            onCreate(db);

        }

        public void addPackage(Package pkg)
        {

            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values= new  ContentValues();
            values.put(KEY_PKG_ID, pkg.getPKG_ID());
            values.put(KEY_PKG_TYPE, pkg.getPKG_TYPE());
            values.put(KEY_PKG_NAME, pkg.getPKG_NAME());
            values.put(KEY_NO_OF_FREE_ITEMS, pkg.getPKG_ITEMS());
            values.put(KEY_DURATION, pkg.getPKG_DURATION());
            values.put(KEY_CHARGES, pkg.getPKG_CHARGES());

            db.insert(TABLE_PACKAGE, null, values);
            db.close();

        }


            public List<Package> getPackages()
            {
                List<Package> pkglist = new ArrayList<Package>();
                String selectquery ="SELECT * FROM "+TABLE_PACKAGE;

                SQLiteDatabase db = this.getWritableDatabase();
                Cursor cursor = db.rawQuery(selectquery, null);

                if(cursor.moveToFirst())
                {
                    do{

                        Package pkg =new Package();

                        pkg.setPKG_ID(cursor.getString(0));
                        pkg.setPKG_TYPE(cursor.getString(1));
                        pkg.setPKG_NAME(cursor.getString(2));
                        pkg.setPKG_ITEMS(Integer.parseInt(cursor.getString(3)));
                        pkg.setPKG_DUARTION(Integer.parseInt(cursor.getString(4)));
                        pkg.setPKG_CHARGES(Float.parseFloat(cursor.getString(5)));

                        pkglist.add(pkg);

                    }while(cursor.moveToNext());
                }
                return pkglist;
            }

        }

包.java:

package com.example.packinfo;

public class Package {

    String _PKG_ID;
    String _PKG_TYPE;
    String _PKG_NAME;
    int _NO_OF_FREE_ITEMS;
    int _DURATION;
    float _CHARGES;

    public Package(){}

    public Package(String _PKG_ID,
    String _PKG_TYPE,
    String _PKG_NAME,
    int _NO_OF_FREE_ITEMS,
    int _DURATION,
    float _CHARGES){

        this._PKG_ID= _PKG_ID;
        this._PKG_TYPE= _PKG_TYPE;
        this._PKG_NAME = _PKG_NAME;
        this._NO_OF_FREE_ITEMS = _NO_OF_FREE_ITEMS;
        this._DURATION= _DURATION;
        this._CHARGES = _CHARGES;
    }

    public Package(String pkgtyp,String pkgname, int nitems, int dur, float charge)
    {

        this._PKG_TYPE= pkgtyp;
        this._PKG_NAME = pkgname;
        this._NO_OF_FREE_ITEMS = nitems;
        this._DURATION= dur;
        this._CHARGES= charge;

    }
    public String getPKG_ID()
    {
        return this._PKG_ID;

    }

    public void setPKG_ID(String pkgid)
    {

        this._PKG_ID= pkgid;
    }

    public String getPKG_TYPE()
    {
        return this._PKG_TYPE;

    }

    public void setPKG_TYPE(String pkgtyp)
    {

        this._PKG_TYPE= pkgtyp;
    }

    public String getPKG_NAME()
    {
        return this._PKG_NAME;

    }

    public void setPKG_NAME(String pkgname)
    {

        this._PKG_NAME= pkgname;
    }

    public int getPKG_ITEMS()
    {
        return this._NO_OF_FREE_ITEMS;

    }

    public void setPKG_ITEMS(int nitems)
    {

        this._NO_OF_FREE_ITEMS= nitems;
    }

    public int getPKG_DURATION()
    {
        return this._DURATION;

    }

    public void setPKG_DUARTION(int dur)
    {

        this._DURATION= dur;
    }
    public float getPKG_CHARGES()
    {
        return this._CHARGES;

    }

    public void setPKG_CHARGES(float charge)
    {

        this._CHARGES= charge;
    }       
    }

活动主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:padding="10dp"
  android:text="@string/some_text" android:textSize="20sp" />

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

</RelativeLayout>

list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip"  >


     <!--  ListRow Left sied Thumbnail image  !-->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/sms" android:contentDescription="@string/todo"/>





    </LinearLayout>  


    <!-- Title Of Package-->
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="@string/pkg_title"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="15sp"
        android:textStyle="bold"/>

     <!--No of Free Items-->
    <TextView
        android:id="@+id/nsms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:textColor="#343434"
        android:textSize="12sp"
        android:layout_marginTop="1dip"
      android:layout_toRightOf="@+id/thumbnail"
        android:text="@string/nitems" />

     <!--  Duration -->
    <TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/name"
        android:gravity="right"
        android:text="@string/dur"
        android:layout_marginRight="5sp"
        android:textSize="15sp"
        android:textColor="#10bcc9"
        android:textStyle="bold"/>

     <!-- Charges -->
     <TextView 
         android:id="@+id/spcharges"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chargesofpkg"
        android:gravity="right"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

任何帮助将不胜感激。

4

1 回答 1

0

您的代码似乎在 List 和 ArrayList 之间混淆了。您正在创建一个 ArrayList,并将其作为列表返回。您在 displayListView 中将其投射到什么位置?您可能正在获取一个列表并尝试将其转换为 ArrayList。它们不相同。

这是对类之间差异的一个很好的描述。

于 2013-05-26T04:06:02.393 回答