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);
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);
}
Below is Simple declaration in this a child and parent is being build with dynamic list value.With relevant code as createGroupList() and createChildList(). I am able to manipulate with TextView but one of field is my dynamic image from getting to JSON not is being load in WebView.
expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), // Creating group List.
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.`enter code here`row_name },
createChildList(), // Creating Child List.
R.layout.child_row,
new String[] { "Sub Item0", "Sub Item1", "Sub Item2", "Sub Item3" },
new int[] { R.id.grpChildName, R.id.grpChildAddress, R.id.grpChildPhoneNo, R.id.childImage }
);
setListAdapter(expListAdapter);
private List createGroupList() {
String header = "";
int headerSize = 0;
ArrayList result = new ArrayList();
for (int i = 0; i < dynamicList.size(); i++) {
HashMap m = new HashMap();
String taxiCompanyData[] = dynamicList.get(i);
if (header != taxiCompanyData[3]) {
m.put("Group Item", taxiCompanyData[3]);
header = taxiCompanyData[3];
headerList[headerSize] = header;
headerSize++;
result.add(m);
counter++;
}
}
return (List) result;
}
Above is Group creating and
private List createChildList() {
ArrayList result = new ArrayList();
Log.i("Info ", "headerList.length" + headerList.length);
for (int i = 0; i < headerList.length; i++) {
final ArrayList secList = new ArrayList();
System.out.println("dynamic list size is in main"
+ dynamicList.size());
webView = (WebView) findViewById(R.id.childImage);
for (int j = 0; j < dynamicList.size(); j++) {
String taxiCompanyData[] = dynamicList.get(j);
if (taxiCompanyData[3].equalsIgnoreCase(headerList[i])) {
final HashMap child = new HashMap();
child.put("Sub Item0", taxiCompanyData[0]);
child.put("Sub Item1", taxiCompanyData[1]);
child.put("Sub Item2", taxiCompanyData[2]);
// child.put("Sub Item3", taxiCompanyData[4]);
Thread th = new Thread(){
#Override
public void run() {
webView.loadUrl("https://lh5.googleusercontent.com/-u8heQyD_4y4/T5VMu-Zc6wI/AAAAAAAi71s/EP32a3D-fc0/s90/Corsino");
secList.add(child);
};
};
th.start();
}
}
result.add(secList);
}
return (List)result;
}
I am unable to load WebUI. Please help anyone.
Two issues here. Following a tutorial and I need a little help adapting it to my project.
1)The children are the same for each group. For example the Arraylist dining contain the children of the group "Dining Commons" I need to make two more arraylists containing academics buildings and residential buildings and those need to be the children of their respective parents.
2) Is it possible to make the children item clickable? using clicklistener or something like that.
java source.
package com.bogotobogo.android.smplexpandable;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Demonstrates expandable lists backed by a Simple Map-based adapter
*/
public class SmplExpandable extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
ArrayList buildings = BuildingList();
ArrayList diningCommonBuildings = DiningList();
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < buildings.size(); i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, (String) buildings.get(i));
//curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> dining = new ArrayList<Map<String, String>>();
for (int j = 0; j < diningCommonBuildings.size(); j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
dining.add(curChildMap);
curChildMap.put(NAME, (String) diningCommonBuildings.get(j));
//curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(dining);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
private ArrayList BuildingList() {
ArrayList buildings = new ArrayList();
buildings.add("Academic Buildings");
buildings.add("Dining Commons");
buildings.add("Residential Buildings");
return buildings;
}
private ArrayList DiningList() {
ArrayList dining = new ArrayList();
dining.add("Berkshire");
dining.add("Franklin");
dining.add("Hampden");
dining.add("Hampshire");
dining.add("Worcester");
return dining;
}
}
Check out this example,
public class MainExpand extends ExpandableListActivity {
private ExpandableListAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> parent = new ArrayList<Map<String,String>>();
List<List<Map<String, String>>> childGroup = new ArrayList<List<Map<String,String>>>();
for (int i = 0; i < 5; i++) {
Map<String, String> parentMap = new HashMap<String, String>();
parentMap.put("lalit", "Parent "+i);
parent.add(parentMap);
List<Map<String, String>> child = new ArrayList<Map<String,String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> childMap = new HashMap<String, String>();
childMap.put("lalit", "Child "+j);
child.add(childMap);
}
childGroup.add(child);
}
adapter = new SimpleExpandableListAdapter(
this,
parent,android.R.layout.simple_expandable_list_item_1,new String[] {"lalit"},new int[] { android.R.id.text1, android.R.id.text2},
childGroup,android.R.layout.simple_expandable_list_item_2,new String[]{"lalit","even"}, new int[]{android.R.id.text1,android.R.id.text2}
);
setListAdapter(adapter);
}
I used a combination of both of your answers. Thanks. I took advatange of the multidimensional array.
First i created these arrays.
private String[] buildingTypes = {
"Academic Buildings", "Dining Commons", "Residential Halls" };
private String[][] buildingList = {
{ "Agriculutural Engineering", "Army ROTC", "Arnold House", "Studio Arts Bldg","Bartlett" },
{ "Berkshire", "Franklin", "Hampden", "Hampshire", "Worcester" },
{ "Baker Hall", "Brett Hall", "Brooks Hall", "Brown Hall","Butterfield Hall" },
Then I modified the for loops
for (int i = 0; i < buildingTypes.length; i++) {
Map<String, String> parentMap = new HashMap<String, String>();
parentMap.put("lalit", buildingTypes[i]);
parent.add(parentMap);
List<Map<String, String>> child = new ArrayList<Map<String,String>>();
for (int j = 0; j < rows; j++) {
Map<String, String> childMap = new HashMap<String, String>();
childMap.put("lalit", buildingList[i][j]);
child.add(childMap);
}
childGroup.add(child);
}
Here are the answer to both of your questions. If something is not OK for you, just tell me in comments.
Answer of part 1):
Please look here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
private String[][] children = {
{ "Academic1", "Academic2", "Academic3", "Academic4" },
{ "Dining1", "Dining2", "Dining2", "Dining3" },
{ "Residential1", "Residential2" },
};
Answer of part 2):
http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html
onChildClick(ExpandableListView parent, View v, int groupPosition, int
childPosition, long id) Callback method to be invoked when a child in
this expandable list has been clicked.
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();
}
}
i'd like each for row in listview has a spinner. But can not. please help me.
//Setting player
adapter = new SimpleAdapter(this, list, R.layout.custom_listitem_edit,
new String[] { "cat", "duration", "order", "id_pos"}, new int[] {
R.id.spn_edibt_selectcat,
/*R.id.spn_editbt_selectpos,*/
R.id.txt_editbt_dur,
R.id.txt_editbt_order,
R.id.txt_edit_idpos
});
populateList();
setListAdapter(adapter);
static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
/* Set thong tin cho listview */
private void populateList() {
cursor_position_mycurrent = dbHelper_position_mycurrent.fetchAllPositionMyCurrentById(id_bt);
startManagingCursor(cursor_position_mycurrent);
Log.e("SO dong lay duoc la", String.valueOf(cursor_position_mycurrent.getCount()));
if (cursor_position_mycurrent != null) {
cursor_position_mycurrent.moveToFirst();
while (cursor_position_mycurrent.isAfterLast() == false) {
HashMap<String, String> tmp = new HashMap<String, String>();
Log.e("DUR", cursor_position_mycurrent.getString(5));
Log.e("DUR", cursor_position_mycurrent.getString(6));
tmp.put("duration", cursor_position_mycurrent.getString(5));
tmp.put("order", cursor_position_mycurrent.getString(6));
tmp.put("id_pos", cursor_position_mycurrent.getString(0));
//Setting spinner category
cursor_position_allposition =dbHelper_position_allposition.fetchAllPositions();
array_spinner_category = new String[cursor_position_allposition.getCount()];
if (cursor_position_allposition != null) {
int i = 0;
cursor_position_allposition.moveToFirst();
while (cursor_position_allposition.isAfterLast() == false) {
array_spinner_category[i] = cursor_position_allposition.getString(0);
cursor_position_allposition.moveToNext();
i++;
}
cursor_position_allposition.close();
}
Log.e("ALL POS", String.valueOf(cursor_position_allposition.getCount()));
Spinner spinner = (Spinner) findViewById(R.id.spn_edibt_selectcat);
int m = 3;//Integer.parseInt(camping.rulesList.getMaxPers().get(camping.tipSel));
String[] array_spinner=new String[m];
for (int indice = 0; indice < m; indice++)
{
if (indice == 0) array_spinner[indice] = String.format("%d persona", indice+1);
else array_spinner[indice] = String.format("%d persone", indice+1);
}
final ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, array_spinner);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
list.add(tmp);
cursor_position_mycurrent.moveToNext();
}
}
}