Emmanuel Okocha
Emmanuel Okocha

Reputation: 397

show something when listview is empty

I have a listview that shows up when a fragment is opened being updated by a saved list. Initially that list is empty so it spits out an error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0. So i want to show show something like a text saying something like empty so that when there is something in the list it will display. I have tried using the getEmptyView but to no avail. Can i please get some help on how to accomplish this thank you.

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View customView = inflater.inflate(R.layout.fragment_reading_monday, container, false);

    addNewButton = (Button) customView.findViewById(R.id.btnAddNewReadingMonday);
    relativeLayoutMonday = (RelativeLayout) customView.findViewById(R.id.frameLayoutMonday);
    listViewMonday = (ListView)customView.findViewById(R.id.listViewMonday);
    listContainerMonday = (RelativeLayout)customView.findViewById(R.id.listContainerMonday);
    tinyDB = new TinyDB(getContext());
    addNewSubject = (Button)customView.findViewById(R.id.btnAddNewSubjectMonday);
    dataModels = new ArrayList<>();
    arrayListSubjectsRead = new ArrayList<>();
    timeOne = new ArrayList<>();
    timeTwo = new ArrayList<>();
    adapter = new CustomListAdapterReading(getContext(), dataModels);

        TextView empty = (TextView) customView.findViewById(R.id.emptyTextView);
        listViewMonday.setEmptyView(empty);


            arrayListSubjectsRead =  tinyDB.getListString("ArrayForSubjects");
            timeOne = tinyDB.getListString("ArrayForTimeOne");
            timeTwo = tinyDB.getListString("ArrayForTimeTwo");

            dataModels.add(new DataModelReading(arrayListSubjectsRead, timeOne, timeTwo));

            adapter = new CustomListAdapterReading(getContext(), dataModels);
            listViewMonday.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            System.out.println(" subjects: "+arrayListSubjectsRead);
            System.out.println("timeone: "+ timeOne);
            System.out.println("timetwo: "+timeTwo);









    addNewButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            relativeLayoutMonday.removeView(noEventTextView);
            Intent intent = new Intent(getActivity(),ReadMain.class);
            startActivity(intent);

        }
    });

    addNewSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });




    return customView;
}

Upvotes: 0

Views: 782

Answers (3)

Lokesh Desai
Lokesh Desai

Reputation: 2677

I am using this way in my application

ListView listView = (ListView)findViewById(android.R.id.listView);
TextView noData = (TextView)findViewById(android.R.id.noData);
listView.setEmptyView(noData);

Listview will automatically set no data text when adapter is empty.

Upvotes: 1

Rachik Abidi
Rachik Abidi

Reputation: 156

I want you to check this post setEmptyView on ListView not showing its view in a android app, it should solve your empty view issue. And if you want to prevent the app from throwing an IndexOutOfBoundsException, you can simply check the size of your dataModels array before setting up your adapter.

if (dataModels.size() > 0)
{
    //Setup our adapter here
}

Hope this helps :)

Upvotes: 2

CBJul
CBJul

Reputation: 31

Something like this ?

ImageListAdapter imageListAdapter= new ImageListAdapter();
listviewObject.setAdapter(imageListAdapter);

if (imageListAdapter!= null)
    if (imageListAdapter.getCount() > 0){
        // implement your work
    }else {
        // do whatever you want on empty list adapter 
    }

Upvotes: 1

Related Questions