diya
diya

Reputation: 53

Long Click Listener does not work

I have an Adapter class where I included an OnClickListener and an OnLongClickListener. The OnClickListener works fine but the OnLongClickListener does not work. I did not find out the reason. I am attaching the code of my adapter class

class RemainderAdapter extends BaseAdapter implements Filterable{
    private Context context;
    private ArrayList<AccountDetails> rList;
    private AccountDetails aDetails;
    DBHelper myDb;
    ArrayList<AccountDetails> suggArray= new ArrayList<AccountDetails>();

    RemainderAdapter(Context context, ArrayList<AccountDetails> rList) {
        this.context = context;
        this.rList = rList;
        this.suggArray = rList;
        DBHelper myDb=new DBHelper(context);
    }

    @Override
    public int getCount() {
        return suggArray.size();
    }

    @Override
    public Object getItem(int position) {
        return suggArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewItem {
        private TextView aName,uName,circle,date;
        ViewItem(View v) {
            aName = (TextView) v.findViewById(R.id.aName);
            uName = (TextView) v.findViewById(R.id.uName);
            circle = (TextView) v.findViewById(R.id.circle);
            date = (TextView) v.findViewById(R.id.date);
        }
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewItem holder=null;

        if(convertView==null){
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.list_item,parent,false);
            holder=new ViewItem(convertView);
            convertView.setTag(holder);
        }else{
            holder=(ViewItem) convertView.getTag();
        }

        AccountDetails pd= suggArray.get(position);
        holder.aName.setText(pd.getaName());
        holder.uName.setText(pd.getuName());
        holder.date.setText(pd.getDate().toString().substring(0, 10));
        holder.circle.setText(pd.getaName().toString().substring(0, 1));
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ActivityFive.class);
                aDetails = suggArray.get(position);
                intent.putExtra("Obj", aDetails);
                context.startActivity(intent);

            }
        });

        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new AlertDialog.Builder(context).
                        setTitle("Delete").setMessage("do Do you want Delete this item ?").setIcon(R.drawable.delete)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                aDetails = suggArray.get(position);
                                myDb.deleteRow(aDetails);
                                Log.e("Values", "Position : " + position + ", Account Value : " + suggArray.get(position).getaName());
                                     context.startActivity(new Intent(context, ActivityThree.class));
                            }
                        }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();
                return true;
            }
        });

        return convertView;
    }

    public Filter getFilter() {
        return mFilter;
    }

    public Filter mFilter  = new Filter() {
        @SuppressLint("DefaultLocale")
        @Override
        protected FilterResults performFiltering(final CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final ArrayList<AccountDetails> nlist = new ArrayList<AccountDetails>();

            String filterableString ;
            if(constraint!=null && constraint.length()>0) {
                for (AccountDetails accountDetails : rList) {
                    filterableString = accountDetails.getaName().toString();
                    if (filterableString.toLowerCase().contains(filterString)) {
                        //Log.e("Values", "Model Value : "+filterableString + "  , String Value : " + constraint);
                        nlist.add(accountDetails);
                    }
                }

                results.values = nlist;
                results.count = nlist.size();
            }else{
                results.values = rList;
                results.count = rList.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            suggArray = (ArrayList<AccountDetails>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
}

Here is log

05-14 14:01:29.278 2977-2977/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sushanta.reminderpwd, PID: 2977
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.sushanta.reminderpwd.DBHelper.deleteRow(com.sushanta.reminderpwd.AccountDetails)' on a null object reference
at com.sushanta.reminderpwd.RemainderAdapter$2$2.onClick(RemainderAdapter.java:95)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 14:01:44.162 1298-1334/? E/InputDispatcher: channel 'd47d530 com.sushanta.reminderpwd/com.sushanta.reminderpwd.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 14:01:44.163 1298-1334/? E/InputDispatcher: channel '2301c89 com.sushanta.reminderpwd/com.sushanta.reminderpwd.ActivityThree (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 14:01:44.163 1298-1334/? E/InputDispatcher: channel 'f9bf7de com.sushanta.reminderpwd/com.sushanta.reminderpwd.ActivityThree (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 14:01:44.183 1298-2881/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa20cd6d0
05-14 14:01:44.263 950-1004/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
05-14 14:26:48.692 1298-1314/? E/BluetoothAdapter: Bluetooth binder is null
05-14 14:26:48.698 1298-1314/? E/KernelCpuSpeedReader: Failed to read cpu-freq
java.io.FileNotFoundException: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76)
at java.io.FileInputStream.
<init>
(FileInputStream.java:103)
at java.io.FileReader.
<init>
(FileReader.java:66)
at com.android.internal.os.KernelCpuSpeedReader.readDelta(KernelCpuSpeedReader.java:49)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8002)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76) 
at java.io.FileInputStream.
<init>
(FileInputStream.java:103) 
at java.io.FileReader.
<init>
(FileReader.java:66) 
at com.android.internal.os.KernelCpuSpeedReader.readDelta(KernelCpuSpeedReader.java:49) 
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8002) 
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366) 
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.os.HandlerThread.run(HandlerThread.java:61) 
at com.android.server.ServiceThread.run(ServiceThread.java:46) 
05-14 14:26:48.699 1298-1314/? E/KernelUidCpuTimeReader: Failed to read uid_cputime
java.io.FileNotFoundException: /proc/uid_cputime/show_uid_stat: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76)
at java.io.FileInputStream.
<init>
(FileInputStream.java:103)
at java.io.FileReader.
<init>
(FileReader.java:66)
at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8031)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76) 
at java.io.FileInputStream.
<init>
(FileInputStream.java:103) 
at java.io.FileReader.
<init>
(FileReader.java:66) 
at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71) 
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8031) 
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366) 
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.os.HandlerThread.run(HandlerThread.java:61) 
at com.android.server.ServiceThread.run(ServiceThread.java:46)

Upvotes: 0

Views: 780

Answers (4)

paul_hundal
paul_hundal

Reputation: 381

According to your logs you are getting a null pointer. Check to make sure your db has data. Most likely it does not, if it were a wrong position it would have thrown an index out of bounds exception. So check initialization of your data and make sure it has stuff in it.

Upvotes: 0

suraj
suraj

Reputation: 641

Try in Main_Activity.java where you mention ListView.

ListView list = findViewById(R.id.listView);
list.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        // put your code here.

        return true;
    }

I hope this code will help you.

Upvotes: 1

Devendra Singh
Devendra Singh

Reputation: 2514

Better you should use onItemLongClickListener on the listView

listeView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            return false;
        }
    });

Upvotes: 0

Pro Mode
Pro Mode

Reputation: 1463

initialize your DBHelper object "myDb" before calling the deleteRow() in setOnLongClickListener.

Upvotes: 0

Related Questions