3

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

Xavier Tobin
  • 43
  • 1
  • 9

2 Answers2

1

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.

Gatekeeper
  • 6,708
  • 6
  • 27
  • 37
  • They are both included. – Xavier Tobin Aug 11 '13 at 07:55
  • Can you check if you drop down into your `filenames == null` statement? Or are the items displaying just not displaying properly? – Gatekeeper Aug 11 '13 at 07:57
  • I'm not sure what your are asking, Im new to this kind of stuff :) – Xavier Tobin Aug 11 '13 at 08:06
  • 1
    Sorry about that =) Add `System.out.println("Filenames is null!");` within your if statement `if (filenames == null)` and see if "Filenames is null!" is ever printed out within the LogCat when you run your app. If it is printed out then we simply need to find why `directory.list();` is returning null. – Gatekeeper Aug 11 '13 at 08:17
  • I did a toast instead of a logcat, and it always says there is more than null - http://pastebin.com/rFXqhDeU – Xavier Tobin Aug 11 '13 at 08:54
  • I know your Original Post uses `android.R.layout.simple_list_item_1` for the row layout, but the link you just posted uses `R.layout.row`. If you are getting an array with valid strings inside of it, using `android.R.layout.simple_list_item_1` should be able to display them within your `ListView`. Once you get at least something displaying in the `ListView` then it won't be hard to switch it over to using a custom row layout. – Gatekeeper Aug 11 '13 at 09:24
  • I've changed it back, but I've turned that code into comments for now, because `directory.list()` is always > 0, but the Integer of directory.length is reporting the number of files in the folder accurately :( thanks for the help! – Xavier Tobin Aug 11 '13 at 09:48
  • Could you try changing your xml and code to follow what this person has done and see if you can get it working? http://stackoverflow.com/questions/4407865/how-to-customize-listview-row-android – Gatekeeper Aug 12 '13 at 14:09
0

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.

laika
  • 1
  • 1
  • 1
  • Ive made that change, still the same, for some reason directory.list() is always > 0 (Even if the number of files is 0), but the Integer of directory.length is reporting the number of files in the folder accurately :( – Xavier Tobin Aug 11 '13 at 11:34
  • why are you using android.support.v4.widget.DrawerLayout?ndroid.support.v4.widget.DrawerLayout in your xml? RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" seems to work – laika Aug 11 '13 at 11:56