• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Populate Spinner with Dir Listing ... how?

Davemar

Lurker
This prog sends an assets directory listing to a TextView. How do I populate a Spinner with it instead? I want to select an mp3 from the directory to play. I cannot just change TextView to Spinner, since the Eclipse editor objects. ..... Thanks.

Note: I tried putting quotes around each filename in the string, and a comma between them,
so it would resemble the {"file1", "file2", "file3"} format, but no luck!
--------------------------------------------------------------------------
package com.example.read_assets_dir;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//mp3 files are in assets/myfiles dir
AssetManager am = getAssets();

TextView firstField =
(TextView)findViewById(R.id.textView1);

// this line would be omitted if spinner is used
firstField.setText("Directory: ASSETS/MYFILES \n");

try{
String[] files = am.list("myfiles");

for (int i = 0; i < files.length; i++) {
firstField.append("\n File:" + i + " name= " + files);
} //for .... use just files for spinner!!!

} //try

catch (IOException e){
e.printStackTrace();
} //catch

} //Bundle

} //MainActivity
 
I started learning android Mar 8-2013, and have been looking for the solution to the above question for almost 5 weeks. Tonight I modified the tutorial by Ashley, who uses a hard coded array, found at: Spinner sample program in Android

I am posting my own solution to my question above in case someone else might have the same requirement.

---------------------MainActivity.java-------------------------

package com.example.dir2spin;

import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
TextView selection;
String[] items;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
selection = (TextView) findViewById(R.id.selection);

//txt files are in assets/myfiles dir
AssetManager am = getAssets();

try{
items = am.list("myfiles");
}
catch (IOException e){
e.printStackTrace();
} //catch

Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);


ArrayAdapter<String> aa = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
items);

aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);

} //bundle

public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
selection.setText(items[position]);
} //ois

public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
} //ons
} //class

******************** activity_main.xml **********************

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff00cccc"
android:textSize="14sp"
android:textStyle="bold"
android:textIsSelectable="true">
</TextView>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
 
Back
Top Bottom