how use an array in string content android? - 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);

Related

xml parsing and show the data in a list view

I have a xml file like [this][1] and i need to get the all the "edition name" according to the "group name"
I have to create a list of group name and show the list of editions name on clicking of the group name.
OutPut :
onclick of catalogs :
this xml is present in different languages, is there any way to get all the editions name without comparing with group name.
currently i am using type tag to get all the catalogs
:
if(parser.getValue(ele2, "type").toString().equalsIgnoreCase("catalog"))
But some magazines are also present in catalogs type. so not getting correct results
..
How to get all the editions name where group name is catalogs and so on ...
Or a better way to do so.. I am not sure the code I am using is a good approach.
Kindly suggest.
My code :
final String URL = "http://xml/web/?frg="+lang+"&user="+count+"_"+user;
System.out.println("URL child : " + URL);
parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
doc = parser.getDomElement(xml); // getting DOM element
NodeList n1 = doc.getElementsByTagName("groups");
for (int i = 0; i < 1; i++) { //ToDo : worked temp
Element e = (Element) n1.item(0);
NodeList children = e.getChildNodes();
if(item_name.equalsIgnoreCase("Catalogs")||item_name.equalsIgnoreCase("Kataloge")) // there could be other languages
{
NodeList nodeList2 = doc.getElementsByTagName("edition");
for (int k = 0; k < nodeList2.getLength(); k++) {
HashMap<String, String> map = new HashMap<String, String>();
Element ele2 = (Element) nodeList2.item(k);
if(parser.getValue(ele2, "type").toString().equalsIgnoreCase("catalog"))
{
System.out.println(""+parser.getValue(ele2, "name").toString());
System.out.println(""+parser.getValue(ele2, "cat_url").toString());
map.put(ss2, parser.getValue(ele2,ss2 ));
map.put(KEY_THUMB_URL, parser.getValue(ele2, KEY_THUMB_URL));
map.put(CAT_URL, parser.getValue(ele2,"cat_url"));
// adding HashList to ArrayList
songsList.add(map);
}
}
}
if(item_name.equalsIgnoreCase("Magazines")||item_name.equalsIgnoreCase("Magazine"))
{
NodeList nodeList2 = doc.getElementsByTagName("edition");
for (int k = 0; k < nodeList2.getLength(); k++) {
HashMap<String, String> map = new HashMap<String, String>();
Element ele2 = (Element) nodeList2.item(k);
if(parser.getValue(ele2, "type").toString().equalsIgnoreCase("pdf"))
{
System.out.println(""+parser.getValue(ele2, "name").toString());
System.out.println(""+parser.getValue(ele2, "cat_url").toString());
map.put(ss2, parser.getValue(ele2,ss2 ));
map.put(KEY_THUMB_URL, parser.getValue(ele2, KEY_THUMB_URL));
map.put(CAT_URL, parser.getValue(ele2,"cat_url"));
// adding HashList to ArrayList
songsList.add(map);
}
}
}
if(item_name.equalsIgnoreCase("Instructions"))
{
NodeList n2 = doc.getElementsByTagName("groups");
for (int i1 = 0; i1 < 1; i1++) { //ToDo : worked temp
Element e1 = (Element) n2.item(0);
NodeList children1 = e1.getChildNodes();
for (int j = 0; j <children1.getLength(); j++) {
HashMap<String, String> map = new HashMap<String, String>();
child = children1.item(j);
// System.out.println("name node22 inside " + parser.getValue((Element)child, "name"));
if(parser.getValue((Element)child, "name").equals("Instructions"))
{
if (child.getNodeName().equalsIgnoreCase("group")) {
NodeList nl2 = child.getChildNodes();
System.out.println("Nodelist lenght : " + nl2.getLength());
for (int jj = 0; jj <nl2.getLength(); jj++) {
Node nn =nl2.item(jj);
System.out.println("name node22 inside " + parser.getValue((Element)nn, "name"));
}
// map.put(KEY_TITLE, parser.getValue((Element)child,KEY_TITLE ));
//map.put(KEY_THUMB_URL, parser.getValue((Element)child, KEY_THUMB_URL));
// adding HashList to ArrayList
//songsList.add(map);
}
}
}
}
}
Solved :
Changed this :
Element e = (Element) n1.item(0);
NodeList nodeList2 = doc.getElementsByTagName("edition");
to this :
Element e = (Element) n1.item(0);
NodeList nodeList2 = e.getElementsByTagName("edition");
I'd rather suggest you to build your own parser using the pattern matching approach. Coz it gives you the power to customize things according to your need.
Gson like parser will be a good fit.

Sometimes json Array appears with images and sometimes empty array

Sometime json array appears with image url's and sometimes it is empty. I am using the following code where array with image url displaying data and image in listview but when array is empty then data is also not displaying. How can i slove this? Any answer will be appreciated
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
JSONArray posts = json.getJSONArray(KEY_POSTS);
// looping through all song nodes <song>
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
//authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);
JSONArray atta = c.getJSONArray("attachments");
for(int j = 0; j < atta.length(); j++){
JSONObject d = atta.getJSONObject(j);
String slug = d.getString(KEY_SLUG);
JSONObject images = d.getJSONObject(KEY_IMAGES);
JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
String url = thumbnail.getString(KEY_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL, url);
// adding HashList to ArrayList
songsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);

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.

android get nodelist attribute from xml file

I make listview from xml file http://view-source:http://www.macetlagi.com/maps/st/canvaser/3/tb/tb123.I will get element from "segment" tag.When i run and debug my code,i get this error java.lang.NullPointerException.Please correct my code if i do my stupid coding.This is my java code in android :
public class ListSegment extends ListActivity {
String URL_XML = "http://www.macetlagi.com/maps/st/canvaser/3/tb/tb123";
static final String KEY_SEGMENT = "segment";
static final String KEY_SEGMENT_ID = "segment_id";
static final String KEY_MAIN_SEGMENT = "main_segment_name";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listsegment_main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL_XML);
Document doc = parser.getDomElement(xml);
NodeList nL = doc.getElementsByTagName(KEY_SEGMENT);
for (int i = 0; i < nL.getLength(); i++) {
Node node = nL.item(i);
if(node.hasAttributes()) {
NamedNodeMap attr_id = node.getAttributes();
attr_id.getNamedItem(KEY_SEGMENT);
}
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nL.item(i);
map.put(KEY_SEGMENT_ID, parser.getValue(e, KEY_SEGMENT_ID));
map.put(KEY_MAIN_SEGMENT, parser.getValue(e, KEY_MAIN_SEGMENT));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[] {KEY_SEGMENT_ID, KEY_MAIN_SEGMENT}, new int[] {
R.id.segmentid, R.id.segmentname
});
setListAdapter(adapter);
}
}

how do i get the value from the android listview?

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

Categories

Resources