Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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 如何从ListView中按钮的onClick事件中更新SQL数据库_Android_Sqlite_Listview_Onclick_Simplecursoradapter - Fatal编程技术网

Android 如何从ListView中按钮的onClick事件中更新SQL数据库

Android 如何从ListView中按钮的onClick事件中更新SQL数据库,android,sqlite,listview,onclick,simplecursoradapter,Android,Sqlite,Listview,Onclick,Simplecursoradapter,如何处理列表视图项中按钮的onClick事件。在应用程序中的内容提供商的帮助下获取数据。实现了一个自定义的SimpleCursorAdapter,但无法从onClick事件中更新数据库。在下面找到CustomSimpleCursorAdapter代码 我正在用android热身。所以可能是我遗漏了一些非常简单的东西。谢谢你的帮助 public class CustomSimpleCursorAdapter extends SimpleCursorAdapter { private

如何处理
列表视图
项中按钮的
onClick
事件。在应用程序中的内容提供商的帮助下获取数据。实现了一个自定义的
SimpleCursorAdapter
,但无法从
onClick
事件中更新数据库。在下面找到
CustomSimpleCursorAdapter
代码

我正在用android热身。所以可能是我遗漏了一些非常简单的东西。谢谢你的帮助

public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
        private Context mContext;
        private Context appContext;
        private int layout;
        private Cursor cr;
        private final LayoutInflater inflater;

        public CustomSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to,int flag) {
            super(context, layout, c, from, to,flag);
            this.layout = layout;
            this.mContext = context;
            this.inflater = LayoutInflater.from(context);
            this.cr = c;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return inflater.inflate(layout, null);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
            TextView name =(TextView)view.findViewById(R.id.productName_value);
            final TextView quantity = (TextView) view.findViewById(R.id.availableInventory_value);
            TextView price = (TextView) view.findViewById(R.id.price_value);
            TextView sales = (TextView) view.findViewById(R.id.unitsSold_value);
            Button sellButton =(Button)view.findViewById(R.id.sellButton);


            int name_index = cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_NAME);
            int quantity_index = cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_QUANTITY);
            int price_index =cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_PRICE);
            int sales_index = cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_SALES);


            name.setText(cursor.getString(name_index));
            quantity.setText(cursor.getString(quantity_index));
            price.setText(cursor.getString(price_index));
            sales.setText(cursor.getString(sales_index));
            Log.i("check before","check ok");

            sellButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri prodUri = Uri.parse(MyInventoryContentProvider.CONTENT_URI + "/" + v.getId());
                    ContentValues values = new ContentValues();
                    String new_quantity = String.valueOf(Integer.parseInt(quantity.getText().toString()) - 1);
                    values.put(InventoryTable.COLUMN_QUANTITY,new_quantity);

  InventoryUpdateTask().execute(MyInventoryContentProvider.CONTENT_URI.toString(), InventoryTable.COLUMN_QUANTITY, new_quantity, InventoryTable.COLUMN_ID, String.valueOf(v.getId()));

                }
            });

        }
        private class InventoryUpdateTask extends AsyncTask<String, Void, Void> {

            private MyInventoryContentProvider dbHelper = new MyInventoryContentProvider();
            @Override
            protected Void doInBackground(String... params) {
                ContentValues values = new ContentValues();
                    values.put(params[1], Integer.parseInt(params[2]));
                dbHelper.onCreate();
                dbHelper.update(Uri.parse(params[0]), values, params[3], new String[]{params[4]});
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {

            }
        }

    }
公共类CustomSimpleCursorAdapter扩展了SimpleCursorAdapter{
私有上下文;
私人语境;
专用int布局;
私有游标cr;
私人充气机;
公共CustomSimpleCursorAdapter(上下文上下文、int布局、光标c、字符串[]从、int[]到、int标志){
super(上下文、布局、c、from、to、标志);
this.layout=布局;
this.mContext=上下文;
this.inflater=layoutiner.from(上下文);
这个.cr=c;
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
返回充气机。充气(布局,空);
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
super.bindView(视图、上下文、光标);
TextView name=(TextView)view.findViewById(R.id.productName\u值);
最终TextView数量=(TextView)view.findViewById(R.id.availableInventory_值);
TextView价格=(TextView)view.findViewById(R.id.price\u值);
TextView销售=(TextView)view.findViewById(R.id.unitsSold_值);
Button sellButton=(Button)view.findViewById(R.id.sellButton);
int name_index=cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_name);
int quantity\u index=cursor.getColumnIndexOrThrow(InventoryTable.COLUMN\u quantity);
int price_index=cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_price);
int sales_index=cursor.getColumnIndexOrThrow(InventoryTable.COLUMN_sales);
name.setText(cursor.getString(name_index));
quantity.setText(cursor.getString(quantity_index));
price.setText(cursor.getString(price_index));
sales.setText(cursor.getString(sales_索引));
Log.i(“检查前”、“检查确定”);
sellButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Uri prodUri=Uri.parse(MyInventoryContentProvider.CONTENT_Uri+“/”+v.getId());
ContentValues=新的ContentValues();
String new_quantity=String.valueOf(Integer.parseInt(quantity.getText().toString())-1);
值.put(InventoryTable.列数量、新数量);
InventoryUpdateTask().execute(MyInventoryContentProvider.CONTENT\u URI.toString(),InventoryTable.COLUMN\u数量,new\u数量,InventoryTable.COLUMN\u ID,String.valueOf(v.getId());
}
});
}
私有类InventoryUpdateTask扩展异步任务{
私有MyInventoryContentProvider dbHelper=新的MyInventoryContentProvider();
@凌驾
受保护的Void doInBackground(字符串…参数){
ContentValues=新的ContentValues();
value.put(params[1],Integer.parseInt(params[2]);
dbHelper.onCreate();
update(Uri.parse(params[0]),值,params[3],新字符串[]{params[4]});
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
}
}
}

@RRR这篇文章仍然需要编辑,代码格式不是从
public类开始的。
而是从
public CustomSimpleCursorAdapter
@userJP开始编辑的。现在代码格式是correct@RRR这篇文章仍然需要编辑,代码格式不是从
public class..
开始的,而是从
public CustomSimpleCursorAdapter
@userJP开始的。谢谢编辑。现在代码格式正确了