使用自定义适配器从sqlite获取ListView

时间:2013-12-02 20:01:17

标签: android android-listview android-sqlite android-custom-view

从数据库的List值处理ArrayList值时遇到问题 enter image description here


基本上我有两个错误:

  • 上面显示了一个
  • 一个来自LogCat(String resource ID #0x1

CustomAdapter类

public class IncomeListAdaptor extends ArrayAdapter<Income>{

Context context;
int layoutResourceId;
List<Income> incomeList;

public IncomeListAdaptor(Context context, int layoutResourceId, ArrayList<Income> objects) {
    super(context, layoutResourceId, objects);
    this.context = context;
    this.layoutResourceId = layoutResourceId;
    this.incomeList = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.txtId = (TextView)row.findViewById(R.id.idItem);
        holder.txtDate = (TextView) row.findViewById(R.id.dateItem);
        holder.txtAmount = (TextView) row.findViewById(R.id.amountItem);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();
    }

    Income income = incomeList.get(position);
    holder.txtId.setText(income.getIncomeid());
    holder.txtAmount.setText(income.getAmount());
    holder.txtSource.setText(income.getSource());
    return row;
}

static class ViewHolder {
    TextView txtId;
    TextView txtAmount;
    TextView txtSource;

}}

数据库列表功能

        /**
     * getting all income
     * */

    public List<Income> getAllIncomes() {
        List<Income> incomes = new ArrayList<Income>();
        String selectQuery = "SELECT  * FROM " + TABLE_INCOME;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                Income in = new Income();
                in.setIncomeid(c.getInt(c.getColumnIndex(KEY_INCOME_ID)));
                in.setAmount(c.getInt(c.getColumnIndex(KEY_AMOUNT)));
                in.setSource(c.getString(c.getColumnIndex(KEY_SOURCE)));
                in.setCdate(c.getString(c.getColumnIndex(KEY_CDATE)));
                in.setPayementmode(c.getString(c.getColumnIndex(KEY_PAYEMENT_MODE)));

                // adding to income list
                incomes.add(in);
            } while (c.moveToNext());
        }

        return incomes;
    }

Fragment1.java类

        mIncomeList = (ListView)view.findViewById(R.id.incomelist);
    List<Income> values = db.getAllIncomes();
    IncomeListAdaptor adapter = new IncomeListAdaptor(getActivity(), R.layout.income_list_item_genrator, values);
    mIncomeList.setAdapter(adapter);

LogCat错误:

    12-03 01:03:23.141: E/AndroidRuntime(17560): FATAL EXCEPTION: main
12-03 01:03:23.141: E/AndroidRuntime(17560): android.content.res.Resources$NotFoundException: String resource ID #0x1
12-03 01:03:23.141: E/AndroidRuntime(17560):    at android.content.res.Resources.getText(Resources.java:201)
12-03 01:03:23.141: E/AndroidRuntime(17560):    at android.widget.TextView.setText(TextView.java:2961)
12-03 01:03:23.141: E/AndroidRuntime(17560):    at parth.any.ttb.adapter.IncomeListAdaptor.getView(IncomeListAdaptor.java:53)

2 个答案:

答案 0 :(得分:0)

更改以下行:

public IncomeListAdaptor(Context context, int layoutResourceId, ArrayList<Income> objects) {

public IncomeListAdaptor(Context context, int layoutResourceId, List<Income> objects) {

IncomeListAdaptor课程中。

原因

您正在将List传递给期望ArrayList的构造函数 ArrayListList的一种类型。因此,您不能只拥有List(您不知道其确切类型)并将其传递给期望确切类型 {的方法{1}}

更准确的解释

ArrayListList,有很多实现者。其中一个实现者是interface。因此,当方法需要ArrayList时,它要求特定的实现者而不是任何实现者。

答案 1 :(得分:0)

而对于

android.content.res.Resources$NotFoundException: String resource ID #0x1

错误,请确保您尝试setText()的值实际上是String数据类型。

holder.txtId.setText(income.getIncomeid());
holder.txtAmount.setText(income.getAmount());
holder.txtSource.setText(income.getSource());

确保income.getIncomeid()income.getAmount()以及income.getSource()实际上是字符串。如果没有使用适当的方法将它们转换为字符串,例如String.valueOf(income.getAmount());