0

我想在行项目ListView中获取信息对象..listview在数据库中加载信息..

添加收入

public void addIncome(OIncome income) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TABLE_INCOME_COLUMN_NAME, income.get_name());
    values.put(TABLE_INCOME_COLUMN_CATEGORY, income.get_category() + 1);
    values.put(TABLE_INCOME_COLUMN_AMOUNT, income.get_amount());
    values.put(TABLE_INCOME_COLUMN_DATE, income.get_date());
    values.put(TABLE_INCOME_COLUMN_TIME, income.get_time());
    values.put(TABLE_INCOME_COLUMN_NOTE, income.get_note());
    db.insert(TABLE_INCOME, null, values);
    db.close();
}

获得收入

public List<OIncome> getIncome() {

    List<OIncome> list = new ArrayList<OIncome>();

    String selectIncome = "SELECT * FROM " + TABLE_INCOME + " INNER JOIN "
            + TABLE_CATEGORY + " ON " + TABLE_CATEGORY + "."
            + TABLE_CATEGORY_COLUMN_ID + "=" + TABLE_INCOME + "."
            + TABLE_INCOME_COLUMN_CATEGORY + " WHERE " + TABLE_INCOME + "."
            + TABLE_INCOME_COLUMN_DATE + "=" + "'" + GET_DATE + "'"
            + " ORDER BY " + TABLE_INCOME + "." + TABLE_INCOME_COLUMN_DATE
            + " DESC";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectIncome, null);
    if (cursor.moveToFirst()) {
        do {
            OIncome income = new OIncome();
            income.set_amount(cursor.getFloat(3));
            income.set_category(cursor.getInt(8));
            income.set_name(cursor.getString(1));
            income.set_date(cursor.getString(4));
            income.set_time(cursor.getString(5));
            list.add(income);
        } while (cursor.moveToNext());

    }

    return list;

}

在 ListIncomeAdapter 类中

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(layout, null);
    }
    OIncome income = list.get(position);
    TextView tvnameincome = (TextView) convertView
            .findViewById(R.id.tvnameincome);
    TextView tvdateincome = (TextView) convertView
            .findViewById(R.id.tvdateincome);
    TextView tvtimeincome = (TextView) convertView
            .findViewById(R.id.tvtimeincome);
    TextView tvamountincome = (TextView) convertView
            .findViewById(R.id.tvamountincome);
    ImageView imglistincome = (ImageView) convertView
            .findViewById(R.id.imglistincome);
    Other other = new Other();
    // OCatergory catergory = data.get(position);
    tvnameincome.setText(income.get_name());
    tvdateincome.setText(other.convertDateYear(income.get_date()));
    tvtimeincome.setText(income.get_time());
    tvamountincome.setText(income.get_amount() + "");

    imglistincome.setImageResource(income.get_category());
    return convertView;
}

我怎样才能得到那个项目的信息..

请给我几行关于该项目的获取信息

4

1 回答 1

-1

您必须从父级中提取它,如下所示。不过,我不是 100% 确定语法,因为这里没有我的 Android 工作区。

 OIncome income = (OIncome) ((ListView) parent).getItemAtPosition(position);

这将替换您的代码行,这会引发 Cast 异常:

OIncome income = list.get(position);
于 2013-11-08T14:25:46.820 回答