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?
Related
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
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);
}
I am trying to display my list of alarms in the ListActivity but it does not work. There rows appear but there is no text visible. I'm not sure if it's retrieving the data from the HashMap and inserting into the rows correctly.
Here is my code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_list);
List<Alarm> list;
Database db = new Database(AlarmListActivity.this);
list = db.getAllAlarms();
List<HashMap<String, String>> alarmMap = new ArrayList<HashMap<String, String>>();
for (Alarm alarm: list) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Integer.toString(alarm.getID()), alarm.toString());
alarmMap.add(map);
}
if(!list.isEmpty() && list != null)
{
System.out.println("inside list");
System.out.println(list);
ListAdapter adapter = new SimpleAdapter(
AlarmListActivity.this,
alarmMap,
R.layout.list_item,
new String[] { "id", "hour"},
new int[] { R.id.id, R.id.time});
setListAdapter(adapter);
}
else
{
Toast.makeText(AlarmListActivity.this, "No Alarms Set",
Toast.LENGTH_SHORT).show();
}
}
This is what I get. You can vaguely see the lines of each row. But there is no text or anything. All the System.out.println() work as expected which print out the HashMap. It's just nothing is being displayed in the list. I've even added: android:text="Testing" to no avail
I'm new to Android and I have Adapter - ListView model to display array.
I set it's value's so:
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.bank_list,
new String[] { TAG_NAME, TAG_central_office_address }, new int[] {
R.id.bank_name, R.id.central_office_address});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, android.view.View view,
int position, long id) {
Intent in = new Intent(getApplicationContext(), BankExchangersListActivity.class);
in.putExtra("Bank_id", TAG_ID);
startActivity(in);
}
});
I get my values from JSON:
url = "http://192.168.1.4:3000/banks.json";
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
banks = json.getJSONArray(TAG_BANKS);
// looping through All Contacts
for(int i = 0; i < banks.length(); i++){
JSONObject c = banks.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String central_office_address = c.getString(TAG_central_office_address);
// Phone number is agin JSON Object
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_central_office_address, name);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
As you can see in JSON I have ID field, but i didn't set it in ListView. For next query I click on ListView element and go to other activity, but I need to get TAG_ID of this element. Maybe I did something wrong? How to get element's TAG_ID on which I click, without displaying it on ListView?
JUST send tag_id of clicked item!
The position parameter of the onItemClick method gives the index of the selected item in the adapted list. So you can find the TAG_ID of the original item at
((Map<String, String>) adapter.getItem(position)).get(TAG_ID)
make the adapter final so that you can reference it inside the listener:
final ListAdapter adapter = ...
Hope this helps you.
you can get Postion of ListSelectedItemId and passed it with Intentand get it in SecondActivity now Here get FirstActivity HashMap with define it static
now use foreachloop to get all values from HashMap like
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}
now with specific position of ListSelectedItemId you can get values from currentList
or like
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
It's connecting and pulling the data from the json array. Its adding two listview items but not showing any text?
JSON array
{"tournaments":[{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":"Kansas","Category":"Roulette"},{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":"Connecticut","Category":"Three Card Poker"}]}
And some of my code in java...
JSONObject json = JSONfunctions.getJSONfromURL("http://kleinefrosch.com/retrieve.php");
try{
JSONArray tourneys = json.getJSONArray("tournaments");
for(int i=0;i<tourneys.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = tourneys.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("Title:" , e.getString("Title"));
map.put("Contact:" , e.getString("Contact"));
map.put("Description:" , e.getString("Description"));
map.put("City:" , e.getString("City"));
map.put("State:" , e.getString("State"));
map.put("Country:" , e.getString("Country"));
map.put("Category:" , e.getString("Category"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "Title","Contact","Description","City","State","Country","Category" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(JsonActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
Try to remove map.put("id", String.valueOf(i));
Hope it runs then.
EDITED
Your Mistake is Here
map.put("Title:" , e.getString("Title"));
it should be
map.put("Title" , e.getString("Title"));
As it is Key which will be Matched in SimpleAdapter.
Like this remove : from each string value.
So please make sure that both the Keys match perfectly.
Assuming that JSON string parsing is completed without any exceptions, I think that you should use this construction:
setListAdapter(new ArrayAdapter<String>(Context context, int resource, int textViewResourceId, List<T> objects);
For more info about go to developers guide
Post your Xml code, are you using any Customized Listview? If it is means add some Dark color for Textview.