how do i get the value from the android listview? - android

Hi this is my code i want to access the KEY_ID that i put in maplist on list item click in a variable..
Please Help me How i can do this.
ArrayList (HashMap) (String, String) mapList = new ArrayList(HashMap(String, String)) ();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml);
// getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_Route);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_SchoolName, parser.getValue(e, KEY_SchoolName));
map.put(KEY_ChildrenName, parser.getValue(e, KEY_ChildrenName));
map.put(KEY_AlertTime, parser.getValue(e, KEY_AlertTime));
map.put(KEY_RouteName, parser.getValue(e, KEY_RouteName));
map.put(KEY_Notification, parser.getValue(e, KEY_Notification));
map.put(KEY_StopName, parser.getValue(e, KEY_StopName));
// adding HashList to ArrayList
mapList.add(map);
}
list = (ListView) findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(this, mapList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int idw = position;
long logss = id;
// There i want to access the KEY_ID..
} catch (Exception e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
});
}

In your onItemClick method, you need to fetch HaspMap out from mapList object from a specific position:
HashMap<String, String> map = mapList.get(position);
//and then read values out from 'map' object

USe this code to get the position of the selected listitem.
ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
}
});

Related

how use an array in string content android?

i have a xml file and get data from this file and save it in arraylist
xml file :
<head>33</head>
<link>http://hamrahtest.ir/testcenterv13.apk</link>
</group>
<group>
<head>355</head>
<link>http://hamrahtest.ir/testcenterv5555.apk</link>
</group>
protected void onPostExecute(Document doc) {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
NodeList nl = doc.getElementsByTagName(KEY_GROUPS);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_HEAD, parser.getValue(e, KEY_HEAD));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
// adding HashList to ArrayList
songsList.add(map);
}
Log.d("tagname",songsList.get(1).get(KEY_HEAD).toString());
}
Under this code i have a line :
private static final String[] CONTENT = new String[] {" page1","page2"};
how i can insert KEY_HEAD valuse to CONTENT Instead {" page1","page2"} ?
You can get Set of HashMap keys using Set<String> and the convert Set<String> to String[] using toArray(T[] a)
Set<String> keys = map.keySet();
String[] CONTENT = new String[keys .size()];
keys.toArray(CONTENT);

How to retrieve arraylist from hashmap with particular value from onlistview click in android?

