I have this code that will eventually be populated from a database but to get it working first I have used the below code
ListView mListView = (ListView) getActivity().findViewById(R.id.listView);
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String,Object>>( );
HashMap<String, Object> listItem;
listItem = new HashMap<String, Object>();
for (int i = 0;i<=10;i++) {
listItem.put("item", "orderTitles" + i);
listItem.put("subitem", "orderDescriptions" + i);
items.add(listItem);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), items, R.layout.list_item_format, new String[]{"item", "subitem"}, new int[]{R.id.itemTitle, R.id.itemDescription});
mListView.setAdapter(adapter);
The problem is that the output to the list is saying only OrderTitles10 and OrderDescriptions10 (listed 10 times) instead of counting incrementally. What am I doing wrong
change your code to this:
ListView mListView = (ListView) getActivity().findViewById(R.id.listView);
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String,Object>>( );
HashMap<String, Object> listItem;
for (int i = 0;i<=10;i++) {
listItem = new HashMap<String, Object>();
listItem.put("item", "orderTitles" + i);
listItem.put("subitem", "orderDescriptions" + i);
items.add(listItem);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), items, R.layout.list_item_format, new String[]{"item", "subitem"}, new int[]{R.id.itemTitle, R.id.itemDescription});
mListView.setAdapter(adapter);
Initialize listItem inside for loop to create and add new HashMap with both values in ArrayList :
for (int i = 0;i<=10;i++) {
listItem = new HashMap<String, Object>();
listItem.put("item", "orderTitles" + i);
listItem.put("subitem", "orderDescriptions" + i);
items.add(listItem);
}
initialize listitem inside the loop and change the for loop condition like below
for (int i = 0;i<10;i++) {
listItem = new HashMap<String, Object>();
listItem.put("item", "orderTitles" + i);
listItem.put("subitem", "orderDescriptions" + i);
items.add(listItem);
}
Related
in below code i'm trying to add some data to items, on each adding data into for clause value of items have the same data, but in log cat there are different result.
ArrayList<HashMap<String, String>> items = new ArrayList<>();
HashMap<String, String> item = new HashMap<>();
for (int p = 0; p < allFoodBean.size(); p++) {
if (allFoodBean.get(p).getItemId().equals("food1")) {
item.put("id", allFoodBean.get(p).getId());
item.put("name", allFoodBean.get(p).getName());
items.add(item);
}
}
problem is adding data to items in this line of code: items.add(item);
how can i resolve this problem?
Try this
ArrayList<HashMap<String, String>> items = new ArrayList<>();
HashMap<String, String> item = null;
for (int p = 0; p < allFoodBean.size(); p++) {
if (allFoodBean.get(p).getItemId().equals("food1")) {
item = new HashMap<>();
item.put("id", allFoodBean.get(p).getId());
item.put("name", allFoodBean.get(p).getName());
items.add(item);
}
}
When ever if condition becomes false create new HashMap<>()
Move item initialization inside the for loop.
Having it initialized outside will keep on adding food beans in for-loop to same item Map instance and finally you would have a single Map item in items ArrayList instead of having multiple Map objects inside the ArrayList.
ArrayList<HashMap<String, String>> items = new ArrayList<>();
for (int p = 0; p < allFoodBean.size(); p++) {
if (allFoodBean.get(p).getItemId().equals("food1")) {
HashMap<String, String> item = new HashMap<>();
item.put("id", allFoodBean.get(p).getId());
item.put("name", allFoodBean.get(p).getName());
items.add(item);
}
}
I parse json data and display it in listview with CHOICE_MODE_MULTIPLE. I trying to get the key of a Hashmap in my case but I only manage to get the value.
Json data
57->Steak Doness
58->Size
59->Cooking Method
63->Coldness
Here is the coding
String option_data = jsonObject.getString("option_data");
JSONObject jsonObject1 = new JSONObject(option_data);
Iterator<String> iterator = jsonObject1.keys();
ArrayList arrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
while (iterator.hasNext()) {
String id = iterator.next();
String name = jsonObject1.getString(id);
map = new HashMap<String, String>();
map.put("id", id);
map.put("name", name);
arrayList.add(name);
}
ArrayAdapter adapter = new ArrayAdapter<String>(item_add.this, android.R.layout.simple_list_item_multiple_choice, arrayList);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String selected = "";
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
if (sparseBooleanArray.get(i)) {
selected += listView.getItemAtPosition(i).toString() + ",";
}
}
Log.d("selected", selected);
}
});
With the logcat, it shows
D/selected: Steak Doness,Size,
However, what I want to get is the Key in Hashmap which means "id" when click the button . Anyone knows how to do it?
Use the following way to iterate and find values of keys
HashMap <String,String> mapOne= new HashMap();
mapOne.put("1","1value");
mapOne.put("2","2value");
mapOne.put("3","3value");
for(Map.Entry<String, String> mapEntry : mapOne.entrySet()){
if (mapEntry.getValue().equals("3value")) {
//System.out.println(mapEntry.getKey());
Log.d("selected", mapEntry.getKey());
}
}
I have the listview from the exel:
try {
Workbook wb = WorkbookFactory.create(getAssets().open("bos.xls"));
Sheet sheet = wb.getSheetAt(0);
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
IrrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
}
} catch(Exception ex) {
return;
}
When I use it I have the one value in my listview.
How can i correct it?
I tried to add this before "HashMap hm;":
for(int i1 = 0, l = IrrList.size(); i1 < l; i1++){
"I dont know what write here"
}
Try this..
Initilize the ArrayList before the for loop and set the listview adapter after the for loop
IrrList = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
}
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
I have a Simple Question.Why is this List showing the last item in the List
Any Answer Appreciated...
List<HashMap<String, Object>> noteItem=new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> hashmapNoteItem = new HashMap<String, Object>();
hashmapNoteItem.put("todo_check", false);
hashmapNoteItem.put("todo_content", "added 0");
noteItem.add(hashmapNoteItem);
HashMap<String,Object> hashmapNoteItem1 = new HashMap<String, Object>();
hashmapNoteItem.put("todo_check", true);
hashmapNoteItem.put("todo_content", "added 1");
noteItem.add(hashmapNoteItem1);
At second code block you are using hashmapNoteItem instead of hashmapNoteItem1:
List<HashMap<String, Object>> noteItem=new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> hashmapNoteItem = new HashMap<String, Object>();
hashmapNoteItem.put("todo_check", false);
hashmapNoteItem.put("todo_content", "added 0");
noteItem.add(hashmapNoteItem);
HashMap<String,Object> hashmapNoteItem1 = new HashMap<String, Object>();
// changed hashmapNoteItem to hashmapNoteItem1
hashmapNoteItem1.put("todo_check", true);
hashmapNoteItem1.put("todo_content", "added 1");
noteItem.add(hashmapNoteItem1);
If your question is: "Why is this List NOT showing the last item in the List"
After noteItem.add(hashmapNoteItem1);
add this line:
yourAdapter.notifyDataSetChanged();
Currently, I have json data of several twitter feeds that I parse to fill my list. I have an object, "image" that contains the url of the users icon that I want to set as the ImageView to in the list. I am having trouble trying to figure out how I can take the url and load the image in the Imageview for each row. Here is the code:
public void getTweets(String selection) {
String formatedcat = selection.toLowerCase();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions
.getJSONfromURL("http://example.com/tweet.php");
try {
JSONArray category = json.getJSONArray(formatedcat);
for (int i = 0; i < category.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = category.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name",
c.getString("fullName") + "\n(#" + c.getString("name")
+ ") ");
map.put("text",
c.getString("text") + "\n - "
+ c.getString("timestamp"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.list,
new String[] { "name", "text"}, new int[] { R.id.item_title,
R.id.item_subtitle});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
How to display image from URL on Android
See this question. Look very practical to me. Do that showing of the image in the getView() method where you populate the list and instantiate the child views.