0

I need my listview to always have a trailing list item different to the other list items, even when the adapter is empty.

I have tried adding a null item at the end of the dataset in the adapter constructor:

mDataset.add(null);

In the getView method I distinguish between the last item and the others and inflate different layouts accordingly.

When the user adds a new list item, I need to delete the null item in the dataset, add the new data the end of the dataset and then add another null item in the last index of the dataset:

public void addData(String data) {
    mDataset.remove(mDataset.indexOf(null));
    mDataset.add(data);
    mDataset.add(null);
    notifyDataSetChanged();
}

This is of course called within the adapter. When I run the app I get the following error:

java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.neutronstar.revu.DataAdapter$DataViewHolder.mDataTextView' on a null object reference

Which maps to this line in the getView() method of my adapter:

holder.mDataTextView.setText(mDataset.get(position));

Here is the getView() method in the adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    long viewType = getItemId(position);

    Log.i("SPECS ADAPTER", viewType + "");

    if (getCount() == 0)
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_add_spec, null);
    else if (viewType == NOT_LAST_ITEM) {
        SpecViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_spec, null);
            holder = new SpecViewHolder(convertView);
            convertView.setTag(holder);
        } else
            holder = (SpecViewHolder) convertView.getTag();

        holder.mSpecTextView.setText(mSpecs.get(position));

    } else {
        if (convertView == null)
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_add_spec, null);
    }
    return convertView;
}

public void addSpec(String spec) {
    mSpecs.remove(mSpecs.indexOf(null));
    mSpecs.add(spec);
    mSpecs.add(null);
    notifyDataSetChanged();
}

static class SpecViewHolder {
    @BindView(R.id.spec_text_view) TextView mSpecTextView;

    SpecViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}

How do I fix this error?

Thanks

9
  • 1
    Basically, you want to add a footer Commented Sep 30, 2016 at 16:02
  • I need an extra list item, so its part of the list but it always shows up at the bottom of the list view. @Rotwang Commented Sep 30, 2016 at 16:59
  • show ur getView() code Commented Sep 30, 2016 at 17:18
  • Updated the post with the getView() method. @uguboz Commented Sep 30, 2016 at 18:02
  • I don't see how you define viewType, so I think your problem is like this. e.g. There are 9 items in the list, so 9 items with layout A, then 10th item with layout B. After added an item, the list tries to update 10th item. This item view in not null, it is layout B. But you treated it as layout A (as this is the NOT_LAST_ITEM now). Hope that help! Commented Oct 1, 2016 at 3:57

2 Answers 2

0

As @Rotwang said. Stop adding null item and attach a footer view to the listview as below in the activity/fragment.

View v = getLayoutInflater().inflate(R.layout.footer, null);
listView.addFooterView(v); //IMPORTANT: make sure you call this before setAdapter
listView.setAdapter(adapter);

And dont forget to create a footer layout.

Sign up to request clarification or add additional context in comments.

Comments

0

You can have a ImageView in your XML pointing that listview is empty and make its visibility.gone. In your adapter class check if your dataset is empty, make Imageview Visibility.visible and Listview's visibility change to visibility.gone.

Sample short example

XML

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
<ListView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"/>
<ImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageview"
    android:visibility="gone"/>
</LinearLayout>

Adapter

public class Adapter extends SomeAdapter{

//{
//constructor...
//overriden methods
//whatever you want...
//}

//place where you check if your dasaet is empty

if(dataset.length=0){
 listview.setVisibility(View.GONE);
 imageView.setVisibility(View.VISIBLE);
}
}

1 Comment

I dont think you understood the question. I have a separate xml layout for the last item of the list. I need to know why this error is occuring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.