2013-11-08 3 views
0

Я хочу, чтобы получить информацию об объекте в строке элемента ListView ..listview Информация загрузки в базе данных ..не может быть приведен к android.database.Cursor

добавить доход

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

Как я могу получить информацию этого пункта ..

пожалуйста, дайте мне несколько строк получить информацию по этому пункту

+0

Где вы получаете исключение броска? – wvdz

+0

yes ... Exception java.lang.ClassCastException: com.it.object.Oincome не может быть добавлен в android.database.Cursor – LKIT

ответ

-1

Вы должны извлечь его из родителей, как показано ниже. Я не уверен на 100% в синтаксисе, потому что у меня нет рабочего места Android здесь.

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

Это заменяет строку кода, который бросает Cast Exception:

OIncome income = list.get(position); 
+0

Я не понимаю .. вы можете пример ?? – LKIT

+0

Я отредактировал мой пост, надеюсь, будет более ясным. – wvdz

Смежные вопросы