I am adding an array to hashmap with particular value.
I am unable to retrieve the values from arraylist with particular value from on listview click item.
HashMap<String, ArrayList<SeatList>> example = new HashMap<String, ArrayList<SeatList>>();
ArrayList<SeatList> seatlistarray;
ArrayList<Item> items = new ArrayList<Item>();
Item and SeatList are bean classes.
///////////////////////////////
for (int i = 0; i < nodeList.getLength(); i++)
{
Element e = (Element) nodeList.item(i);
Node seat=(Node)seatlist.item(k);
if(seat.hasChildNodes()){
NodeList seats=seat.getChildNodes();
for(int m=0;m<seats.getLength();m++){
Node seatf=(Node)seats.item(m);
if (seatf.getNodeType() == Node.ELEMENT_NODE) {
HashMap hash=new HashMap();
Element eElement = (Element) seatf;
seatlistarray=new ArrayList<SeatList>();
hash.put("ID", eElement.getElementsByTagName("ID").item(0).getTextContent());
hash.put("X_pos", eElement.getElementsByTagName("X_pos").item(0).getTextContent());
hash.put("Y_pos", eElement.getElementsByTagName("Y_pos").item(0).getTextContent());
hash.put("Radius", eElement.getElementsByTagName("Radius").item(0).getTextContent());
seatlistarray.add(new SeatList(eElement.getElementsByTagName("ID").item(0).getTextContent()));
}}}}
example.put(parser.getValue(e, "Hall-ID"), seatlistarray);
items.add(new EntryItem(parser.getValue(e, NODE_NAME), parser.getValue(e, NODE_DESIGNATION),parser.getValue(e, "Hall-ID")));
}
Here listview on click listener function:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(!items.get(position).isSection()){
EntryItem item = (EntryItem)items.get(position);
Toast.makeText(this, "You clicked " + item.VenueName , Toast.LENGTH_SHORT).show();
ListIterator<SeatList> testExtracted = example.get(item.HallID).listIterator();
if(testExtracted.hasNext()){
System.out.println("MYUID:"+testExtracted.next().getID());
}
super.onListItemClick(l, v, position, id);
}
}
// try this
for (int i = 0; i < nodeList.getLength(); i++)
{
Element e = (Element) nodeList.item(i);
Node seat=(Node)seatlist.item(k);
if(seat.hasChildNodes()){
NodeList seats=seat.getChildNodes();
seatlistarray=new ArrayList<SeatList>();
for(int m=0;m<seats.getLength();m++){
Node seatf=(Node)seats.item(m);
if (seatf.getNodeType() == Node.ELEMENT_NODE) {
HashMap hash=new HashMap();
Element eElement = (Element) seatf;
hash.put("ID", eElement.getElementsByTagName("ID").item(0).getTextContent());
hash.put("X_pos", eElement.getElementsByTagName("X_pos").item(0).getTextContent());
hash.put("Y_pos", eElement.getElementsByTagName("Y_pos").item(0).getTextContent());
hash.put("Radius", eElement.getElementsByTagName("Radius").item(0).getTextContent());
seatlistarray.add(new SeatList(eElement.getElementsByTagName("ID").item(0).getTextContent()));
}
}
example.put(parser.getValue(e, "Hall-ID"), seatlistarray);
items.add(new EntryItem(parser.getValue(e, NODE_NAME), parser.getValue(e, NODE_DESIGNATION),parser.getValue(e, "Hall-ID")));
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(!items.get(position).isSection()){
EntryItem item = (EntryItem)items.get(position);
Toast.makeText(this, "You clicked " + item.VenueName , Toast.LENGTH_SHORT).show();
ArrayList<SeatList> list = example.get(item.HallID);
}
}

showing xml data into gridview in android

I am trying to display data from xml file in to grid view in android, but this page is showing one error,can any one please make me clear.....
GridviewSample.java
public class GridviewSample extends Activity
{
// All static variables
static final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders";
// XML node keys
static final String KEY_TABLE = "Table"; // parent node
static final String KEY_CUST = "Cust_Name";
static final String KEY_ORDER = "Order_No";
static final String KEY_FREIGHT = "Freight_Rate";
static final String KEY_STATION1 = "Station_Name";
static final String KEY_STATION2 = "Station_Name1";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv = (GridView)findViewById(R.id.gridView1);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TABLE);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_CUST, parser.getValue(e, KEY_CUST));
map.put(KEY_ORDER, parser.getValue(e, KEY_ORDER));
map.put(KEY_FREIGHT, parser.getValue(e, KEY_FREIGHT));
map.put(KEY_STATION1, parser.getValue(e, KEY_STATION1));
map.put(KEY_STATION2, parser.getValue(e, KEY_STATION2));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
SimpleAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.grid_item,
new String[] { KEY_CUST, KEY_ORDER, KEY_FREIGHT,KEY_STATION1,KEY_STATION2 }, new int[]
{
R.id.cust, R.id.order, R.id.freight,R.id.statio1,R.id.station2 });
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> Table, View v,int position, long id)
{
// getting values from selected GridItem
String cust = ((TextView) v.findViewById(R.id.cust)).getText().toString();
String order = ((TextView) v.findViewById(R.id.order)).getText().toString();
String freight = ((TextView) v.findViewById(R.id.freight)).getText().toString();
String station1 = ((TextView) v.findViewById(R.id.statio1)).getText().toString();
String station2 = ((TextView) v.findViewById(R.id.station2)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), Single_gridview_item.class);
in.putExtra(KEY_CUST, cust);
in.putExtra(KEY_ORDER, order);
in.putExtra(KEY_FREIGHT, freight);
in.putExtra(KEY_STATION1, station1);
in.putExtra(KEY_STATION2, station2);
startActivity(in);
}
});
}}
Thanks for you time!..
You should use an AsyncTask to perform the connection because otherwise the main thread can get stuck and Android will exit your application.

cannot in parsing nested xml structure

