2014-06-07 103 views
0

目前我正在試圖來填充列表視圖與列表,我的代碼看起來像這樣列表視圖沒有得到填充了正確的數據

ListView lv = (ListView) findViewById(R.id.listView1); 
    SQLdb sql = new SQLdb(this); 
    List<AppUsage> lv_arr = sql.getAllAppUsage(); 

    ArrayAdapter<AppUsage> adapter = new ArrayAdapter<AppUsage>(this, 
      android.R.layout.simple_list_item_1, lv_arr); 

    lv.setAdapter(adapter); 

AppUsage.java

public class AppUsage { 

    //private variables 
    String _package; 
    String _timeopened; 
    String _timeclosed; 
    String _duration; 
    String _name; 

    // Empty constructor 
    public AppUsage(){ 

    } 
    // constructor 
    public AppUsage(String _package, String _timeopened, String _timeclosed, String _duration, String _name){ 
     this._package = _package; 
     this._timeopened = _timeopened; 
     this._timeclosed = _timeclosed; 
     this._duration = _duration; 
     this._name = _name; 
    } 

    // package 
    public void setPackage(String appPackage){ 
     this._package = appPackage; 
    } 

// package 
public void setTimeOpened(String timeOpened){ 
    this._timeopened = timeOpened; 
} 

// package 
public void setTimeClosed(String timeClosed){ 
    this._timeclosed = timeClosed; 
} 

// package 
public void setDuration(String duration){ 
    this._duration = duration; 
} 

    // setting name 
    public void setName(String name){ 
     this._name = name; 
    } 



} 

SQLdb.java

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

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

public class SQLdb extends SQLiteOpenHelper { 

    public static final String DB_KEY = "key"; 
    public static final String DB_PACKAGE = "packageName"; 
    public static final String DB_TIMEOPEN = "timeOpened"; 
    public static final String DB_TIMECLOSED = "timeClosed"; 
    public static final String DB_DURATION = "duration"; 
    public static final String DB_NAME = "appName"; 


    private static final int DATABASE_VERSION = 2; 
    private static final String DATABASE_NAME = "AppMonitorDB"; 
    private static final String DICTIONARY_TABLE_NAME = "AppData"; 
    private static final String DICTIONARY_TABLE_CREATE = 
       "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" + 
       DB_KEY + " INT PRIMARY KEY, " + 
       DB_TIMEOPEN + " TEXT, " + 
       DB_TIMECLOSED + " TEXT, " + 
       DB_DURATION + " TEXT, " + 
       DB_NAME + " TEXT, " + 
       DB_PACKAGE + " TEXT);"; 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(DICTIONARY_TABLE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + DICTIONARY_TABLE_NAME); 

     // Create tables again 
     onCreate(db); 
    } 

     public void addAppUsage(String packageName, String timeOpened, String timeClosed, float duration, String appName) { 
      SQLiteDatabase db = this.getWritableDatabase(); 

      ContentValues values = new ContentValues(); 
      values.put(DB_PACKAGE, packageName); 
      values.put(DB_TIMEOPEN, timeOpened); 
      values.put(DB_TIMECLOSED, timeClosed); 
      values.put(DB_DURATION, duration); 
      values.put(DB_NAME, appName); 

      // Inserting Row 
      db.insert(DICTIONARY_TABLE_NAME, null, values); 
      db.close(); // Closing database connection 
     } 
     public AppUsage getAppUsage(int id) { 
      SQLiteDatabase db = this.getReadableDatabase(); 

      Cursor cursor = db.query(DICTIONARY_TABLE_NAME, new String[] { DB_PACKAGE, 
        DB_TIMEOPEN, DB_TIMECLOSED, DB_DURATION, DB_NAME }, DB_PACKAGE + "=?", 
        new String[] { String.valueOf(id) }, null, null, null, null); 
      if (cursor != null) 
       cursor.moveToFirst(); 

      AppUsage usage = new AppUsage(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4)); 
      // return contact 
      return usage; 
     } 
     public List<AppUsage> getAllAppUsage() { 
      List<AppUsage> contactList = new ArrayList<AppUsage>(); 
      // Select All Query 
      String selectQuery = "SELECT " + DB_NAME + ", " + DB_PACKAGE + ", Sum(" + DB_DURATION + ") " + DB_DURATION + " FROM "+ DICTIONARY_TABLE_NAME +" GROUP BY " + DB_NAME + ", " + DB_PACKAGE; 

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

      // looping through all rows and adding to list 
      if (cursor.moveToFirst()) { 
       do { 
        AppUsage contact = new AppUsage(); 
        contact.setName(cursor.getString(0)); 
        contact.setPackage(cursor.getString(1)); 
        contact.setDuration(cursor.getString(2)); 
        // Adding contact to list 
        contactList.add(contact); 
       } while (cursor.moveToNext()); 
      } 

      // return contact list 
      return contactList; 
     } 
} 

我的問題是,它得到填充,但數據沒有按;噸看起來是正確的,而不是它使用的應用程序包名稱和奇異數,如下面例如

[email protected] 

難道我做錯了什麼?

+1

它使用您的AppUsage對象引用填充列表。嘗試使用列表中的一些屬性填充它。 –

+1

Shoun,你是uvercomplcating的東西,爲什麼不使用SimpleCursorAdapter,你的數據畢竟來自sqlite db? – pskink

+0

感謝@pskink,我最終重寫了代碼並使用了simplecursor,以便它更有意義。 – Shaun

回答

0

你需要重寫的toString()用於AppUsage

像如下方法:

@Override 
public String toString() 
{ 
    return _name; 
} 

當您使用默認調用爲每個對象適配器toString方法和對象引用返回,所以如果你想生成的列表您需要的類對象覆蓋toString()方法,或者您可以創建自定義適配器