Pessoal,
Alguém poderia me ajudar com esse problema, estou buscando o nome da imagem de um arquivo xml, só que não consigo mostrar essa imagem no meu listview, o nome da imagem é o nome da pessoa, estou fazendo isso para um trabalho da faculdade e tentei me aventurar no mundo do android, ficaria mto grato com ajuda de alguém.
meu.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/sexo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000" />
<TextView
android:id="@+id/idade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"/>
<ImageView
android:id="@+id/nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</AbsoluteLayout>
FirstTab.java
package com.testenaweb;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class FirstTab extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();
Map<String, Object> data;
String xml = XMLfunctions.getXML("MEU SITE XML");
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(FirstTab.this, "No Data", Toast.LENGTH_LONG).show();
//finish();
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
data = new HashMap<String, Object>();
Element e = (Element)nodes.item(i);
data.put("nome", XMLfunctions.getValue(e, "nome"));
data.put("sexo", XMLfunctions.getValue(e, "sexo"));
data.put("idade", XMLfunctions.getValue(e, "idade"));
resourceNames.add(data);
}
SimpleAdapter adapter = new SimpleAdapter(this, resourceNames , R.layout.lista,
new String[] {"nome","sexo","idade" },
new int[] {R.id.nome, R.id.sexo,R.id.idade});
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);
Toast.makeText(FirstTab.this, "" + o.get("nome"), Toast.LENGTH_LONG).show();
}
});
}
}
XMLFunctions.java
public class XMLfunctions {
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 url){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
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 XMLfunctions.getElementValue(n.item(0));
}
}
ImageAdapter.java
package com.testenaweb;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
Context Con;
//array to hold the values of image resources
int [] Resources;
List<ImageView> views;
public ImageAdapter(Context con,int[] resources)
{
Con=con;
Resources=resources;
views=new ArrayList<ImageView>(resources.length);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Resources.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return views.get(position);
//return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
//return views.get(position).getId();
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView img=new ImageView(Con);
img.setImageResource(Resources[position]);
views.add(img);
return img;
}
}
A imagem está salva dentro da pasta res\drawable.
Desde já agradeço.