Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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自定义适配器_Android_Database - Fatal编程技术网

Android自定义适配器

Android自定义适配器,android,database,Android,Database,我想创建一个自定义适配器,以便在每个列表项中都有一个可单击的按钮。 我的问题是,我从数据库获取数据,不知道如何使用自定义适配器在列表中显示这些数据。这是我的适配器代码: public class Adapter extends SimpleCursorAdapter { private Context mContext; final Drawable delete; private ImageButton imageButton2; private Layou

我想创建一个自定义适配器,以便在每个列表项中都有一个可单击的按钮。 我的问题是,我从数据库获取数据,不知道如何使用自定义适配器在列表中显示这些数据。这是我的适配器代码:

public class Adapter extends SimpleCursorAdapter {
    private Context mContext;

    final Drawable delete;
    private ImageButton imageButton2;

    private LayoutInflater inflater;

    private int favoriteListRow;

    public Adapter(Context ctx, int favoriteListRow, Cursor cursor,
            String[] columns, int[] to) {
        super(ctx, favoriteListRow, cursor, columns, to);

        mContext = ctx;
        inflater = LayoutInflater.from(mContext);

        delete_btn = ctx.getResources().getDrawable(R.drawable.delete);

        DB entry = new DB(ctx);
        entry.open();
        cursor = entry.getData();
        columns = new String[] { DBHelper.NAME, DBHelper.NAME2 };
        to = new int[] { R.id.textView01, R.id.textView02 };
        entry.close();

    }

    public View getView(Context context, Cursor cursor, ViewGroup parent) {

        final LayoutInflater inflater = LayoutInflater.from(mContext);

        View v = inflater.inflate(R.layout.row, parent, false);

        TextView name_text = (TextView) v.findViewById(R.id.textView01);

        int nameCol = cursor.getColumnIndex(DBHelper.NAME);

        String name = cursor.getString(nameCol);
        name_text.setText(name);

        TextView name2_text = (TextView) v.findViewById(R.id.textView02);
        int theCol = cursor.getColumnIndex(DBHelper.NAME2);

        String the = cursor.getString(theCol);
        the_text.setText(the);

        imageButton2 = (ImageButton) v.findViewById(R.id.delete_btn);
        imageButton2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(mContext, "Button pressed", Toast.LENGTH_LONG)
                        .show();

            }
        });

        return v;
    }



}
然后从我的主要活动调用适配器:

Adapter adapter;

ListView list = (ListView) findViewById(R.id.list);

        list.setAdapter(adapter);

尝试使用此行初始化布局充气器

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);

你有什么问题吗?看起来你的思路是正确的。是的,正如你现在看到的我的代码,我得到了一个错误java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换到android.widget.AblistView$LayoutParams在适配器类中读取我的数据库数据是正确的吗?或者我最好在我的main中读取它们?这不是一个好的选择,但与你的问题无关。你的充气机好像出毛病了。阅读此链接我替换了该链接,我没有收到此错误。但是我仍然没有结果显示在我的列表中看起来您在设置适配器之前没有初始化适配器