删除按钮listviewitem不起作用

时间:2016-07-14 07:04:30

标签: android

这是我的onStartMethod

 protected void onStart() {

    dbHelper = new MyDBHandler(this);
    //   dbHelper.open();
    Cursor cursor = dbHelper.fetch();
    listView = (ListView) findViewById(R.id.list_view);
    listView.setEmptyView(findViewById(R.id.empty));
    adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
    adapter.notifyDataSetChanged();

    listView.setAdapter(adapter);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
            TextView titleTextView = (TextView) view.findViewById(R.id.titlelist);
            TextView dateTextView = (TextView) view.findViewById(R.id.datelist);
            TextView timeTextView = (TextView) view.findViewById(R.id.timelist);
            TextView idTextview = (TextView) view.findViewById(R.id.idlist);
            TextView descriptionTextView = (TextView) view.findViewById(R.id.descriptionlist);
            TextView snoozeTextView = (TextView) view.findViewById(R.id.snoozelist);
            TextView repeatTextView = (TextView) view.findViewById(R.id.repeatlist);
            final Button deleteButton = (Button) view.findViewById(R.id.deletelist);

            String title = titleTextView.getText().toString();
            String date = dateTextView.getText().toString();
            String time = timeTextView.getText().toString();
            String id = idTextview.getText().toString();
            String description = descriptionTextView.getText().toString();
            String snooze = snoozeTextView.getText().toString();
            String repeat = repeatTextView.getText().toString();
            Intent modify_intent = new Intent(getApplicationContext(), AlarmActivity.class);

            modify_intent.putExtra("id", id);
            modify_intent.putExtra("title", title);
            modify_intent.putExtra("time", time);
            modify_intent.putExtra("date", date);
            modify_intent.putExtra("description", description);
            modify_intent.putExtra("snooze", snooze);
            modify_intent.putExtra("repeat", repeat);

            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    AlertDialog.Builder adb = new AlertDialog.Builder(ReminderList.this);
                    adb.setTitle("Delete?");
                    adb.setMessage("Are you sure you want to delete  this reminder?");
                    final int positionToRemove = v.getId();
                    adb.setNegativeButton("Cancel", null);
                    adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dbHelper.remove(positionToRemove);
                            adapter.notifyDataSetChanged();
                        }
                    });
                    adb.show();
                }
            });
            startActivity(modify_intent);
            listView.invalidateViews();
        }
    });
    super.onStart();
}

在这个程序中我想删除一个listitem,但删除按钮根本没有响应。

这是Button的xml。我已经删除了对触摸的关注。这是导致问题的原因。

    <Button
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/deletelist"
    android:layout_alignParentRight="true"
    android:background="@drawable/ic_delete_24dp"
    android:gravity="center|right"
    android:layout_marginRight="8dp"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

请帮帮我,我是Android的新手。

1 个答案:

答案 0 :(得分:1)

首先,您应该在适配器类中放置一个内部接口,如下所示

public static interface ProductsCheckoutAdapterCallbacks {

        void onremoveCartButtonClicked(ImageView removebutton);
    }

在适配器类中创建一个

的对象
   ProductsCheckoutAdapterCallbacks callbacks;

相应地开发列表适配器构造函数

public ProductCheckoutAdapter(Context context, int layoutResourceId, ArrayList<ProductCheckout> data, ProductsCheckoutAdapterCallbacks callBacks, ArrayList<Cartupdate> cartupdatelist)
    {
        super(context, layoutResourceId, data);
        this.context = context;
        //other implementation
}
    this.layoutResourceId = layoutResourceId;
    this.callbacks = callBacks;

并确保您的活动应该将该接口实现为

public class Product_Checkout extends Activity implements
        ProductCheckoutAdapter.ProductsCheckoutAdapterCallbacks

在您的活动中覆盖方法

@Override
    public void onremoveCartButtonClicked(ImageView removeBtn) {
        // TODO Auto-generated method stub
        this.removeButton = (ImageView) removeBtn;
        String inputs[] = removeBtn.getTag().toString().split(":");
        // inputs[5]=cartData.cartId;
        /*
         * String productid = inputs[0]; String size = inputs[1]; String color =
         * inputs[2];
         */
        Constants.context=getActivity();
        if(Constants.isConnectingToInternet()) {
            new Removeproduct().execute(inputs);
        }
        // Toast.makeText(getActivity(), "Product removed from cart",
        // 6000).show();
    }