1

It keeps on popping. I've checked my syntax already. Help! My code goes like this, they click the edit button, an dialog pops up, then updates my SQLite database.

Here's my viewHolder

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView foodTextView;
    TextView calorieTextView;
    Button editButton;
    Button deleteButton;

    // each data item is just a string in this case
    public ViewHolder(View v) {
        super(v);
        foodTextView = (TextView) v.findViewById(R.id.food_text_view);
        calorieTextView = (TextView) v.findViewById(R.id.calorie_text_view);
        editButton = (Button) v.findViewById(R.id.edit_button);
        deleteButton = (Button) v.findViewById(R.id.delete_button);
        editButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.edit_button){
            itemClickCallback.onEditClick(this.getAdapterPosition());
        } else if(view.getId()==R.id.delete_button){
            itemClickCallback.onDeleteClick(this.getAdapterPosition());
        }
    }
}

Here is the implementation of the interface

public void onEditClick(int p) {
    foodDatabaseAdapter.helper.getWritableDatabase();
    FragmentManager manager = getSupportFragmentManager();
    EditItemDialog editItemDialog = new EditItemDialog();
    editItemDialog.show(manager,"EditItemDialog");
}

I've tried other stuff like getLayoutPosition() but it does the same thing. I've edited it some more though I can't remember the changes I made but it seems to be working for one time only everytime I run the app but crashes the next time I try to edit. It also doesn't update after the first edit. Help!

Here's the error log

06-22 10:27:18.624 25685-25685/com.leviebora.simplecaloriecounter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.leviebora.simplecaloriecounter, PID: 25685
                                                                                java.lang.NullPointerException: Attempt to invoke interface method 'void com.leviebora.simplecaloriecounter.MyAdapter$ItemClickCallback.onEditClick(int)' on a null object reference
                                                                                    at com.leviebora.simplecaloriecounter.MyAdapter$ViewHolder.onClick(MyAdapter.java:54)
                                                                                    at android.view.View.performClick(View.java:5198)
                                                                                    at android.view.View$PerformClick.run(View.java:21147)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    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)

ItemClickCallback is in the adapter class. Here is the code to set it.

private ItemClickCallback itemClickCallback;

public interface ItemClickCallback {
    void onEditClick(int p);
    void onDeleteClick(int p);
}

public void setItemClickCallback(final ItemClickCallback itemClickCallback){
    this.itemClickCallback = itemClickCallback;
}

Here is the onCreate

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SimpleDateFormat currentDate = new SimpleDateFormat("ddMMyyyy");
    Date todayDate = new Date();
    thisDate = currentDate.format(todayDate);

    foodDatabaseAdapter = new FoodDatabaseAdapter(this);

    /* prefs = this.getSharedPreferences("runToday", MODE_PRIVATE);
    edit = prefs.edit();
    itemNumber = prefs.getInt("runToday",0);*/

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    adapter = new MyAdapter(this,getData());
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    recyclerView.addItemDecoration(new DividerItemDecoration(this));

    recyclerView.setAdapter(adapter);
    adapter.setItemClickCallback(this);

}

Edited my adapter constructor. Here's the fix.

public MyAdapter(Context context, List<Information> data, ItemClickCallback itemClickCallback ) {
    inflater = LayoutInflater.from(context);
    this.itemClickCallback = itemClickCallback;
    this.data = data;

}

3 Answers3

0

On you adaptor write the function

 public void setTtemClickCallback(itemClickCallback item) {
   this.itemClickCallback = item;
 }

and give in the fragment

adaptor.setTtemClickCallback(this);

Hope this may help

Sanoop Surendran
  • 3,241
  • 3
  • 24
  • 46
0

You have to pass your callback to the view holder. For instance, by the constructor:

private ItemClickCallback itemClickCallback;

public ViewHolder(View v, ItemClickCallback itemClickCallback) {
    super(v);
    this.itemClickCallback = itemClickCallback;
    //more code
}
Héctor
  • 19,875
  • 26
  • 98
  • 200
0

Please check "itemClickCallback" interface not initialized properly.. may be

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ItemClickCallback itemClickCallback;

        public interface ItemClickCallback {
            void onEditClick(int p);
            void onDeleteClick(int p);
        }


         public ViewHolder(View v, ItemClickCallback itemClickCallback) {
                super(v);
                ...........................
            .............................
             this.itemClickCallback = itemClickCallback;
            } 

}
Kush
  • 1,050
  • 11
  • 14