this is my xml structure :
<menu>
<item_category>
<category_name>ICECREAM</category_name>
<item>
<item_name>SOFTY</item_name>
<item_price>7.00</item_price>
</item>
<item>
<item_name>STICK</item_name>
<item_price>15.00</item_price>
</item>
<item>
<item_name>CONE</item_name>
<item_price>25.00</item_price>
</item>
</item_category>
<item_category>
<category_name>PIZZA</category_name>
<item>
<item_name>PIZZA-HOT</item_name>
<item_price>35.00</item_price>
</item>
<item>
<item_name>PIZZA-CRISPY</item_name>
<item_price>29.00</item_price>
</item>
</item_category>
</menu>
this is my code where i am parsing into list view
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
//static final String URL = "http://api.androidhive.info/pizza/?format=xml";
static final String URL = "http://192.168.1.112/dine/index.php/dineout/mob_view";
//static final String URL = "http://192.168.1.112/dineout/index.php/dineout/view";
// XML node keys
static final String KEY_MENU= "menu"; // parent node
//static final String KEY_ID = "foodjoint_id";
static final String KEY_CATEGORY= "category_name";
static final String KEY_ITEM= "item";
static final String KEY_ITEM_NAME= "item_name";
static final String KEY_PRICE= "item_price";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_CATEGORY, parser.getValue(e,KEY_CATEGORY));
map.put(KEY_ITEM_NAME, parser.getValue(e, KEY_ITEM_NAME));
map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] {KEY_CATEGORY,KEY_ITEM_NAME,KEY_PRICE}, new int[] {R.id.category,R.id.name,R.id.costlab });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String category = ((TextView) view.findViewById(R.id.category)).getText().toString();
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
//String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
//in.putExtra(KEY_ITEM, name);
in.putExtra(KEY_CATEGORY, category);
in.putExtra(KEY_ITEM_NAME,name);
in.putExtra(KEY_PRICE, cost);
//in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
}
My problem is i am not getting the value of category_name. I tried with nested loop but its all the same. please help me . what should i change in the code.
This is my xmlParser
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
try this way
NodeList n,ncategories;
Element e;
static final String KEY_ITEMCATEGORY= "item_category";
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
ncategories = doc.getElementsByTagName(KEY_MENU);
for(int i=0;i<ncategories.getLength();i++){
e=(Element)ncategories.item(i);
n=e.getElementsByTagName(KEY_ITEMCATEGORY);
for(int c=0;c<n.getLength();c++){
HashMap<String, String> map = new HashMap<String, String>();
e=(Element)n.item(c);
String category= parser.getValue(e, KEY_CATEGORY).toString();
Log.e("catname", parser.getValue(e, KEY_CATEGORY).toString());
map.put(KEY_CATEGORY, category);
NodeList ns=e.getElementsByTagName(KEY_ITEM);
for(int j=0;j<ns.getLength();j++){
e=(Element)ns.item(j);
Log.e("itemname", parser.getValue(e,KEY_ITEM_NAME).toString());
Log.e("itemprice", parser.getValue(e, KEY_PRICE).toString()+"\n");
map.put(KEY_ITEM_NAME, parser.getValue(e, KEY_ITEM_NAME));
map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
}
menuItems.add(map);
}
Try something like this:
NodeList nl_Cat = doc.getElementsByTagName(KEY_ITEM_CATEGORY);
for (int i = 0; i < nl_Cat.getLength(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element cat = (Element) nl_Cat(i);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int j = 0; j < nl.getLength(); j++)
{
Element e = (Element) nl.item(j);
map.put(KEY_ITEM_NAME, parser.getValue(e, KEY_ITEM_NAME));
map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
}
menuItems.add(map);
}
Hope this works!
You are retrieving list of item elements and trying to find category_name in item element. However, looking at your xml, category_name is sibling of item element, not child. So you will not get category name in that way.
You might want to retrieve list of item_category and then populate items from it.
NodeList nl_categories = doc.getElementsByTagName(KEY_ITEM_CATEGORY);
for(int i = 0; i < nl_categories; i++)
{
Element e = (Element) nl.item(i);
String category = parser.getValue(e,KEY_CATEGORY);
NodeList nl = e.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int j = 0; j < nl.getLength(); j++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(j);
map.put(KEY_CATEGORY, category);
map.put(KEY_ITEM_NAME, parser.getValue(e, KEY_ITEM_NAME));
map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
menuItems.add(map);
}
}

XML parser in android

I am using XML parser.In that i have to pass one id from one activity to other ,but this id is not shown in textview of the 1st screen.How can i pass this id to other screen.
Here is my code:
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_LASTVALUE,parser.getValue(e, KEY_LASTVALUE));
map.put(KEY_CHANGE, parser.getValue(e, KEY_CHANGE));
map.put(KEY_STKEXCHANGE, parser.getValue(e, KEY_STKEXCHANGE));
map.put(KEY_LASTPRICE, parser.getValue(e, KEY_LASTPRICE));
// adding HashList to ArrayList
menuItems.add(map);
}
Here i have to pass the id on clicking the particular element value,how can i do it:
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String name = ((TextView) view.findViewById(R.id.name_label)).getText().toString();
// String cost = ((TextView) view.findViewById(R.id.cost_label)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.description_label)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), IndicesDetails.class);
// in.putExtra(KEY_STKEXCHANGE, name);
// in.putExtra(KEY_COST, cost);
// in.putExtra(KEY_DESC, description);
// string ids = getItemId(position);
// in.putExtra("idvalue", id);
startActivity(in);
}
});
In Current Activity :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Starting new intent
Intent in = new Intent(getApplicationContext(), IndicesDetails.class);
in.putExtra("idvalue", id);
startActivity(in);
}
Then in the new activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
long value = extras.getLong("idvalue");
}
try something like it may help.
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap map=yourArrayList.get(position);
String id=map.get("Your key");
in.putExtra("idvalue", id);
}

Categories

Resources