Я думаю, что это может помочь вам.
// Основной класс
public class Place extends ListActivity
{
HashMap<String, String> map;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.placelist);
ArrayList<HashMap<String, String>> accountlist = new ArrayList<HashMap<String, String>>();
String xml = XMLParser.getXML();
Log.i("Retrieved Xml", xml);
Document doc = XMLParser.parse(xml);
//Parsing data directly from the XML
NodeList nodes = doc.getElementsByTagName("place");
for (int i = 0; i < nodes.getLength(); i++)
{
map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLParser.getValue(e, "id"));
map.put("name", XMLParser.getValue(e, "name"));
accountlist.add(map);
}
ListAdapter adapter = new PlaceAdapter(this, accountlist , R.layout.main,
new String[] { "id", "name" },
new int[] { R.id.id, R.id.name });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Intent intent = new Intent(Place.this, Results.class);
Bundle b = new Bundle();
b.putString("name", o.get("name"));
b.putString("id", o.get("id"));
intent.putExtras(b);
startActivity(intent);
}
});
}
//Place-Adapter
public class PlaceAdapter extends SimpleAdapter
{
private ArrayList<HashMap<String, String>> results;
public PlaceAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent)
{
int[] colors = new int[] {0x30ffffff, 0x30ff2020, 0x30808080};
View v = super.getView(position, view, parent);
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.main, null);
}
TextView id = (TextView) v.findViewById(R.id.id);
id.setText(results.get(position).get("id"));
TextView name = (TextView) v.findViewById(R.id.name);
name.setText(results.get(position).get("Name"));
int colorPos = position % colors.length;
v.setBackgroundColor(colors[colorPos]);
return v;
}
} }
// Парсинг
public class XMLParser {
public final static Document XMLfromString(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
*/
public final static String getElementValue( Node elem ) {
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return "";
}
public static String getXML(){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://dating.rs/nemanja/WebService/getPlaces.php?lat=44.8061999&lon=20.4595333&rad=3502&tfs=0");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
public static int numResults(Document doc){
Node results = doc.getDocumentElement();
int res = -1;
try{
res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
}catch(Exception e ){
res = -1;
}
return res;
}
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return XMLParser.getElementValue(n.item(0));
}
}
//Отображение результатов по щелчку списка
public class Results extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
Bundle b = getIntent().getExtras();
String id = b.getString("id");
String name = b.getString("name");
EditText aid = (EditText) findViewById(R.id.eid);
EditText aname = (EditText) findViewById(R.id.ename);
aid.setText(id);
aname.setText(name);
}
}