I am attempting to obtain all of the files inside of a directory and display them inside of a spinner. I am trying to use the code below but can't figure out how to display it inside of the spinner.
File("sdcard/Velocity").walkTopDown().forEach { println(it) }
To get the files as
public String[] getFiles(String path) {
File directory = new File(path);
File[] files = directory.listFiles();
String arr[] = new String[files.length];
for (int i = 0; i < files.length; i++) {
fileList[i] = files[i].getName();
}
}
To show the in activity
<Spinner
android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
To populate
public class SpinnerExample extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] arraySpinner = getFiles();
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
}
}
Related
so i've got this piece of code and i want it to output attr1 and attr2 in the listview, but the current error i get is: cannot resolve constructor. Here is the piece of code:
private String[][] content = {
{"attr1", "url1"},
{"atrr2", "url2"}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ListView lv = (ListView) findViewById(R.id.lv);
for(int i = 0; i < content.length; i++) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, content[i][0]);
lv.setAdapter(adapter);
}
}
Hopefully somebody could help me, thanks in advance (sorry for the bad english)
Move the creation of the list outside your for loop like so:
ListView lv = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
for(int i = 0; i < content.length; i++) {
adapter.add(content[i][0]);
}
lv.setAdapter(adapter);
You can get the index of the clicked item and use that information to access your array again and get the second piece of information
public class MainActivity extends Activity {
private GestureDetector gestureDetector;
String dateData;
int choice ;
public HashSet<String> keyList = new HashSet<String>();
public ArrayList<String> temperatures = new ArrayList<String>();
public ArrayList<String> time = new ArrayList<String>();
public ArrayList<String> atList=new ArrayList<String>();
public ArrayList dataList=new ArrayList();
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this,new SwipeGestureDetector());
Spinner toList = (Spinner) findViewById(R.id.toList);
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
........................
protected void onPostExecute(String result)
{
Dialog.dismiss();
Iterator<String> iter = keyList.iterator();
while(iter.hasNext())
{
String key =iter.next();
dataAdapter.add(key);
dataAdapter.notifyDataSetChanged();
}
......................
..............................................................................
Sorry for this silly question , but i hav been doing it again and again from 0. it still doesn't get what i want. WHat problem i having now is bout the spinner didn't update itself after i set dataAdapter.notifyDataSetChanged().
Before this it work but my table isn't working .
now my table is working, this spinner not working. Omg
really need help desperately.
It seems that you are trying to add toList.setAdapter(dataAdapter) before initialize dataAdapter.So set dataAdapter after getting data in it,i.e. change
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
to
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
I'm trying to show files in list view :
It works perfectly. Now I'm trying to add TextView to this Listview so I tried
this i:
I don't want to use adapter class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv =(TextView) findViewById(R.id.textView2);
path = Environment.getExternalStorageDirectory().getPath();
file = new File(path);
setListView();
}
public void setListView(){
String[] mFilesList = file.list();
mListView = (ListView)findViewById(R.id.listView1);
mArray = new ArrayList<String>();
fArray = new ArrayList<String>();
for(int i = 0; i<mFilesList.length; i++){
mArray.add(mFilesList[i]);
}
for (int i = 0; i < mFilesList.length; i++) {
fArray.add(mFilesList[i].length()+" files");
}
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(getApplicationContext()
, R.layout.rows, R.id.textView1,mArray);
ArrayAdapter<String> mAdapterSecond = new ArrayAdapter<String>(getApplicationContext()
, R.layout.rows, R.id.textView2,fArray);`
mListView.setAdapter(mAdapterSecond);
mListView.setAdapter(mAdapter);
It's showing just one textview. Why?
The ListView accepts only one Adapter. Your last code line changes the ListView adapter from mAdapterSecond to mAdapter. What you need in your case is to implement your custom adapter that fill the two TextView as you wish.
i trying tu put some elements from database to a List view,
my problem is that when i starrt my activity, I get this:
**com.example.restaurant.Restaurant#2be2d1d0
com.example.restaurant.Restaurant#2be3d3a8**
instead of database objects.
public class Liste extends Activity{
private ListView listview;
private Button boutonAjouter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_restaurant);
listview = (ListView)findViewById(R.id.listView1);
Restaurant mcdo = new Restaurant("mcdo","brossard","take-out","fastfood","450-555-5555");
Restaurant burger = new Restaurant("burger","longueuil","take-out","fastfood","450-999-9999");
RestaurantBDD restaurantBDD = new RestaurantBDD(this);
restaurantBDD.openForWrite();
restaurantBDD.insertRestaurant(mcdo);
restaurantBDD.insertRestaurant(burger);
ArrayList <Restaurant> restaurantlist = restaurantBDD.getAllRestaurants();
restaurantBDD.close();
ArrayAdapter <Restaurant> adapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, restaurantlist);
listview.setAdapter(adapter);
boutonAjouter = (Button) findViewById(R.id.btn_nouveau);
boutonAjouter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Liste.this, Formulaire.class);
startActivity(intent);
}
});
}
}
If you want to show a list with the names of the restaurants, supposing that your Restaurant class contains a string attribute "name", you should first create an array containing the names of the restaurants and then use an ArrayAdapter <String>:
String[] values = new String[restaurantlist.size()];
for(int i=0;i<restaurantlist.size();i++) {
values[i] = restaurantlist.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);
Once a value is selected from the spinner, I am looking to have another list of values populate the spinner from the selection made. For example: When the user clicks the spinner the values "Home Team, Home Subs, Home Other" comes up. The user clicks one of these and then the players that are affiliated with that selection then populate the spinner. Below is code for the original spinners populated.
public class ExampleMain extends Activity {
JSONArray jsonArray = null;
JSONArray str_login = null;
public String kode;
public String Team_Name;
public String Home_team;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
JSONParser jParser = new JSONParser();
String link_url = "http://10.0.2.2/NPD/P_name.php";
JSONObject json = jParser.FunctionParser(link_url);
Spinner d11 = (Spinner)findViewById(R.id.doubles11);
Spinner d12 = (Spinner)findViewById(R.id.doubles12);
Spinner d21 = (Spinner)findViewById(R.id.doubles21);
Spinner d22 = (Spinner)findViewById(R.id.doubles22);
Spinner d31 = (Spinner)findViewById(R.id.doubles31);
Spinner d32 = (Spinner)findViewById(R.id.doubles32);
try {
jsonArray = json.getJSONArray("team");
final String[] items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i]=jsonObject.getString("P_name");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d11.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d12.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d21.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d22.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d31.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d32.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
Thanks.
You need to use OnItemSelectedListener
d11.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
item = spinner.getItemAtPosition(position).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
Now you will have your value and you can use that wherever you need to populate the other Spinners. Make item in this example a member variable so you can use it anywhere in this Activity. You will also need to add implements OnItemSelectedListener to your class definition
public class ExampleMain extends Activity implements OnItemSelectedListener{
Also, you are initializing adapter many times with the same values it looks like. Just initialize it once then set your Adapters on your Spinners