List showing the last item in the ArrayList - android

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();

Related

Android adding to ArrayList replace older items and i have the same data

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);
}
}

Android listview not working as expected

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);
}

ListView with custom backgroundResource

i need to set a listview with custom text and custom setbackgroundresource when i create the listview
This is my code:
The listView receive the data from URL by JSON encdoe like that:
{"results":[
{"db_id":"6","discount":"active","db_description":"bla bla bla ","db_num":"137","db_num2":"260"},
{"db_id":"14","db_type":"discount","db_description":"blaaaaaaa","db_num":"39","db_num2":"46"},
{"db_id":"18","db_type":"discount","db_description":"blaaaaaaa","db_num":"335","db_num2":"456"},
]}
Adding the data to map
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("type",RESULTS_PARAMS));
JSONObject json = jsonParser.makeHttpRequest(RESULTS_URL, "GET",
params);
Log.d("JSON RESULT: ", json.toString());
try {
queues = json.getJSONArray(TAG_RESULTS);
for (int i = 0; i < queues.length(); i++) {
JSONObject c = queues.getJSONObject(i);
String id = c.getString(TAG_ID);
String type = c.getString(TAG_TYPE);
String description = c.getString(TAG_DESCRIPTION);
String num = c.getString(TAG_NUM);
String num2 = c.getString(TAG_NUM2);
int imageint = getResources().getIdentifier(c.getString(TAG_TYPE) , "drawable", getPackageName());
String image = String.valueOf(imageint);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TYPE, type);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_NUM, num);
map.put(TAG_NUM2, num2);
map.put(TAG_IMAGE, image);
QueueList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
the code the set the DATA into the listview
ListAdapter adapter = new SimpleAdapter(
QueueActivity.this,
QueueList,
R.layout.queue_row,
new String[] { TAG_ID, TAG_DESCRIPTION, TAG_NUM, TAG_NUM2, TAG_IMAGE},
new int[] { R.id.queueid, R.id.description, R.id.num, R.id.num2, R.id.list_image });
setListAdapter(adapter);
now i want to set a setBackgroundResource to R.drawable.customlistviewback
if the db_id (TAG_ID) = int info
For ex, int info = 6;
To do row-level view modifications you will probably need to use a custom Adapter. Typically you override the getView function and make your changes there. See how to customize listview row android for a decent example.

How to Compare String of HashMap and arrange them?

Supose i have name Mink,Mark,Aashis. How do compare them and arrange them according to alphabetical order. Collections.sort() dont work for me. Here i should have results Aashis,Mark and Mark.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", XMLfunctions.getValue(e, "name"));
mylist.add(map);
Collections.sort( mylist, new Comparator<HashMap<String,String>>(){
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
//how do i compare string name
return 0;
}
});
Collections.sort(list, new Comparator(){
public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
return (o1.get("name")).compareToIgnoreCase(o2.get("name"));
}
Here "name" is the Key that you have provided at the time of insertion in the HashMap using put method.
EXTRA
If you want to parse double values then you can use the Double.parse() method.
return Double.compare(o1.get(value1), o1.get(value2));
NOTE - The nice thing about this approach is that you then sort any object by any attribute or even a combination of attributes. For example if you have objects of type Person with an attribute income and dataOfBirth you could define different implementations of Comparator and sort the objects according to your needs.
Hope this will help you.
try this code
insert values
public ArrayList<HashMap<String, String>> list;
list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 5; i++) {
HashMap<String, String> info = new HashMap<String, String>();
info.put("title", "gdfgdfgdfgdfggdfgfdgdgdgdffghfghfgh");
info.put("time", "44:55");
info.put("view", "54,353");
list.add(info);
}
get and compare
String title1 = ketan;
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map = list.get(i);
if(map.get("title")==title1);
{
........
......
}
}

Get hashmap out of an Array with Hashmap value X

I have an arraylist with multiple hashmaps that contains information that comes from a sql database
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
This arraylist will eventually fill my listview with items (names of people).
When I click on one item I want to send all of the content of that hashmap to another page.
For example:
John <
Now I want to send the hashmap with all the information of John to another android page.
How do I get that information out of the hashmap?
This is my code:
public class Contactenlijst extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactview);
ListView lv;
lv = (ListView) findViewById(R.id.list);
lv.setTextFilterEnabled(true);
String[] from = new String[] {"naam"};
int[] to = new int[]{R.id.naam};
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
// Get the data (see above)
JSONObject json = Database
.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");
try {
JSONArray contactinfo = json.getJSONArray("contactlijst");
// Loop the Array
for (int i = 0; i < contactinfo.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = contactinfo.getJSONObject(i);
map.put("voornaam", e.getString("staff_name"));
map.put("achternaam", e.getString("staff_lastname"));
map.put("geboortedatum", e.getString("staff_dateofbirth"));
map.put("adres", e.getString("staff_address"));
map.put("postcode", e.getString("staff_address_postal"));
map.put("woonplaats", e.getString("staff_address_city"));
map.put("email", e.getString("staff_email"));
map.put("telefoon", e.getString("staff_phone"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
//array for view
ArrayList<HashMap<String, String>> mylist2 = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < mylist.size(); i++){
HashMap<String,String> map2 = new HashMap<String, String>();
map2.put("naam", (mylist.get(i).get("voornaam")+" "+(mylist.get(i).get("achternaam"))));
mylist2.add(map2);
}
try{
lv.setAdapter(new SimpleAdapter(this, mylist2, R.layout.list_item, from, to));
}catch (Exception e){Log.d("test1","test2");}
//onclick stuur array naar contactinfo
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text = ((TextView) view).getText().toString();
Intent i = new Intent(Contactenlijst.this, Contactinfo.class);
String uittekst[] = text.split(" ");
String voornaam = uittekst[0].toString();
String achternaam = uittekst[1].toString();
startActivity(i);
}
});
}
}
So it all has to happen under "String achternaam = uittekst[1].toString();"
for(Hashmap<String, String> map: mylist) {
for(Entry<String, String> mapEntry: map) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
}
}

Categories

Resources