xml parsed result stores in an array without repetition of values in array
I am able to display开发者_Python百科 sax xml parsing and result is stored in an array list but that array contains more than one time same value as in my xml files now I want to enter one value only one time menas no repetition of value om same kind...as you look in xml file url http://site4demo.com/artealdiaonline/output.php?lat=-34.6394879&lng=-58.3617837kkj in this url "TYPE" tag contain same type of value more then one ..I want to enter it in array only one time ...I think my qus will be clear ..so dear how to do this?
this code throwing java.lang.NullPointerException
, why?
Spinner s = (Spinner) findViewById(R.id.Spinner01);
**ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ciudad);**
s.setAdapter(adapter);
You can use the contains
method of ArrayList to check whether a value already exists. This will be very inefficient if the list of unique values is long. A better approach would be to also maintain a HashSet of the values you have already added to the ArrayList and check against that instead of the ArrayList itself. If you do not need to preserve the order of values as they were encountered in the XML, just use the HashSet alone and (if needed) convert to an ArrayList after the parsing is done.
Maybe look at a Set as your data structure e.g. HashSet. Or add some logic to not add duplicates to your Array.
ArrayList ciudad = new ArrayList(); for (int i = 0; i < sitesList.getCiudad().size(); i++) {
if (!ciudad.contains(sitesList.getCiudad().get(i)))
{
ciudad.add(sitesList.getCiudad().get(i));
}
}
Spinner s = (Spinner) findViewById(R.id.Spinner01);
**ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ciudad);**
s.setAdapter(adapter);
精彩评论