I want to populate a Spinner with items which have a main text and a sub text, just like Android Studio shows when building the view on the "Designer" tab.
So far I was able to fill it with the main text only.
I am doing it via code. Using a SimpleAdapter.
I tried the following but with no success, it just gives me same result (only main text):
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
List<Map<String, String>> itens = new ArrayList<>();
Map<String, String> item = new HashMap<>(2);
item.put("text", "MAIN TEXT");
item.put("subText", "SUB TEXT");
itens.add(item);
SimpleAdapter adapter = new SimpleAdapter(spinner.getContext(), itens,
android.R.layout.simple_spinner_dropdown_item,
new String[]{"text", "subText"},
new int[]{android.R.id.text1, android.R.id.text2}
);
// i am not sure what this does
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I had the same problem and have used the OP's code as a base to create this solution:
final Spinner spinner = (Spinner)fragmentView.findViewById(R.id.spinner);
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
Map<String, String> item0 = new HashMap<String, String>(2);
item0.put("text", "Browse aisles...");
item0.put("subText", "(Upgrade required)");
items.add(item0);
Map<String, String> item1 = new HashMap<String, String>(2);
item1.put("text", "Option 1");
item1.put("subText", "(sub text 1)");
items.add(item1);
Map<String, String> item2 = new HashMap<String, String>(2);
item2.put("text", "Option 2");
item2.put("subText", "(sub text 2)");
items.add(item2);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), items,
android.R.layout.simple_spinner_item, // This is the layout that will be used for the standard/static part of the spinner. (You can use android.R.layout.simple_list_item_2 if you want the subText to also be shown here.)
new String[] {"text", "subText"},
new int[] {android.R.id.text1, android.R.id.text2}
);
// This sets the layout that will be used when the dropdown views are shown. I'm using android.R.layout.simple_list_item_2 so the subtext will also be shown.
adapter.setDropDownViewResource(android.R.layout.simple_list_item_2);
spinner.setAdapter(adapter);
You can also substitute android.R.layout.simple_spinner_item and/or android.R.layout.simple_list_item_2 with your own custom views (that would typically reside in your layout folder).
This is a much better solution than PhoneGap!! :D
You will have to create a custom ArrayAdapter that creates a custom view for your Spinner dropdown. This link gives a good example: How to customize the Spinner dropdown view
Related
I am trying to fill a ListView with Items. Everything seems to work perfectly but ListView doesn't show Items. I am wondering if I miss a step here.
Please take a look at my code below;
ListView listView = (ListView) myView.findViewById(R.id.listViewLocations);
List<WMS_Location> list = (List<WMS_Location>) response.body();
String listViewItems[] = new String[list.size()];
for (int i=0;i<list.size();i++)
{
listViewItems[i]= list.get(i).getCode().toString().toUpperCase();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, listViewItems);
listView.setAdapter(adapter);
NOTE: I am sure that listViewItems is not empty and it has 5 string object in it.
I am a beginner in Android studio and I am having hard time to debug this issue.
when any item in my ListView gets clicked the application crashes, here is the class for the OnItemClick. The objective is to change the ListView from the Categories to the items inside the categories. The Categories and items are stored in a HashMap <String, String[]>, so passing the value of cat.toString() should return the string containing the category. Lastly using myList (Stored outside of the function) should return an array of string containing the items. However, when I click any of the items the application instantly crashes. Thanks for your help!
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Choosen category
TextView cat = (TextView) view;
ArrayList<String> tempItems = new ArrayList<String>();
//Toast.makeText(this, cat.getText().toString(),Toast.LENGTH_SHORT).show();
listAdapter.clear();
for(String val : myList.get(cat.getText().toString())){
tempItems.add(val);
}
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tempItems);
listAdapter.notifyDataSetChanged();
}
Variables
ListView myViewList;
Map<String, String[]> myList = new HashMap<String, String[]>();
ArrayAdapter<String> listAdapter;
Populating the ListView
//Creating a list of all categories
Set<String> myListKeys = myList.keySet();
ArrayList<String> categories = new ArrayList<String>();
for(String val : myListKeys){
categories.add(val);
}
//String[] categories = myListKeys.toArray(new String[myListKeys.size()]);
//Populating list
myViewList = (ListView) findViewById(R.id.groceryList);
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,categories);
myViewList.setAdapter(listAdapter);
myViewList.setOnItemClickListener(this);
You are getting exception because you are using listAdapter.clear(); which tries to clear the List inside the adapter. But apparently you have passed an array (String[] categories) which cannot be cleared. You should convert the array to ArrayList, (not just List) by iterating through it and pass it into the adapter.
You should create the adapter by passing ArrayList
What do you think you're doing by creating a new adapter inside the onItemClick of old adapter?
Inside the onItemClick, don't create a new adapter. Instead, after clearing the adapter and creating a temp ArrayList, add all the temp ArrayList to the adapter.
listAdapter.addAll(tempItems);
Also to get text from a TextView, use cat.getText().toString(); instead of cat.toString();
I'm trying to add a different icon to each of my list items but I'm having trouble. The idea was to have each of the list view items together to make editing easier but adding an image is proving to be more complicated than I thought.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "title", "description" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Title 1", "Description 1"));
list.add(putData("Title 2", "Description 2"));
list.add(putData("Title 3", "Description 3"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
This is if not impossible, then at least quite troublesome at your current setup.
As the name states a SimpleAdapter is a basic class which provides you with basic functionality. That is, usually - a list with text views.
When you create the adapter you specify an exact layout for every single item in it (that's your android.R.layout_simple_list_item_2). You cannot push any additional items there (unless you're really stubborn).
What you need:
A custom made adapter (preferably)
A custom made layout for the adapter
A data source in the adapter which will map specific icons to every element.
Here is a nice demo: http://hmkcode.com/android-custom-listview-titles-icons-counter/
you can use custom adaptor in listview . so can customize your row.
check this
Currently using simple_list_item_2.xml for ListView. this layout contains only two TextView's. so it's not possible to show image in ListView row's using simple_list_item_2.xml layout
How to I add an image to list view items in Android?
Should create a custom adapter :
1. Creating a custom layout with required views which want to show in each listview row like with TextView,ImageView,...
2. Create a custom adapter class by extending SimpleAdapter class to change behavior of getView method for showing images and textview from data-source
See following tutorial for reference :
ListView with Images and Text using Simple Adapter in Android
I have a ListView, and the list view is populated with an HashMap. I can add text without in issue. I have an issue adding spinners.
How would I go about adding spinners to a HashMap in Android?
Here is the code for my HashMap:
final Spinner hashSpinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);
hashSpinner.setAdapter(hashSpinner);
mapTwo = new HashMap<String, String>();
mapTwo.put("zero", "OK");
mapTwo.put("one", " 2");
mapTwo.put("two", " 3");
listTwo.add(mapTwo);
try {
adapter = new SimpleAdapter(this, listOne, R.layout.row_two,
new String[] { "zero", "one", "two" }, new int[] {
R.id.spinner1, R.id.spinner2, R.id.spinner3 });
listViewOne.setAdapter(adapter);
} catch (Exception e) {
}
row_two already contains the spinners, the issue is, I can't get it to display. How will I go about adding the spinners to the ListView?
I recently created a new ListView object for an Android application, but I am encountering some errors. When I try using a Simple Adapter to create an Item that includes a subitem on my list, The newest item created overlaps the other ones. I am using a List of Maps to create the items.
For example, If I add an item to my map list that displays "1" with a subitem that displays "A1", the item and subitems will be displayed. But if I add an new item to my map list named "2" with a subitem "B2", the "1" and "A1" will be replaced with "2" and "B2". There will still be 2 items on the ListView, but one of them is empty and the other one is "2" and "B2"
Here is my code:
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.listView1);
Map<String, String> datum = new HashMap<String, String>();
datum.put("RouterName", "Test1");
datum.put("RouterIP", "SubTest1");
data.add(datum);
Map<String, String> datum2 = new HashMap<String, String>();
datum.put("RouterName", "Test2");
datum.put("RouterIP", "SubTest2");
data.add(datum2);
SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"RouterName", "RouterIP"}, new int[] {android.R.id.text1, android.R.id.text2});
lv.setAdapter(adapter);
}
Changing the list type will not work because the Simple Adapter specifically uses a Map List.
Hi I found some minor mistakes while storing in Hashmap. In that HashMap object is not creating. Previous instance is getting deleted.
Kindly use the below code. Hope it will help you.
ArrayList<HashMap<String, String>> data;
private String[] titleArray,subItemArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
ListView lv = (ListView)findViewById(R.id.listview);
data = new ArrayList<HashMap<String, String>>();
titleArray=new String[]{"Test1","Test2"};
subItemArray=new String[]{"SubTest1","SubTest2"};
for(int i=0;i<titleArray.length;i++){
HashMap<String,String> datum = new HashMap<String, String>();
datum.put("RouterName", titleArray[i]);
datum.put("RouterIP", subItemArray[i]);
data.add(datum);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"RouterName", "RouterIP"}, new int[] {android.R.id.text1, android.R.id.text2});
lv.setAdapter(adapter);
}