2015-04-02 130 views
0

我目前正在面對基於我的數據庫數據生成列表視圖的問題。我試圖創建一個實體類來存儲從數據庫中檢索到的2個變量(ID & DESCR)並將其存儲在數組列表中。然後我創建了另一個字符串列表來存儲我想要顯示的變量(DESCR)並將其放入列表視圖中。 PS:我插入了一個日誌來測試populateList()來檢查我是否運行了populateList()方法,並且它沒有出現在我的日誌cat中。從數據庫檢索的數據生成列表視圖

這裏是我的代碼:

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


} 

登錄貓報告

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 

預先感謝幫助提供!

+0

您打印了IDbuffer = clickedObject.getID();'?這裏你有沒有ID? – 2015-04-02 13:49:04

+0

@NewDeveloper我只是試圖通過日誌查看它,並沒有顯示它。我懷疑它會顯示任何東西,因爲當我嘗試運行此頁面時,甚至沒有生成界面,所以我不可能做出任何點擊並觸發onitemclick監聽器。 – JamesYTL 2015-04-02 13:58:40

+0

我認爲你沒有爲'mySQLiteAdapter'創建對象。可能會是這個問題。從你的日誌錯誤發生在第72行。請檢查.. – 2015-04-02 14:21:20

回答

0

解決了我的問題。 這是因爲不正確的光標指向數據庫中的錯誤數據,因此無法運行代碼並生成接口。