title: How can a click Listener in a custom ListAdapter modify the lists data?
body: <p>I'm trying to make a relatively simple filesystem browser that displays directories and lets you select them. I have a list fragment which uses a custom adapter to display the directories as a list.  I have created a clickListener for each entry in the list. I need to get it so that when a list entry is clicked, the entire list view refreshes. As this click listener is defined within my adapter though, how can it signal up the stack somehow to the list fragment, in order to tell it to refresh the data that the adapter uses. Help much appreciated. </p>
  
  <p>Here is my code:</p>
  
  <p>PickerFragment.java:</p>
  
  <pre><code>package com.grid.picker;
  
  import android.content.Intent;
  import android.os.Bundle;
  //import android.support.v4.app.Fragment;
  import android.os.Environment;
  import android.support.v4.app.ListFragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  
  import java.io.File;
  import java.io.FilenameFilter;
  import java.util.ArrayList;
  import java.util.List;
  
  import static android.content.Intent.getIntent;
  
  public class PickerFragment extends ListFragment
  {
      private String currentFilesystemPath = "";  // This path does not include the root path
      private int numberOfFolders = 0;
      private List&lt;Folder&gt; folders = new ArrayList&lt;Folder&gt;();
  
  
      public PickerFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
      {
          View rootView = inflater.inflate(R.layout.fragment_main, container, false);
          return rootView;
      }
  
      private void tempPopulate()
      {
          folders.add(new Folder("TestFolder", "/test/path"));
      }
  
      private void populateFolders()
      {
  
          // Create a new File at the current path
          File currentFolder = new File(Environment.getExternalStorageDirectory().getPath()+currentFilesystemPath);
  
          Utility.debugOutput("populateFolders() currentFolder: " +currentFolder);
  
          // Lets get the list of folders
          String[] directories = currentFolder.list(new FilenameFilter() {
              @Override
              public boolean accept(File current, String name) {
                  return new File(current, name).isDirectory();
              }
          });
  
          // Build up the list of folders with folder objects
          for(int i=0; i&lt;directories.length;i++)
          {
              //Utility.debugOutput("Folder:" +directories[i].toString());
              folders.add(new Folder(directories[i].toString(), currentFolder.getPath()+directories[i].toString()));
          }
  
  
      }
  
      public void onActivityCreated(Bundle savedInstanceState)
      {
          super.onActivityCreated(savedInstanceState);
  
  
  
          // Temporary folder population
          //tempPopulate();
          populateFolders();
  
  
  
          /*
          // Set an adapter for this list fragment to use
          setListAdapter(new ArrayAdapter&lt;Folder&gt;(getActivity(),
                  android.R.layout.simple_list_item_activated_1, folders));
          */
  
          // We want to use our custom adapter. We pass in this activity, the layout to use, and an array of folders
          // We give it a new Folder object just so it knows what type of objects are in the array...??
          FolderAdapter adapter = new FolderAdapter(this.getActivity(), R.layout.listview_item_row, folders.toArray(new Folder[0]));
  
  
         // Use our custom adapter for the list
         setListAdapter(adapter);
  
  
  
  
      }
  
  }
  </code></pre>
  
  <p>FolderAdapter.java:</p>
  
  <pre><code>package com.grid.picker;
  
  import android.app.Activity;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class FolderAdapter extends ArrayAdapter&lt;Folder&gt;
  {
      Folder[] folders = null;
      Context context;
      int layoutResourceId;
  
      // Constructor
      // Param 1: Reference of the activity
      // Param 2: The resource id of the layout file we want to use for displaying each ListView item
      public FolderAdapter(Context context, int layoutResourceId, Folder[] data)
      {
          super(context, layoutResourceId, data);
          this.layoutResourceId = layoutResourceId;
          this.context = context;
          this.folders = data;
      }
  
      // This is a function that the click listener can call to start the list update process
      public void updateList(String name)
      {
          Utility.debugOutput("updateList() rx: " +name);
  
  
          this.notifyDataSetChanged();
  
  
      }
  
      // Presumably getView is called for every row in the list ?
      // position is the index of the list item in the list
      @Override
      public View getView(int position, View convertView, ViewGroup parent)
      {
          // Create a new view to play with
          View row = convertView;
  
          final int rowID = position;
  
          FolderHolder holder = null;
  
          if(row == null)
          {
              LayoutInflater inflater = ((Activity)context).getLayoutInflater();
              // Inflate (parse) the layout XML
              row = inflater.inflate(layoutResourceId, parent, false);
  
              //Instantiate a new static object for folder icons (static for speed (caching)
              holder = new FolderHolder();
              holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
              holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
  
              row.setTag(holder);
  
              // Set clickListener
              row.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v)
                  {
                      Utility.debugOutput("Row ID clicked: " +rowID);
                      Utility.debugOutput("Folder array:" +folders[rowID].folderName);
                      updateList(folders[rowID].folderName);
                      //Toast.makeText(context,"you clicked item: "+rowID, Toast.LENGTH_LONG.show();
                      //code you want to execute on click of list item...
                  }
              });
          }
          else
          {
              holder = (FolderHolder)row.getTag();
          }
  
          Folder folder = folders[position];
          holder.txtTitle.setText(folder.folderName);
  
          holder.txtTitle.setText(folder.folderName);
          holder.imgIcon.setImageResource(folder.icon);
  
          /*
          // Add click listener to the view
          View.OnClickListener clickListener = new View.OnClickListener() {
              public void onClick(View v) {
                  Utility.debugOutput("Something clicked???!");
  
                  // This call requires API level 15 minimum...
                  v.callOnClick();
              }
          }; */
  
          //row.setOnClickListener(clickListener);
          return row;
      }
  
      // This class will be used as a cache for the folder images
      static class FolderHolder
      {
          ImageView imgIcon;
          TextView txtTitle;
      }
  }
  </code></pre>
  
  <p>Folder.java:</p>
  
  <pre><code>package com.grid.picker;
  
  
  public class Folder
  {
      // Each instance of this class will be an entry in the folder list
      public String folderName;
      public String fullPath;
      public boolean hasChildren = false; // False by default
  
  
      public static final int icon = R.drawable.folder;
  
      // Constructor
      public Folder(String newName, String newFullPath)
      {
          folderName = newName;
          fullPath = newFullPath;
      }
  
  
  }
  </code></pre>
  
  <p>listview_item_row.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10dp"&gt;
  
      &lt;ImageView android:id="@+id/imgIcon"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:gravity="center_vertical"
          android:layout_alignParentTop="true"
          android:layout_alignParentBottom="true"
          android:layout_marginRight="15dp"
          android:layout_marginTop="5dp"
          android:layout_marginBottom="5dp" /&gt;
  
      &lt;TextView android:id="@+id/txtTitle"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:gravity="center_vertical"
          android:layout_alignParentTop="true"
          android:layout_alignParentBottom="true"
          android:textStyle="bold"
          android:textSize="22dp"
          android:textColor="#000000"
          android:layout_marginTop="5dp"
          android:layout_marginBottom="5dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Eventually I am going to need to put a checkbox on each folder displayed in the list, but for now I need to get it so the folders shown in the list can be clicked on to refresh the display and update the list.  Many thanks. </p>