1

I have an Activity Class, from where I'm calling a ListView adapter. In the adapter Class I have the onClick methods for the two buttons I have in each row. When I click the one button, I delete the row from the database, but I have to call the method inside my Activity Class, in order to update the UI, however calling this method like this:

MainActivity main = new MainActivity();
main.removeTimer();

Throws a NullPointerException. This is my method in the Activity Class:

public void removeTimer() {
    Cursor result1 = myDb.rawQuery("SELECT * from Timers", null);
    result1.moveToFirst();
    timers.clear();
    names.clear();
    while (result1.isAfterLast() == false) {
        timers.add(result1.getString(result1.getColumnIndex(TIME_COLUMN)));
        names.add(result1.getString(result1.getColumnIndex(NAME_COLUMN)));
        result1.moveToNext();
    }
    timerRow.clear();
    for (int i = 0; i < timers.size(); i++) {
        TimerRow item = new TimerRow(timers.get(i), names.get(i), bRemove,
                bStartStop);
        timerRow.add(item);
    }
    adapter.notifyDataSetChanged();
}

And it works perfectly well if I call it inside the Activity Class. This is how I call the method from my ListViewAdapter Class:

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    final TimerRow rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.timer_row, null);
        holder = new ViewHolder();
        // make layout params
        holder.txtTimer = (TextView) convertView.findViewById(R.id.tvTimes);
        holder.txtTimer.getLayoutParams().height = height / 10;
        holder.txtName = (TextView) convertView.findViewById(R.id.tvName);
        holder.txtName.getLayoutParams().height = height / 10;
        holder.bRemove = (Button) convertView.findViewById(R.id.bRemoveTimer);
        holder.bRemove.getLayoutParams().height = height / 10;
        holder.bStartStop = (Button) convertView
                .findViewById(R.id.bStartStopTimer);
        holder.bStartStop.getLayoutParams().height = height / 10;

        convertView.setTag(holder);

    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtName.setText(rowItem.getName());
    holder.txtTimer.setText(rowItem.getTimer());

    holder.bRemove.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final String name = holder.txtName.getText().toString();
            final String timer = holder.txtTimer.getText().toString();
            final SQLiteDatabase myDb = context.openOrCreateDatabase("timersDB", NO_SELECTION, null);
            //final int pos = position;
            AlertDialog alert;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(R.string.delete_timer);
            builder.setMessage(context.getString(R.string.want_to_delete) + " " + name
                        + " " + context.getString(R.string.timer2));
            builder.setPositiveButton(R.string.delete,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            myDb.execSQL("DELETE FROM Timers WHERE "
                                    + TIME_COLUMN + " = '" + timer
                                    + "' AND " + NAME_COLUMN + " = '"
                                    + name + "';");

                            // THIS IS WHERE THE METHOD IS CALLED
                            MainActivity main = new MainActivity();
                            main.removeTimer();

                        }
                    });
                alert = builder.create();
                alert.show();
                }

This is my log:

02-22 17:46:15.300: E/AndroidRuntime(4508): FATAL EXCEPTION: main
02-22 17:46:15.300: E/AndroidRuntime(4508): Process: com.adrissa.kitchentimer, PID: 4508
02-22 17:46:15.300: E/AndroidRuntime(4508): java.lang.NullPointerException
02-22 17:46:15.300: E/AndroidRuntime(4508):     at com.albert.timer.MainActivity.removeTimer(MainActivity.java:115)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at com.albert.timer.CustomTimerRowAdapter$2$2.onClick(CustomTimerRowAdapter.java:272)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at android.os.Looper.loop(Looper.java:136)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at android.app.ActivityThread.main(ActivityThread.java:5586)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at java.lang.reflect.Method.invoke(Method.java:515)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
02-22 17:46:15.300: E/AndroidRuntime(4508):     at dalvik.system.NativeStart.main(Native Method)
Marcus
  • 6,697
  • 11
  • 46
  • 89
Kæmpe Klunker
  • 865
  • 1
  • 10
  • 27

2 Answers2

0

Maybe it's because you are calling main.removeTimer() instead of mainAct.removeTimer()?

TTG_TRE
  • 19
  • 5
0

It was actually this simple, and only took me 5 hours to find this question on Google.

How to let OnClick in ListView's Adapter call Activity's function

Community
  • 1
  • 1
Kæmpe Klunker
  • 865
  • 1
  • 10
  • 27