Android Gridview: how to pass an XML to Gridview? - android

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>
<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.

Related

Espresso: How to check if an integer value in a recylerview's child view is greater that 4000

I have a recyclerView in my app. I have 2 fields in it(image_view, price_text_view). The value of price_textview looks like Rs.8000. Now I wanted to keep scrolling until I find an item on the recyclerview whose price is more that Rs.4000(>4000). Can someone please help me with the matcher etc.?
I can't give you whole code here but can give you some logic for implementing this.
this can be done by For loop. And using ArrayList or Array you are passing in your Adapter.
Now lets assume you are passing Arralist to your Adapter say "arrayLst". and from your qustion I assume you are using ArrayList with HashMap.
so your ArraList would be like.
ArrayList<HashMap<String,String>> arrayList = new ArrayList<HashMap<String,String>>();
now do something like this.
for(int i = 0; i<arrayList.size; i++){
if(Integer.valueOf(arraylist.get(i).get("prise_tag")) > 4000){
yourlayoutManager.smoothScrollTo(i);
break;
}
}
I hope this will help you somehow.

Nested list in android with listview from several xml files

I am creating an app that has several Buttons on the first screen. Every Button will parse a separate XML file and is supposed to create a nested list from that information.
All the XML files have this structure:
<blalist>
<house>
<name>Blaname1</name>
<eaddress>195 bla steet</eaddress>
<caddress>195 bla strasse</caddress>
<telephone>123456</telephone>
</house>
<house>
<name>Blaname2</name>
<eaddress>15 bla steet</eaddress>
<caddress>15 bla strasse</caddress>
<telephone>12345685</telephone>
</house>
</blalist>
After clicking the first button i would like to generate a ListView consisting out of only the names from tag "name". So the list should only show Blaname1,Blaname2...
After clicking on one of the names all the other tags should be displayed as content;
tags "eaddress","caddress","telephone" and three extra buttons.
Basically a 3 screens scenario. First page with the main buttons. Second page shows a list generated only with the names. Third screen shows the details of the specific name clicked.
How do i go about that? I found loads of info for parsing xml to ListView and some info about ExpandableListView but i couldnt really wrap my head around of how to go about this one. Please help!
All you need to do is parse your XML and structure the data in the format that is supported by the SimpleExpandableListAdapter class.
1) For groups
List<HashMap<String, String>> lstGroups
2) For sub items of groups
List<ArrayList<HashMap<String, String>>> lstGroupItems
and then you can use SimpleExpandableListAdapter
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
getApplicationContext(), lstGroups,
R.layout.group_row, // Group item layout XML.
new String[] { "Group Item" }, // the key that will identify group in `lstGroups`.
new int[] { R.id.row_name }, // ID of each group item.-Data
lstGroupItems, // childData describes second-level
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] { "Sub Item" }, // Keys that will identify child items in `lstGroupItems`
new int[] { R.id.grp_child }
);
setListAdapter(expListAdapter);
You can refer this example
Expandable ListView in ANDROID using SimpleExpandableListAdapter, a simple example.
Added :
I think you should use SQLiteDatabase if you are having large amount of data or else you can structure your data in the following format
Map<String, Map<String, String>> mapHouses;
Here the key will be the house name and its value will be again the Map which will hold the house properties and the corresponding value.
You will be requiring access to mapHouses accross diffrent activity so you can have the same in your application context i.e the Application class and pass the house name to the activity that neds to display the house details. But you will end up holding large data into the memory and which can be lost if your application is killed by Android.
Based on your comment in response to Vishal your question is actually asking how to build an entire application.
You need to build
a main activity with a button that launches your list activity
a list activity that displays a list of names
a detail activity that can be used to display the details associated with a name
In addition you'll need to decide on a technology you'll use to deserialize the XML and store it somehow. I'd recommend XStream to deserialize but there're loads of options and you may already have a preference.
Then you'll want to store the Name objects you've deserialized. I'd recommend that you use ORMLite or DB4O.
You'll need a way to load a list of all names and a way to load the details of a particular name from whichever persistence technology you've chosen.
Your best bet is to try and do those things and then post new questions about specific problems you have trying to build the application.
Hope that helps... and isn't just teaching grandma to suck eggs

Android list view to display unique values from xml source

For an Android applicaton, I am getting data from an XML source like below:
<item>
<description>product description A </description>
<category>categoryA</category>
</item>
<item>
<description>product description B </description>
<category>categoryB</category>
</item>
I want to show all the category values in a list view. When user clicks on one category, show all description values for that category.
For this, I need to extract all category values, remove the duplications, sort them in alphabetical order and use in a List View. What is the simplest way of doing it? If I write my own functions, it goes into so many loops. Any simpler way of achieving this?
I think that you should use a sax parser to retrieve each element and putting into a HashMap so no duplicate Key values are allowed, then you can use that Map to populate your list.
If you want to filter by category, an interesting option is to populate a db in your app with the parsed data so you can use Cursor to filter,search and query :)
An example of Sax Parser
The simpler way to "remove the duplications"
ArrayList<String> entries;
entries=new ArrayList<String>();
if(!entries.contains("this entry")){
entries.add("this entry");
}

Listview delete item from Database

Hey. I have the following code:
final String text = (String) lt.getItemAtPosition(position);
db.removeCategory(text);
What I want to do is to remove a item from a ListView. The problem I have is that I only can remove the item that is in the first position of the list.
Is like the getItemAtPosition(0);
Why's that? Can somebody help?
Thanks
Bind your array to ArrayAdapter and use its remove method to remove particular object. Refer this link to get some understanding of how it works.
Remove ListView items in Android
Why don't you add items into a collection or data datable and then bind it to ListView.
In ste removal phase you delete the item from the collection/data table instead direct one from the ListView.

auto search in listview

I created a list view which extends ListActivity and I have a search field at the top of the page
but I don't know how to write it , I want a search like search contact (type just only 1 char and listview will change, don't have to press any button)
please give me some example code
thanks
I think you should see this
If you are currently using a CursorAdapter to display the List, than you can create a custom CursorAdapter which does filtering automatically as you type keys. The following webpage provides an excellent example of this: http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/
Good luck!
Create a AutoCompleteTextView in your Layout,then put this in your class file,
AutoCompleteTextView txtPhoneNo = (AutoCompleteTextView) findViewById(R.id.txtPhoneNo);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_Val);
txtPhoneNo.setAdapter(adapter);
where name_Val is the String Array that contains "DATA"

Categories

Resources