user9463351
user9463351

Reputation:

How i can display a message when listview is empty

I am working on a project which fetches the files from the SD card. All fetches do work perfectly but there is a problem once I want to add a condition that if listview is empty then the program should show ** There is nothing to view**. But I don't know how I can do that.

code example:

 ListView lv;
    ArrayList<String> FilesInFolder = GetFiles( Environment.getExternalStorageDirectory() +"/" + "foldername"+"/"+"file.txt");
    lv = (ListView)findViewById(R.id.lit_view);
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, FilesInFolder));

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Intent intent = new Intent(getApplicationContext(), pdfviewer.class);
            intent.putExtra("FileName", (String)parent.getItemAtPosition(position));
            startActivity(intent);
            finish();
                       }
    });
}
public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i=0; i<files.length; i++)
            MyFiles.add(files[i].getName());
    }

    return MyFiles;
}

Upvotes: 0

Views: 343

Answers (4)

kavinesh
kavinesh

Reputation: 1

declare a empty view which you want to show when list is empty:

TextView emptyView;

at OnCreate:

emptyView = findViewById(R.id.emptyview);

get position of listview in which case you have show alternative view:

if (List.get(pos).getListPosition() == null) {
                        primaryView.setVisibility(View.GONE);
                        emptyView.setVisibility(View.VISIBLE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        primaryView.setVisibility(View.VISIBLE);
                        primaryView.setAdapter(pAdapter);
                        adapter.notifyDataSetChanged();
                    }

Upvotes: 0

Syed Naeem
Syed Naeem

Reputation: 693

in your xml file where you have listview you can add a textview with a text "there is nothing to view" or "press + to add record" and center it in the parent

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/emptyMessage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="press + to add record" />
</RelativeLayout>

once you are done with the this next thing is to show and hide this textview whenever your list is empty, in your case you are using ArrayList, so you can do this

findViewById(R.id.emptyMessage).setVisibility(FilesInFolder.size() == 0 ? View.VISIBLE : View.INVISIBLE);

incase this statement confuses you, this statement is a boolean check / or you can say it is a way of writing if/else in a single line so you need to call this statement when you are done finalizing your list, in your case i believe this statement should be come after listview creation, hope it is what you wanted

Upvotes: 1

Stanislav Batura
Stanislav Batura

Reputation: 420

As i understand you problem, i can advice smth like that. You should add EmptyView in your activity.xml, it maybe ImageView or TextView as you wish. Add a methode that checks if there are no files to add. And then set EmptyView to your ListView.

if (noFiles) {        
        ListView exempleListView = (ListView) findViewById(R.id.list);
        // Find and set empty view on the ListView, so that it only shows when the list has 0 items.
        View emptyView = findViewById(R.id.empty_view);
        petListView.setEmptyView(emptyView); 
}

Upvotes: 0

Viswanath Kumar Sandu
Viswanath Kumar Sandu

Reputation: 2274

        ListView lv;
        ArrayList < String > FilesInFolder = GetFiles(Environment.getExternalStorageDirectory() + "/" + "foldername" + "/" + "file.txt");
        lv = (ListView) findViewById(R.id.lit_view);


        // This will do the needful 

        if (FilesInFolder == null || FilesInFolder.isEmpty()) {
            lv.setVisibility(View.GONE)
            Toast.makeText(this, "Their is nothing to view", Toast.LENGTH_SHORT).show()
            // Add another textView in xml and make it visible
        } else
            lv.setAdapter(new ArrayAdapter < String > (this,
                android.R.layout.simple_list_item_1, FilesInFolder));
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView << ? > parent, View v, int position, long id) {

                Intent intent = new Intent(getApplicationContext(), pdfviewer.class);
                intent.putExtra("FileName", (String) parent.getItemAtPosition(position));
                startActivity(intent);
                finish();
            }
        });
    }

Upvotes: 0

Related Questions