Question

What I want is to get as much textviews (listrows) as much the number of items. means for loop. Please help me out CODERS. I was getting only last item, so I "hard-coded" it.

public class AllNotes extends Activity {

Button button;
TextView textview;

// trial copy code from other from net
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

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

    String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
    Log.d("Files", "Path: " + path);
    File f = new File(path);        
    File file[] = f.listFiles();
    Log.d("Files", "" + file.length);
    Toast.makeText(this, "" + file.length ,Toast.LENGTH_SHORT).show();

    for (int i=0; i < file.length; i++)
    {

     // trial copy code from other from net
        // Find the ListView resource. 
        mainListView = (ListView) findViewById( R.id.mainListView );

        ArrayList<String> planetList = new ArrayList<String>();     
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

        listAdapter.add(file[0].getName());
        listAdapter.add(file[1].getName());
        listAdapter.add(file[2].getName());
        listAdapter.add(file[3].getName());
        listAdapter.add(file[4].getName());

     // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter ); 

        Log.d("Files", "FileName:" + file[i].getName());
        Toast.makeText(getApplicationContext(), file[i].getName(),Toast.LENGTH_LONG).show();
    }

    /*// Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter ); */     
  }
}
Was it helpful?

Solution 2

As Der Golem has stated just use file[i] however you will also need to declare listAdapter before your for loop or it will re-assign the values each time you iterate through the loop!

Here's a quick guide that should help you:

    mainListView = (ListView) findViewById( R.id.mainListView );

    ArrayList<String> planetList = new ArrayList<String>();     
    // Create ArrayAdapter using the planet list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

    for (int i=0; i < file.length; i++)
    {
       listAdapter.add(file[i].getName());
    }

 // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter ); 

    Log.d("Files", "FileName:" + file[i].getName());
    Toast.makeText(getApplicationContext(), file[i].getName(),Toast.LENGTH_LONG).show();          

Hope this helps.

OTHER TIPS

Perhaps the problem was in for loop you were creating always new Listview and arrayadapter

Try with following

String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
        Log.d("Files", "Path: " + path);
        File f = new File(path);        
        File file[] = f.listFiles();
        Log.d("Files", "" + file.length);
        Toast.makeText(this, "" + file.length ,Toast.LENGTH_SHORT).show();
        ListView mainListView = (ListView) findViewById( R.id.mainListView );
        ArrayList<String> planetList = new ArrayList<String>(); 


        for (int i=0; i < file.length; i++)
        {   
            planetList.add(file[i].getName());
        }
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter ); 

        Log.d("Files", "FileNames:" + planetList);
        Toast.makeText(getApplicationContext(), planetList.toString(),Toast.LENGTH_LONG).show();
Button button;
TextView textview;

// trial copy code from other from net
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

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

    String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;

    File f = new File(path);        
    File file[] = f.listFiles();

    ArrayList<String> planetList = new ArrayList<String>();     

    for (int i=0; i < file.length; i++) {
        planetList.add(file[i].getName());
    }

    // Create ArrayAdapter using the planet list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

    // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.mainListView );

    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter ); 

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top