i have my listview activity that works pretty fine but i have an annoying problem.
Whenever it resumes it adds the same items in the the list and that ist gets bigger. I just want it to keep the values i feeded at first.
How can i do that?
here is my activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"pen","price","color"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
And here is my arrayList:
static final ArrayList<LinkedHashMap<String,String>> list =
new ArrayList<LinkedHashMap<String,String>>();
private void populateList() {
LinkedHashMap<String,String> temp = new LinkedHashMap<String,String>();
temp.put("pen"," Mission name");
list.add(temp);
LinkedHashMap<String,String> temp1 = new LinkedHashMap<String,String>();
temp1.put("pen"," Map activity");
list.add(temp1);
LinkedHashMap<String,String> temp2 = new LinkedHashMap<String,String>();
temp2.put("pen"," Check sensors");
list.add(temp2);
LinkedHashMap<String,String> temp3 = new LinkedHashMap<String,String>();
temp3.put("pen"," Infrared Image");
list.add(temp3);
LinkedHashMap<String,String> temp4 = new LinkedHashMap<String,String>();
temp4.put("pen"," Radar Image");
list.add(temp4);
LinkedHashMap<String,String> temp5 = new LinkedHashMap<String,String>();
temp5.put("pen"," Visual image");
list.add(temp5);
LinkedHashMap<String,String> temp6 = new LinkedHashMap<String,String>();
temp6.put("pen"," suscribe to Viewer");
list.add(temp6);
}
Don't declare the list as static.
i.e.:
final ArrayList<LinkedHashMap<String,String>> list = new ArrayList<LinkedHashMap<String,String>>();
Related
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);
}
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);
At the following code i am loading an ArrayList with data one by one.
This ArrayList is used in an ExpandableListView. So it contains groups and childs.
Every group has a Name and a Description.
Every child has a Name.
Some groups do not have childern.
public class CategoriesExpandableList extends Activity implements OnChildClickListener,
OnGroupClickListener, OnGroupExpandListener, OnGroupCollapseListener {
private static Context mContext;
private ExpandListAdapter ExpAdapter=null;
private ArrayList<ExpandListGroup> ExpListItems;
ArrayList<ExpandListGroup> list=null;
private ExpandableListView ExpandList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.categoriesexpandable);
ExpandList = (ExpandableListView) findViewById(R.id.ExpList);
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapter(CategoriesExpandableList.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
ExpandList.setOnGroupClickListener(this);
ExpandList.setOnChildClickListener(this);
mContext=this;
}
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru0 = new ExpandListGroup();
gru0.setName("My Favorites");
gru0.setDescription("My favourite points of interest");
gru0.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru1 = new ExpandListGroup();
gru1.setName("Essentials");
gru1.setDescription("Very essential points of interest");
gru1.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru2 = new ExpandListGroup();
gru2.setName("Sights");
gru2.setDescription("Beautiful sights you have to visit");
gru2.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru3 = new ExpandListGroup();
gru3.setName("Beaches");
gru3.setDescription("Beaches near Agios Nikolaos");
ExpandListChild ch3_0 = new ExpandListChild();
ch3_0.setName("In town");
ch3_0.setTag(null);
list2.add(ch3_0);
ExpandListChild ch3_1 = new ExpandListChild();
ch3_1.setName("Outside town");
ch3_1.setTag(null);
list2.add(ch3_1);
gru3.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
list.add(gru0);
list.add(gru1);
list.add(gru2);
list.add(gru3);
return list;
}
}
I want to know if there is a quick way to load these data at the ArrayList. For example by calling a method like this:
addPoint(String groupname, String groupdescription, String child01)
addPoint("Banks", "Banks and ATMs", "Only banks")
addPoint("Banks", "Banks and ATMs", "Only ATMs")
// Here i have only a group and no children...
addPoint("Sights", "Beautiful sights to visit", "")
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
String[][] expandablelist={ {"My Favorites", "My favourite points of interest","","","","","","","","","",""},
{"Essentials", "Very essential points of interest","","","","","","","","","",""},
{"Sights", "Beautiful sights you have to visit","","","","","","","","","",""},
{"Beaches", "Nice beaches","In town","Around town","","","","","","","",""},
{"Banks", "Banks and ATMs","","","","","","","","","",""},
{"Pharmacies", "All pharmacies near you","","","","","","","","","",""},
{"Doctors", "Doctors in case of emergency","Pediatricians","Pathologists","General doctors","Eye doctors","Surgeons","Otorhinolayngologists","Orthopaedists","Gynecologists","Dermatologists","Cardiologists"}
};
for(int i=0; i<=6; i++) {
System.out.println("i= "+i);
ExpandListGroup gru = new ExpandListGroup();
gru.setName(expandablelist[i][0]);
gru.setDescription(expandablelist[i][1]);
for(int j=2; j<=11; j++) {
System.out.println("j= "+j);
System.out.println("length= "+expandablelist[i][j].length());
if(expandablelist[i][j].length()!=0) {
ExpandListChild chld = new ExpandListChild();
chld.setName(expandablelist[i][j]);
chld.setTag(null);
list2.add(chld);
}
}
if(list2!=null) {
gru.setItems(list2);
}
list2 = new ArrayList<ExpandListChild>();
list.add(gru);
}
}
I have founnd a way to load my ArrayList from an Array like you see at the code above.
Thank you for reading about my problem.
I am sure there is a better solution and if i have some time i will try to figure it out.
package com.coltonnicotera.londontransitguide;
//bunch of imports
public class SchedulesActivity extends Activity {
/** Called when the activity is first created. */
List<Map<String, String>> routesList = new ArrayList<Map<String,String>>();
String[] routesArray = new String[] {"01. Kipps Lane/Thompson Road", "02. Dundas", "03. Hamilton Rd", "04. Oxford East", "05. Springbank", "06. Richmond", "07. Wavell", "08. Riverside", "09. Whitehills", "10. Wonderland", "11. Southcrest", "12. Warncliffe South", "13. Wellington Road", "14. Highbury", "15. Westmount", "16. Adelaide", "17. Oxford West", "19. Oakridge", "20. Cherryhill", "21. Huron Heights", "22. Trafalgar", "23. Berkshire", "24. Baseline", "25. Kilally", "26. Jalna Blvd. West", "27. Fanshawe College", "28. Lambeth", "30. Newbold", "31. Orchard Park", "32. Windermere", "33. Proudfoot", "34. Medway", "35. Argyle", "36. Airport/Industrial", "37. Sovereign Road", "38. Stoney Creek", "39. Fanshawe West"};
public void onCreate(Bundle savedInstanceState) {
initList();
ListView lv = (ListView) findViewById(R.id.listView);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, routesList, android.R.layout.simple_list_item_1, new String[] {"route"}, new int[] {android.R.id.text1});
/*LINE 63 */ lv.setAdapter(simpleAdpt);
}
private void initList() {
for (int i = 0; i<=36; i++)
routesList.add(createRoute("route", routesArray[i]));
}
private HashMap<String, String> createRoute(String key, String name) {
HashMap<String, String> route = new HashMap<String, String>();
route.put(key, name);
return route;
}
}
My problem is that on line 63 there is a NullPointerException. I'm not sure what exactly is null here. I've removed all the imports for simplicity. R.id.listView also exists.
You have not set your layout that why listview findViewbyId returns null...so what you need to do is In oncreate() ,set contentView before actually getting your listview ...like this setContentView(R.layout.yourlayout)
I have a little problem sending the right id to another activity as extra.I'm getting id from database which I'm sending to the new activity so I can get the right data. But the problem is that my code is sending only the last id,which is on arraylist. How can I fix that?
Here is the code I'm using :
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
if (vf != null) {
vf.removeAllViews();
}
text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
cardsCount = cursorTitle.getString(cursorTitle.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursorTitle.getString(cursorTitle.getColumnIndex("objectId")));
Log.i("CollID","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
array = new ArrayList<Integer>();
array.add(collId);
vf = new ViewFlipper(MyCollectionList.this);
myListView = new ListView(MyCollectionList.this);
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
final SimpleAdapter adapter = new SimpleAdapter(MyCollectionList.this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", array.get(position));
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
});
}
Thanks in advance!
You're creating a new ArrayList in each iteration and then assigning only a single value to it. I don't think that is what you intended. You should instantiate the ArrayList outside of your loop. You'll also want to move all of your other one-time initialization code (ListView, SimpleAdapter, etc.) out of the loop.
What i got is,you need to populate a listview with the records you get in cursor.You are populating your listview in each iteration of for loop and that is what causes problem.i think,following should work for you. (Required changes for varibles should be made accordingly.)
Try this:
vf = new ViewFlipper(MyCollectionList.this);
myListView = new ListView(MyCollectionList.this);
array = new ArrayList<Integer>();
hm = new HashMap<String, Object>();
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
if (vf != null) {
vf.removeAllViews();
}
text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
cardsCount = cursorTitle.getString(cursorTitle.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursorTitle.getString(cursorTitle.getColumnIndex("objectId")));
Log.i("CollID","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
array.add(collId);
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
}
final SimpleAdapter adapter = new SimpleAdapter(MyCollectionList.this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", array.get(position));
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
Put array = new ArrayList<Integer>(); outside of for loop.
like,
array = new ArrayList<Integer>();
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
{
// your stuff
array.add(collId);
// your stuff
}