-1

I'm trying to get folders inside my app for my directory. I got the first part working but the image won't show up. My main goal is to access subdirectory in my folder.

here is my Java code:

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Create extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);

        //Click this it will take you back home
        Button button1 = (Button) findViewById(R.id.Home);

        button1.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                 Intent i = new Intent(Create.this,  MainActivity.class);
                 startActivity(i);
                 Toast.makeText(getApplicationContext(),
                         "You are Home", Toast.LENGTH_LONG)
                 .show();
            }
        });
    }


    //given a path for a folder I want to list all subdirectories under it
    public void addUser(View view){
        String filename = "test";
        String content = "hello";
        String folername ="THE_FOLDER";

        FileOutputStream stream = null;
        //getFilesDir().listFiles();
        try{
            File file = new File(getFilesDir().getAbsoluteFile().toString() + "/" + folername);
            File folder = new File(getFilesDir().getAbsoluteFile().toString());
            File[] listOfFiles = folder.listFiles();
            stream = new FileOutputStream(file);
            stream = openFileOutput(filename,Context.MODE_PRIVATE);
            stream.write(content.getBytes());
            stream.close();
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    Log.d("FILE IS: ",listOfFiles[i].getName());
                } else if (listOfFiles[i].isDirectory()) {
                    System.out.println("Directory " + listOfFiles[i].getName());
                }
            }
        } catch(Exception e){
            e.printStackTrace();
        }

    }

}

Here is my XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:backgroundTint="@android:color/holo_orange_light"
    android:foregroundTint="@android:color/holo_orange_light"
    android:background="#ffffff"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/deleteBtn"
        android:ems="30" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/addBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:backgroundTint="@android:color/holo_red_dark"
        android:shadowColor="#A8A8A8"
        android:onClick="addUser"
        android:text="Create" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_margin="20dp"
        android:layout_marginLeft="19dp"
        android:layout_toRightOf="@+id/editText1"
        android:text="Create Boxes"
        android:textColor="#000000"
        android:textSize="16dp" />

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/addBtn" 
     android:backgroundTint="@android:color/holo_green_dark" />

    <Button
        android:id="@+id/Home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/addBtn"
        android:layout_alignBottom="@+id/addBtn"
        android:layout_margin="10dp"
        android:layout_marginLeft="33dp"
        android:layout_toRightOf="@+id/addBtn"
        android:background="@drawable/home"
        android:textSize="18dp" />

</RelativeLayout>
ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73

1 Answers1

0

What do you mean, "The image won't show up"? Please be as precise as possible. You may need to create a custom listView in this case, to do this you create a new class and new layout resource file for the row design. Check out this: Custom Adapter for List View

Community
  • 1
  • 1
grantespo
  • 2,233
  • 2
  • 24
  • 62