Question

I'm having a lot of trouble trying to figure out this array adapter stuff.

I've put this together with the help of some tutorials and this page How to add/remove item from listview in android when click button in item listview

But I'm having an issue where "adapter" becomes set to "null" when getView runs. I have no idea how this is happening. I've got around it with the horrible hacky solution of backing up adapter to backupAdapter, and then copying it back when I need to call adapter.notifyDataSetChanged();

I've found that the first time I use the deleteButton, resetAdapter finds that adapter is null and copies the value from the backupAdapter. After that it's fine and doesn't need to be restored again. So my code works, but it's horrible. What am I doing wrong?

public class EditTaskList extends Activity {
    private static ArrayList<Something> list;
    private CustomAdapter adapter;
    private CustomAdapter backupAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_task_edit);
        ListView listView = (ListView)findViewById(R.id.edit_task_listview);
        list = MainActivity.getMasterList();

        CustomAdapter adapter = new CustomAdapter(this, list);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        backupAdapter = adapter;
    }

    public void resetAdapter(){
        if(adapter == null){
            System.out.println("Not fixed");
            adapter = backupAdapter;
        }
        else{System.out.println("Fixed!");}
        adapter.notifyDataSetChanged();
    }

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

    public class CustomAdapter extends ArrayAdapter<Something>{
        //private Context context;
        private ArrayList<Something> things;
        private LayoutInflater inflater;

        class ViewHolder {
            public TextView text;
            public Button editButton;
            public Button deleteButton;
        }

        public CustomAdapter(Context context, ArrayList<Something> things) {
            super(context, R.layout.edit_task_list_item, things);
            inflater = LayoutInflater.from(context);
            this.things = things;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // reuse views
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.edit_task_list_item, null);
                // configure view holder
                viewHolder.text = (TextView) ConvertView.findViewById(R.id.edit_task_name);
                viewHolder.editButton = (Button) convertView.findViewById(R.id.edit_task_edit);
                viewHolder.deleteButton = (Button) convertView.findViewById(R.id.edit_task_delete);
                convertView.setTag(viewHolder);
            }

            else {
                viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.text.setTag(position);
        viewHolder.text.setText(things.get(position).toString());
        viewHolder.deleteButton.setTag(position);
        viewHolder.editButton.setTag(position);
        viewHolder.deleteButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
                int tag = (Integer)view.getTag();
                things.remove(tag);
                resetAdapter();
                }
        });

        return convertView;
    }
    }
}
Was it helpful?

Solution

This line:

CustomAdapter adapter = new CustomAdapter(this, list);

creates a local variable, so the field adapter never gets initialized. Instead you want to assign the newly created adapter to the field with:

adapter = new CustomAdapter(this, list);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top