Jim Moody
Jim Moody

Reputation: 808

NullPointer Exception on notifyDatasetChanged with custom Adapter

This is my first post on stack overflow so apologies if confusing.

I am getting nullpointer on foodListAdapter.notifyDatasetChanged() in my onClick method.

I am using a custom adapter. My onClick method is in my MainActivity class below. Also, please note that if I comment out the foodListAdapter.notifyDataSetChanged in my onClick method and uncomment out the other 3 lines, my code works fine. But I think this is not the cleanest way.

public class MainActivity extends Activity implements View.OnClickListener
{
    Button mainButton;
    ListView mainListView;
    ArrayAdapter spinnerDataAdapter;
    FoodListAdapter foodListAdapter;
    ArrayList newFoodList = new ArrayList();
    ShareActionProvider mShareActionProvider;
    private static final String PREFS = "prefs";
    private static final String PREF_NAME = "name";
    SharedPreferences mSharedPreferences;
    Spinner mainSpinner;
    TextView foodTextView;
    CharSequence currentDate;

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

        //Access button in activity_main and set onClickListener
        mainButton = (Button) findViewById(R.id.main_button);
        mainButton.setOnClickListener(this);

        //Access edit text in activity_main
        foodTextView = (TextView) findViewById(R.id.food_textview);

        //Access list view in activity_main
        mainListView = (ListView) findViewById(R.id.main_listview);
        mainSpinner = (Spinner) findViewById(R.id.main_spinner);
        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);

        //Create an array foodListAdapter for the spinner
        spinnerDataAdapter =  ArrayAdapter.createFromResource(this, R.array.food_types, android.R.layout.simple_spinner_item);
        mainSpinner.setAdapter(spinnerDataAdapter);

        //Set this listview to react to items being pressed
        // mainListView.setOnItemClickListener(this);

        //Greet the user or ask for their name if they are new
        displayWelcome();

        //create food list and create foodListAdapter then set the foodListAdapter
        newFoodList.add(new FoodItem(R.drawable.ic_chicken, "Chicken"));

        FoodListAdapter adapter = new FoodListAdapter(this,
                R.layout.listview_item_row, newFoodList);

        mainListView.addHeaderView(header);
        mainListView.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        //Access the Share Item defined in the menu XML
        MenuItem shareItem = menu.findItem(R.id.menu_item_share);

        //Access the object responsible for
        //putting together the sharing submenu
        if(shareItem !=null) {
            mShareActionProvider = (ShareActionProvider)shareItem.getActionProvider();
        }


        return true;
    }

    public CharSequence getFormattedDate(){
    Date d = new Date();
    CharSequence s  = DateFormat.format("MMMM d, yyyy ", d.getTime());
    return s;
    }
    @Override
    public void onClick(View v) {
        currentDate = getFormattedDate();
        foodTextView.setText(mainSpinner.getSelectedItem().toString());
        newFoodList.add(new FoodItem(R.drawable.ic_chicken, mainSpinner.getSelectedItem().toString() + " was stored on: " + currentDate ));
        foodListAdapter.notifyDataSetChanged();
       // FoodListAdapter foodListAdapter = new FoodListAdapter(this,
              // R.layout.listview_item_row, newFoodList);
       // mainListView.setAdapter(foodListAdapter);

    }

And here is my full custom adapter code:

package com.vintage.freshulator;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class FoodListAdapter extends ArrayAdapter<FoodItem> {

    Context context;
    int layoutResourceId;
    ArrayList<FoodItem> data;

    public FoodListAdapter(Context context, int layoutResourceId, ArrayList data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

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

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new FoodListHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (FoodListHolder)row.getTag();
        }

        FoodItem foodItem = data.get(position);
        holder.txtTitle.setText(foodItem.title);
        holder.imgIcon.setImageResource(foodItem.icon);

        return row;
    }

    static class FoodListHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

Upvotes: 0

Views: 926

Answers (3)

Simas
Simas

Reputation: 44158

First of all you are using a variable that will not be reachable later by other methods. Instead create your adapter in onCreate like this:

foodListAdapter = new FoodListAdapter(this, R.layout.listview_item_row,
        newFoodList);
mainListView.setAdapter(foodListAdapter);

Second of all changing the array in your activity won't change it inside of the adapter. So make the data variable inside of the adapter public:

public ArrayList<FoodItem> data;

Then in your onClick method, you can manipulate the adapters data like this:

foodListAdapter.data.add(new FoodItem(R.drawable.ic_chicken, 
    mainSpinner.getSelectedItem().toString()+" was stored on: "+currentDate));
foodListAdapter.notifyDataSetChanged();

Upvotes: 1

Giru Bhai
Giru Bhai

Reputation: 14408

Your foodListAdapter is null, because you are initializing it with adapter and using notifyDatasetChanged with foodListAdapter,so change

FoodLisAdapter adapter = new FoodListAdapter(this,
                R.layout.listview_item_row, newFoodList);
    mainListView.addHeaderView(header);
    mainListView.setAdapter(adapter);

to

foodListAdapter = new FoodListAdapter(this,
                R.layout.listview_item_row, newFoodList);
    mainListView.addHeaderView(header);
    mainListView.setAdapter(foodListAdapter);

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Use

    foodListAdapter = new FoodListAdapter(this,
            R.layout.listview_item_row, newFoodList);

for creating FoodListAdapter object. getting NullPointer Exception because foodListAdapter is null(creating new adapter object of FoodListAdapter ).

Upvotes: 0

Related Questions