Android Gridview: how to pass an XML to Gridview?
I have a control Gridview and an XML that must populate the GridView.
XML is:
<?xml version='1.0' encoding='iso-8859-1' ?>
<MEDS>
<MED>
<NOM>GELOCATIL</NOM>
<COD>12812931</COD>
</MED>
&开发者_运维百科lt;MED>
<NOM>OTRO GELOCATIL</NOM>
<COD>1281293222</COD>
</MED>
</MEDS>
How do I create a Cursor for this? Or alternatives, pls?
Bye.
gridViewObj.setListAdapter(Adapters.loadAdapter(this, R.xml.contacts));
gridViewObj : GridView Object
xml source : R.xml.contacts
[for More info click here][1]
[1]: https://developer.android.com/resources/samples/XmlAdapters/index.html
I think the right answer is to do an adapter class specific for this xml. But it is more complicated and takes more time. #Sravan has not provided the code so I am obliged to give this answer as right.
We can look for a creative and easy solution although not optimized.
If the case is that you only need 1 or 2 fields of the XML, which can be usual, because you're showing a list that is clickable, so you need only to show to title and to give a substantial information (id for example or use the text of the title itself) so that when you click, takes you to another activity. So the easiest is to use an ArrayAdapter, a TreeMap and an ArrayList
TreeMap<String, String> xmlMapping;
//here you add the values of the xml with Xpath to the variables clave and valor
xmlMapping.put(clave,valor);
ArrayList<String> lista=new ArrayList<String>();
for(Map.Entry<String, String> entry: xmlMapping.entrySet())
{
lista.add(entry.getKey());
}
ArrayAdapter ad=new ArrayAdapter<String>(this.activity.getApplicationContext(),
R.layout.gridrow,R.id.colName,
lista);
grid.setAdapter(ad);
Then to know which element has been clicked the only thing is to iterate through the xmlMapping or take the value of the ArrayList.
精彩评论