从数据库中检索的数据生成列表视图

时间:2015-04-02 13:29:09

标签: android sqlite listview

我目前在根据我的数据库数据生成列表视图时遇到问题。我试图创建一个实体类来存储从数据库中检索的2变量(ID& DESCR)并将其存储在数组列表中。然后我创建了另一个String列表来存储我想要显示的变量(DESCR)并将其放入列表视图中。

PS:我在populateList()中插入了一个日志,以检查我是否确实运行了populateList()方法,并且它没有显示在我的日志中。

以下是我的代码:

package com.example.businesscalculatorassignment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;

public class HistoryActivity extends Activity implements OnItemClickListener {
    private TextView tvHis;
    private TableRow row1;
    private SQLiteAdapter mySQLiteAdapter;
    private ListView lv;
    DatabaseHelper myDbHelper = new DatabaseHelper(this);
    private SQLiteDatabase sqLiteDatabase;
    private ListAdapter HTListAdapter;
    private ArrayList<HistoryTrans> HTArrayList = new ArrayList<HistoryTrans>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // db testing

        lv = new ListView(this);
        try {
            myDbHelper.createDataBase();
        } catch (IOException ioe) {

            throw new Error("Unable to create database");
        }

        try {
            myDbHelper.openDataBase();
        } catch (SQLException sqle) {
            throw sqle;
        }


        lv.setOnItemClickListener(this);


        HTListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, populateList());
        lv.setAdapter(HTListAdapter);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.addView(lv);
        setContentView(ll);

    }

    @SuppressWarnings("deprecation")
    public List<String> populateList() {
        mySQLiteAdapter.openToRead();
        String test = "testing";
        Log.d("test", test);
        List<String> descList = new ArrayList<String>();
        String ID = new String();
        String DESCR = new String();
        Log.d("ID", ID);
        Log.d("DESCR", DESCR);

        String[] columns = new String[] { ID, DESCR };
        Cursor cursor = sqLiteDatabase.query(SQLiteAdapter.MYDATABASE_TABLE,
                columns, null, null, null, null, null);

        while (cursor.moveToNext()) {
            ID = cursor.getString(cursor.getColumnIndex(ID));
            DESCR = cursor.getString(cursor.getColumnIndex(DESCR));

            HistoryTrans HT = new HistoryTrans();
            HT.setID(ID);
            HT.setDESCR(DESCR);
            HTArrayList.add(HT);
            descList.add(DESCR);
            Log.d("ID", ID);
            Log.d("DESCR", DESCR);
        }
        mySQLiteAdapter.close();
        return descList;

    }

    @Override
    protected void onResume() {
        super.onResume();
        HTListAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, populateList());
        lv.setAdapter(HTListAdapter);
    }


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    Toast.makeText(getApplicationContext(), "Clicked on :" + arg2, Toast.LENGTH_SHORT).show();

    // We want to redirect to another Activity when the user click an item on the ListView
    Intent HistoryResultIntent = new Intent(this, HIstoryResult.class);

    // We have to identify what object, does the user clicked, because we are going to pass only clicked object details to the next activity
    // What we are going to do is, get the ID of the clicked item and get the values from the ArrayList which has
    //same array id.
    HistoryTrans clickedObject =  HTArrayList.get(arg2);

    // We have to bundle the data, which we want to pass to the other activity from this activity
    String IDbuffer = new String();
    IDbuffer = clickedObject.getID();

    // Attach the bundled data to the intent
    HistoryResultIntent.putExtra("ID",IDbuffer);

    // Start the Activity
    startActivity(HistoryResultIntent);

}
}

以下是我需要的实体类

package com.example.businesscalculatorassignment;

import java.io.Serializable;
import java.util.ArrayList;

public class HistoryTrans implements Serializable{

    private String ID;
    private String DESCR;

    private ArrayList<String> IDList = new ArrayList<String>();
    private ArrayList<String> DescrList = new ArrayList<String>();
    public HistoryTrans(String iD, String dESCR) {
        super();
        ID = iD;
        DESCR = dESCR;
    }
    public HistoryTrans() {
    }
    public String getDESCR() {
        return DESCR;
    }
    public void setDESCR(String dESCR) {
        DESCR = dESCR;
    }

    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }


}

Log cat report

04-02 21:56:08.400: E/AndroidRuntime(5125): in writeCrashedAppName, pkgName :com.example.businesscalculatorassignment
04-02 21:56:08.400: D/AndroidRuntime(5125): file written successfully with content: com.example.businesscalculatorassignment StringBuffer : ;com.example.businesscalculatorassignment
04-02 21:56:08.400: E/AndroidRuntime(5125): FATAL EXCEPTION: main
04-02 21:56:08.400: E/AndroidRuntime(5125): Process: com.example.businesscalculatorassignment, PID: 5125
04-02 21:56:08.400: E/AndroidRuntime(5125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.businesscalculatorassignment/com.example.businesscalculatorassignment.HistoryActivity}: java.lang.NullPointerException
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.os.Looper.loop(Looper.java:136)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.ActivityThread.main(ActivityThread.java:5021)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at java.lang.reflect.Method.invoke(Method.java:515)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at dalvik.system.NativeStart.main(Native Method)
04-02 21:56:08.400: E/AndroidRuntime(5125): Caused by: java.lang.NullPointerException
04-02 21:56:08.400: E/AndroidRuntime(5125):     at com.example.businesscalculatorassignment.HistoryActivity.populateList(HistoryActivity.java:71)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at com.example.businesscalculatorassignment.HistoryActivity.onCreate(HistoryActivity.java:59)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.Activity.performCreate(Activity.java:5231)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
04-02 21:56:08.400: E/AndroidRuntime(5125):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-02 21:56:08.400: E/AndroidRuntime(5125):     ... 11 more

提前感谢您提供的帮助!

1 个答案:

答案 0 :(得分:0)

解决了我的问题。 这是因为错误的光标指向数据库中的错误数据,因此无法运行代码并生成接口。