Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用CustomAdapter从sqlite查看ListView_Android_Android Listview_Android Sqlite_Android Custom View - Fatal编程技术网

Android 使用CustomAdapter从sqlite查看ListView

Android 使用CustomAdapter从sqlite查看ListView,android,android-listview,android-sqlite,android-custom-view,Android,Android Listview,Android Sqlite,Android Custom View,从数据库的列表值处理ArrayList值时遇到问题 基本上我有两个错误: 上图 一个来自LogCat(字符串资源ID#0x1) 自定义适配器类: public class IncomeListAdaptor extends ArrayAdapter<Income>{ Context context; int layoutResourceId; List<Income> incomeList; public IncomeListAdaptor(Context

从数据库的列表值处理ArrayList值时遇到问题


基本上我有两个错误:

  • 上图
  • 一个来自LogCat(
    字符串资源ID#0x1

自定义适配器类

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;

}}

更改以下行:

public IncomeListAdaptor(Context context, int layoutResourceId, ArrayList<Income> objects) {
public IncomeListAdaptor(上下文、int-layoutResourceId、ArrayList对象){

public IncomeListAdaptor(上下文、int-layoutResourceId、列表对象){
在您的
incomalistadaptor
课程中

原因

        /**
     * 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;
    }
您正在将
列表
传递给需要
数组列表的构造函数

ArrayList
是一种类型的
列表
。因此,您不能只拥有一个
列表
(您不知道它的确切类型),然后将其传递给需要确切类型的方法
ArrayList

更准确的解释

        /**
     * 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;
    }
列表
是一个有很多实现者的
接口
。其中一个实现者是
ArrayList
。因此,当一个方法需要一个
ArrayList
时,它要求的是一个特定的实现者,而不是任何实现者。

android.content.res.Resources$NotFoundException: String resource ID #0x1
错误,请确保您试图设置xt()的值实际上是字符串数据类型

i、 介入

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())
etc

您的构造函数希望得到一个
ArrayList
但是您给了它一个
列表
您试过给它一个ArrayList吗?是的,但是这样做我仍然得到了我解释过的logCat的第二个错误..!!适配器中的第53行在哪里?这个:holder.txtId.setText(income.getIncomeid());为什么要使用
ViewHolder
?只需使用
row
设置
textView的文本,还可以检查
收入。getIncomeid()
正在返回一些东西,正如@tyczj所说,制作
列表
数组列表
android.content.res.Resources$NotFoundException: String resource ID #0x1
holder.txtId.setText(income.getIncomeid());
holder.txtAmount.setText(income.getAmount());
holder.txtSource.setText(income.getSource());