Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:08:36+00:00 2026-05-30T02:08:36+00:00

I am making an android application that needs to display all of the items

  • 0

I am making an android application that needs to display all of the items in a specific directory on the sd-card onto a listview. I have gone though several tutorials but none seemed to give me any help. I have managed to add and delete things from my sd card and listview. But i need to show the items(files) from a directory onto the listview. I am using a dynamic listview. Please help and thanks SO much in advance! This is the code that i am using so far and i need to read the items on the onCreate method.

public class NotesActivity extends ListActivity implements OnClickListener {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
Button AddItemToListView, AddItemToListViewButton, CancelButton, DeleteButton,CancelButton2, DeleteAllButton;
LinearLayout AddItemToListViewLinearLayout, DeleteItemFromListViewLinearLayout, DeleteAllItemsFromListViewLinearLayout;
public int DeleteIndexNumber;
public String NameOfSaveItemToSdCard = "";
public String NameOfDeleteItemFromSdCard = "";
public int DeleteIndexNumber2;
  static final String[] COUNTRIES = new String[] {
      "Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
      };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes);
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
        DeleteIndexNumber = position;
        DeleteIndexNumber2 = position;
        NameOfDeleteItemFromSdCard = myList.get(position);
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.VISIBLE);
  }
    });
  }
@Override
public boolean onCreateOptionsMenu(Menu meny) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.listviewmenubuttons, meny);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.AddItemToListView:
        AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
        CancelButton = (Button)findViewById(R.id.CancelButton);
        DeleteButton = (Button)findViewById(R.id.DeleteButton);
        CancelButton.setOnClickListener(this);
        DeleteButton.setOnClickListener(this);
        AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
        AddItemToListViewButton.setOnClickListener(this);
        AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
        break;
    case R.id.DeleteAllNotes:
        DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
        DeleteAllItemsFromListViewLinearLayout.setVisibility(View.VISIBLE);
        CancelButton2 = (Button)findViewById(R.id.CancelButton2);
        DeleteAllButton = (Button)findViewById(R.id.DeleteAllButton);
        CancelButton2.setOnClickListener(this);
        DeleteAllButton.setOnClickListener(this);
        break;
        }
    return true;
}
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.AddItemToListViewButton:
        AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
        myList.add(AddItemToListViewEditText.getText().toString());
        NameOfSaveItemToSdCard = AddItemToListViewEditText.getText().toString();
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
        AddItemToListViewEditText.setText("");
        AddItemToListViewEditText.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        AddItemToListViewLinearLayout.setVisibility(View.GONE);
        //Check if directory exists
        checkIfDirectoryExist();
        break;
    case R.id.CancelButton:
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.DeleteButton:
        myList.remove(DeleteIndexNumber);
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();
        File f = new File(Environment.getExternalStorageDirectory() + "/SchoolAppNotes/" + NameOfDeleteItemFromSdCard);
        if(f.exists()) {
            boolean deleted = f.delete();
        }
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.DeleteAllButton:
        myList.removeAll(myList);
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();
        DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
        DeleteAllItemsFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.CancelButton2:
        DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
        DeleteAllItemsFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    }
}
private void checkIfDirectoryExist() {
    // TODO Auto-generated method stub
    File f = new File(Environment.getExternalStorageDirectory() + "/SchoolAppNotes");
    if(f.exists()) {
        try {
            OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()
                     + "/SchoolAppNotes/" + NameOfSaveItemToSdCard);
            Toast.makeText(getApplicationContext(), "File created:-)",
                    Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getApplicationContext(), "We failed to create the file",
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    else {
        //Create directory
        File folder = new File(Environment.getExternalStorageDirectory() + "/SchoolAppNotes");
        boolean success = false;
        if(!folder.exists())
        {
            success = folder.mkdir();
        }         
        if (!success) 
        { 
            // Do something on success
//Writing file...(It doesn't work)
        }
        else 
        {
            // Do something else on failure 
        }
        checkIfDirectoryExist();
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:08:36+00:00Added an answer on May 30, 2026 at 2:08 am

    I assume that myList is supposed to be holding the file names from your directory? It looks like you never instantiated it.

    To do that you’ll need to get a list of your file names and load it in to there before you use it to make an adapter.

    So add something like this before your .setAdapter() calls:

    File mFile = new File(Environment.getExternalStorageDirectory() + "yourDirectory");
    myList = mFile.list();
    

    You should be good to go if that fills your array correctly.

    p.s. File.list() docs

    EDIT:

    Whoops, didn’t notice the type on myList. Use this instead

    myList = Arrays.asList(mFile.list());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an android application that needs to use a ListView . I
I am making an android application that needs to add items to a dynamic
I am making an android application in which i have five items in a
I'm making an application for Android that needs to react to when the device
I am making an android application that uses a listview. I want to get
So I'm making this android application, that needs to read data from user-provided CSV
I am making an android application that will play an mp3 file once a
I'm in the process of making an Android application that creates a new file
i am making one application in Android 2.2 in that application i want to
I am making an android application in which I have put 10 images and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.