ListView setOnItemLongClickListener无法运行android

时间:2017-01-17 19:56:18

标签: android

我有一个Android应用程序,它显示了一个项目列表。通过按项目,它应该执行删除项目之类的操作。问题是,当我按下项目时,没有任何反应,就像屏幕无法识别我的手指按压一样。但是,在页面上,我有一个按钮,当我按下它时工作正常。

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

private ListView listView;
public static FirebaseUtil firebaseUtil;
public static CostumeAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    firebaseUtil = new FirebaseUtil();
    setContentView(R.layout.activity_main);

    final Context context = this;

    if (firebaseUtil.getmFirebaseUser() == null) {
        loadLogInView();
    } else {
        firebaseUtil.setmUserId(firebaseUtil.getmFirebaseUser().getUid());

        listView = (ListView) findViewById(R.id.listView);
        adapter = new CostumeAdapter(this);
        listView.setAdapter(adapter);

        fillData();

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final Costume car = adapter.getCostumes().get(position);
                final String uuid = car.getUuid();
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle("Delete")
                        .setMessage("This car will be deleted.")
                        .setPositiveButton("Delete", new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                firebaseUtil.remove(uuid, car);
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .show();
                return true;
            }
        });

xml文件没问题,我保证。

2 个答案:

答案 0 :(得分:1)

您实现的OnItemLongClickListener仅在执行else部分时才有效。 firebaseUtil.getmFirebaseUser()== null false?

答案 1 :(得分:0)

我使用了它,并且它可以工作,您可以尝试使用一些Log.d()来查看是否执行了clickListener。

public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    Log.d(TAG, "onViewCreated: on View Created");
    getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){

        public boolean onItemLongClick(AdapterView<?> adapterView,View view,final int position, long id) {
            new AlertDialog.Builder(getContext())
                    .setTitle(R.string.attention)
                    .setMessage(R.string.confirm_to_delete)
                    .setNegativeButton(R.string.cancel,null)
                    .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Cursor c= adapter.getCursor();
                            c.moveToPosition(position);
                            int itemID = c.getInt(c.getColumnIndex("_id"));
                            dbWrite.delete("diary","_id=?",new String[]{""+itemID});
                            refreshList();
                        }
                    }).show();
            return true;
        }
    });
}