Xavier Tobin
Xavier Tobin

Reputation: 43

I can't get a ListView to display the names of files in a folder

Thanks in advance for any help,

I'm trying to get a listview on the main activity to display the names of files in a folder, despite doing research here and on tutorials, I just can't get if to work and I don't know why.

Here is the row.xml for the listview row textview:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:gravity="center_vertical"
          android:textColor="@color/black"
          android:paddingLeft="6dip"
          android:minHeight="?android:attr/listPreferredItemHeight"
        />

here is the main_activity.java:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

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

    ListView listView1 = (ListView) findViewById(R.id.actions);

    File directory = new File(extStorageDirectory
            + "/Android/data/com.tobin.backup");

    String[] filenames = directory.list();

    if (filenames == null){

    }

    else{

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, filenames);

        listView1.setAdapter(adapter);

    }

I have no idea what is wrong, thanks for the help!

Edit: Here is the main activity layout: http://pastebin.com/cTkwRCNQ

Upvotes: 3

Views: 1351

Answers (2)

laika
laika

Reputation: 1

Problem seems to be with you activity_main.xml. Try activity_main.xml with only listview in it and setting listviews layout_width and layout_height to fill_parent and the file list should show up. Then modify xml if needed.

Upvotes: 0

Gatekeeper
Gatekeeper

Reputation: 7138

Would you by chance be missing either

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

or

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In your manifest file? Using WRITE_EXTERNAL_STORAGE will give you both read and write permissions.

Upvotes: 1

Related Questions