我目前正在尝试使用列表填充列表视图,我的代码看起来像这样
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;
}
}
我的问题是它确实已填充,但数据看起来不正确,而是使用应用程序包名称和奇怪的数字,例如下面的
com.package.name.appname@41509668
我做错了吗?
答案 0 :(得分:0)
您需要覆盖AppUsage
如下:
@Override
public String toString()
{
return _name;
}
当你使用默认适配器toString方法为每个对象和对象引用返回时,所以如果你想生成Class对象的列表,你需要覆盖toString()
方法或者你可以创建自定义适配器