Android list view to display unique values from xml source - android

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

Related

Android: Storing an array with the app

I have an array of data 10,000 entries long. This data almost never changes and I could handle it in the app if it did anyway. It seems unnecessary for me to store this on an external database considering it is just string data so wont be large.
Is there a way of storing this on the users phone with the app. I was thinking the string resources or a csv file. But these don't seem like the quickest or best option. What are the considerations I need when deciding whether to upload it to a database vs storing it on the device. Currently I use DynamoDB so searching the array on many attributes would be difficult.
Thanks in advance for your help.
If your data will not be change in future . Means its static then put Data in on string.xml
OR
If your data will be change in future then go with it database .....
I would go with storing Strings in a String-Array in your Strings.xmllike so:
<resources>
<string-Array name ="data">
<item> Data 1 </item>
<item> Data 2 </item>
<item> etc etc </item>
</string-array>
</sesources>
and then within your class you would reference the array and store that array into your List
List<String> stringData = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.data)));
for (String row : stringData){
Log.d("Data", row);
}
if you're coding this from a Fragment I suggest using getActivity().getResources().getStringArray(R.array.data) or pass the context to your Fragment.
Seeing the size of your data (10,000), it's not good to use string resources.Also, your data are static which mean they will be access by read only. So it's better to use a file or Sqlite.However, if you do lot of queries to your data such as finding a specific entry, I encourage you to use Sqlite, because you can use SQLite index to query data faster, speed up sort...

ArrayList with Multiple data

I have a small issue with ArrayList. I have to fetch the document from the server.
The document contains 7 fields of data. I have to show the document names in the list view.
For this I have added different fields data to the different ArrayList. So when I click on the document name, based on the position of the document, I fetched the all fields data from the different Arraylist based on the position.
But Have a small issue using by using the above procedure. Is there any procedure that is not depend on the position, what I want is irrespective of position if I click on the Document,based on the keyword document data to be extract.
Any Help Appreciated.
Thanks in Advance.
I got your point. If you try to manage different ArrayLists then it would be difficult to manage it. I mean if you delete item from particular position from particular ArrayList then you will have to delete items from same position from other ArrayList, if you forgot to do so then it will be unbalanced.
Solution:
Instead feasible solution is to create ArrayList<Object> or ArrayList<HashMap<String,String>>, so your every item is the type of particular object, and every object contains detail and everything of particular items.
For example: ArrayList<Documents>, here ArrayList will contains list of Documents objects, and every objects contains values of 7 fields.
Its simply easy to define Documents class with getter/setter attributes.

Searchable String-Array, Android?

How can I search through an String-Array? I've got an dictionary app and the words are saved in a String-Array and it would be user-friendlier, if you could search for the word you want to look up, instead of looking its way to the word. Can somebody help?
Thanks.
You can try using an ArrayList instead. Then you can see if the word is in your 'dictionary' by using the contains method, ex:
ArrayList <String> myDictionary = new ArrayList<String>();
myDictionary.add(new String("foo"));
myDictionary.add(new String("bar"));
...
// To check if the word exists in your dictionary.
if (myDictionary.contains(new String("word_to_look_up")))
{
}
Not entirely sure what you are looking for- do you want to see if the word is in the array, or what is the goal? If you want the user to get to the word faster, changing the string-array will not help the UI jump to the right place.
If you want it to be like Google suggest you could take your array and make a tree data object, which each node representing a letter in a word. Then if the user types a, you go into that node and offer possible words.
You may use the Arrays.binarySearch() method to search an element from the sorted array.

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

logical problem in an android app

hi i would like to know solution for such a problem..... i have an xml file containing users data of around 1000 users listed out in alphabetical order. The xml file use to be as follow
<usersdata>
<user>
<id>1</id>
<firstname>A</firstname>
<middlename>AA</middlename>
......
......
</user>
<user>
<id>2</id>
<firstname>B</firstname>
<middlename>BB</middlename>
......
......
</user>
........
........
</usersdata>
Now from the above xml file i am parsing all the tags and storing them in an array list for each tag. I am listing out the Firstname in a listview, by array list of first name. When the any of the list is clicked, it opens up a new activity where the all other details of the selected name is been shown.
For example if third name in the list is clicked, by using its position(example 3), in the next activity i am listing out the third values stored in all the array lists i am using. This is what currently i am doing.
Now the problem is i have a edit box above the list view, named as a search box. If the letter S is typed in it, then all the names starting with S gets listed first. Opening the next activity by clicking the list now gets some wrong data, how to avoid this.
Please give me a suggestion....
For example if the first name C is clicked, it will be listed at position 3
There is a quick and dirty hack: you can store user id in the invisible field, retrieve it on click and use it as an argument for the second activity. I'm afraid I can't come up with better suggestions without seeing the code.
i tried out by setting some flag and by using an example code in developers site. And i am able to list out the data's but this idea does not full fill my apps requirement in the next activity. So i removed the search part in my app.
Sorry for posting such a question here......

Categories

Resources