I currently have a problem with my two-line ListView. The two-line ListView shows the list separator, however, there is no data or text inside it. Where could I possibly have done wrong?
For additional information, the results.get(i) will show Tungro,18.92%, for example.
ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
HashMap<String, String> listItemData;
for (int i=0; i<results.size(); i++) {
if (results.size() != 0) {
listItemData = new HashMap<String, String>();
String resultStr = results.get(i).toString();
String[] resultStrVar = resultStr.split(",");
listItemData.put(resultStrVar[0], resultStrVar[1]);
listItems.add(listItemData);
} else {
listItemData = new HashMap<String, String>();
listItemData.put("No predictions found", "Kindly shot again");
listItems.add(listItemData);
}
}
SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,
android.R.layout.simple_list_item_2,
new String[] {"First Line", "Second Line"},
new int[] {android.R.id.text1, android.R.id.text2 });
listView.setAdapter(adapter);
Below is the screenshot of the ListView, where you could see the list separator but without text or data:
Is there something wrong with how I tokenize the string or with the adapter? Thanks a lot.
Thanks Mike M! I have changed my code to the following:
ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
HashMap<String, String> listItemData;
for (int i=0; i<results.size(); i++) {
if (results.size() != 0) {
listItemData = new HashMap<String, String>();
String resultStr = results.get(i).toString();
String[] resultStrVar = resultStr.split(",");
listItemData.put("disease_name", resultStrVar[0]);
listItemData.put("confidence", resultStrVar[1]);
listItems.add(listItemData);
} else {
listItemData = new HashMap<String, String>();
listItemData.put("disease_name", "No predictions found");
listItemData.put("confidence", "Kindly shot again");
listItems.add(listItemData);
}
}
SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,
android.R.layout.simple_list_item_2,
new String[] {"disease_name", "confidence"},
new int[] {android.R.id.text1, android.R.id.text2 });
listView.setAdapter(adapter);
Related
I wrote code:
List<String> Names = new ArrayList<String>();
List<Double> Prices = new ArrayList<Double>();
List<String> Dates = new ArrayList<String>();
List<String> Hours = new ArrayList<String>();
List<String> iDs = new ArrayList<String>();
SimpleAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.listMyReceipts);
SearchView m = (SearchView) findViewById(R.id.action_search);
HashMap<String, String> NamePrice = new HashMap<>();
TextView test = (TextView) findViewById(R.id.test);
Names.add("Starbucks"); Prices.add(11.99); Dates.add("01/01/2017"); Hours.add("9:33");
Names.add("Apple Store"); Prices.add(500.00); Dates.add("01/01/2017"); Hours.add("9:30");
Names.add("Esselunga"); Prices.add(135.67); Dates.add("01/01/2017"); Hours.add("11:51");
Names.add("Mediaworld"); Prices.add(19.98); Dates.add("01/01/2017"); Hours.add("12:03");
Names.add("Starbucks"); Prices.add(11.99); Dates.add("01/01/2017"); Hours.add("12:47");
for (int i = 0; i < Names.size(); i++) {
iDs.add(";+#:" + i + ":#+;");
NamePrice.put(iDs.get(i) + Names.get(i), " " + Prices.get(i).toString() + "€" + " - " + Dates.get(i) + " " + Hours.get(i));
}
List<HashMap<String, String>> listItems = new ArrayList<>();
adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[] {"First", "Second"},
new int[] {R.id.listitemTitle, R.id.listitemSubItem});
//Integer x = 0;
//int x = -1;
Iterator it = NamePrice.entrySet().iterator();
for (int i = 0; i < iDs.size(); i++) {
HashMap<String, String> resultMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultMap.put("First", pair.getKey().toString().replace(iDs.get(i), ""));
resultMap.put("Second", pair.getValue().toString());
listItems.add(resultMap);
}
//test.setText(IDs.get(x - 1));
resultsListView.setAdapter(adapter);
}
This should replace all the IDs into "". It doesn't. So the output could be: ;+#:0:#+;NAME1 12.99 ;+#:1:#+;NAME2 0.99
But in the second loop, if I say
resultMap.put("First", pair.getKey().toString().replace(IDs.get(0), ""));
it replaces correctly the element 0.
The output would be: NAME1 12.99 ;+#:1:#+;NAME2 0.99
You are not getting the entries of NamePrice in the order you expect. I inserted a System.out.println() in your second for loop and got the output in this order:
;+#:3:#+;Mediaworld
;+#:0:#+;Starbucks
;+#:4:#+;Starbucks
;+#:1:#+;Apple Store
;+#:2:#+;Esselunga
So you are trying to replace ;+#:0:#+; in ;+#:3:#+;Mediaworld and so forth. That doesn’t replace anything. The reason is that NamePrice is a HashMap. A HashMap’s iterator does not return the entries in any specific order, particularly not in the order they were inserted.
You may use a LinkedHashMap instead:
LinkedHashMap<String, String> NamePrice = new LinkedHashMap<>();
Its iterator returns the entries in the order they were created. So now the replacements work.
When we select the spinner the internally we use the id in this spinner
stringArray = new ArrayList<String>();
myList = new ArrayList<HashMap<String,String>>();
for(int i=0; i<createdtrs_array.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", createdtrs_array.getJSONObject(i).getString("id"));
map.put("name", createdtrs_array.getJSONObject(i).getString("name"));
myList.add(map);
stringArray.add(createdtrs_array.getJSONObject(i).getString("id"));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(BusTickets.this,android.R.layout.simple_spinner_item, stringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
destnation.setAdapter(adapter);
String res = getIntent().getStringExtra("Response");
Log.d("Wallet_Management Response", "getIntent" +res);
//Subscriber_Name.setText(res);
System.out.println("subscriber name response : " + res.toString());
String [] strings = new String [] {res };
List<String> stringList = new ArrayList<String>(Arrays.asList(strings));
//System.out.println("LISTTTTTTTTT : " + stringList);
for (String s :stringList){
String[] str = s.split("\\[");
for(String item : str){
//System.out.println("Checking"+item);
String [] str1 = item.split("\\]");
for (String item1 :str1)
System.out.println("Whole String"+item1);
for(int i =0;i<=str1.length;i++){
System.out.println("OUPUT" +str1[0]);
//System.out.println("OUPUT" +str1[1]);
//System.out.println("OUPUT" +str1[2]);
}
}
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, stringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinnerEmp.setAdapter(dataAdapter);
Output is :
08-28 02:05:04.117: D/Wallet_Management Response(1501): getIntent["MaheshKhatmode","Pranav","RameshSippy"]
08-28 02:05:04.117: I/System.out(1501): subscriber name response : ["MaheshKhatmode","Pranav","RameshSippy"]
After Splitting your result as follows:
08-28 02:05:04.137: I/System.out(1501): Whole String"MaheshKhatmode","Pranav","RameshSippy"
This should work:
List<String> myStringList = new ArrayList<String>();
try {
JSONArray array = new JSONArray(res.toString());
for (int i = 0; i < array.length(); i++) {
myStringList.add(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myStringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinnerEmp.setAdapter(dataAdapter);
can u try like this:
Android convert String to String[]
JSONArray temp = new JSONArray(res);
String[] str1= temp.join(",").split(",");
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinnerEmp.setAdapter(dataAdapter);
i have data fetched from my webservice in
ArrayList<HashMap<String,String>>
Now i want to convert each object of the above to
String[]
how do i do this?
any help would be much appreciated!
try
ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
HashMap<String, String> n = new HashMap<String, String>();
n.put("a", "a");
n.put("b", "b");
test.add(n);
HashMap<String, String> m = test.get(0);//it will get the first HashMap Stored in array list
String strArr[] = new String[m.size()];
int i = 0;
for (HashMap<String, String> hash : test) {
for (String current : hash.values()) {
strArr[i] = current;
i++;
}
}
The uses for an Hashmap should be an Index of HashValues for finding the values much faster. I don't know why you have Key and Values as Strings but if you only need the values you can do it like that:
ArrayList<HashMap<String, String>> test = new ArrayList<>();
String sum = "";
for (HashMap<String, String> hash : test) {
for (String current : hash.values()) {
sum = sum + current + "<#>";
}
}
String[] arr = sum.split("<#>");
It's not a nice way but the request isn't it too ;)
ArrayList<HashMap<String, String>> meterList = controller.getMeter();
HashMap<String, String> mtiti = meterList.get(0);//it will get the first HashMap Stored in array list
String[] strMeter = new String[mtiti.size()];
String meter = "";
for (HashMap<String, String> hash : meterList) {
for (String current : hash.values()) {
meter = meter + current + "<#>";
}
}
String[] arr = meter.split("<#>");
i have two arrays (actually one, but i created two for each columns). I want to populate a hashmap with the values for a listview but all elements of the listview is the last element of the arrays:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for (int i=0; i<13; i++)
{
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);
Any ideas?
Thanks
You're adding the same map to every slot of the array. Try this instead:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i=0; i<13; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}