2014-06-07 72 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()方法,或者您可以创建自定义适配器