ListView problema

3 respostas
L

Boa tarde rapaziada, eu tô com um problema aqui tentando seguir esse tutorial [url]http://p-xr.com/android-tutorial-how-to-parseread-xml-data-into-android-listview/[/url] , na verdade não sou nem um pouco catedrático em Android, no seguinte código o eclipse fica frisando em vermelho dizendo que o código tá errado mas não tô sabendo resolver;

package com.delphimasters.androidboletos;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {
	
	EditText edittext_localiza_boleto;
	Button   localiza_boleto_botao;
	ListView lista_boletos;
	
	
	public static String getXML(String filtro){		
	    String line = null;
	    try {
	
	        DefaultHttpClient httpClient = new DefaultHttpClient();		
	        HttpPost httpPost = new HttpPost("http://delphimasters.zapto.org/boletoservidor/pesquisaboleto.php?chave=marcus&filtro="+filtro);
	        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 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;
	}	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.edittext_localiza_boleto = (EditText) findViewById(R.id.edittext_localiza_boleto);
        this.localiza_boleto_botao    = (Button) findViewById(R.id.localiza_boleto_botao);
        this.lista_boletos            = (ListView) findViewById(R.id.lista_boletos);                
        this.localiza_boleto_botao.setOnClickListener(this);
    }
    
    public void onClick(View v) {        
        if(v == this.localiza_boleto_botao) {        	
	        ArrayList<hashmap<string, string="">> mylist = new ArrayList<hashmap<string, string="">>();
	        String xml     = XMLfunctions.getXML();
	        Document doc   = XMLfunctions.XMLfromString(xml);
	        int numResults = XMLfunctions.numResults(doc);
	        if((numResults <= 0)){
		        Toast.makeText(Main.this, "Nenhum boleto encontrado!", Toast.LENGTH_LONG).show();
		        finish();
	        }
	        NodeList nodes = doc.getElementsByTagName("boleto");
	        
	        //popula a lista com os boletos
	        for (int i = 0; i < nodes.getLength(); i++) {
		        HashMap<string, string=""> map = new HashMap<string, string="">(); 
		        Element e = (Element)nodes.item(i);
		        map.put("nossonumero", "Nosso Número:" + XMLfunctions.getValue(e, "nossonumero"));
		        map.put("cliente", "Cliente:" + XMLfunctions.getValue(e, "cliente"));
		        map.put("vencimento", "Vencimento: " + XMLfunctions.getValue(e, "vencimento"));
		        map.put("valor", "Valor: " + XMLfunctions.getValue(e, "valor"));	        
		        mylist.add(map);
	        }
	        
	        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "nossonumero", "cliente", "vencimento", "valor" },
	        		                                new int[] { R.id.item_title, R.id.item_subtitle });
	        
	        setListAdapter(adapter);
	        lista_boletos.setTextFilterEnabled(true);
	        lista_boletos.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(Main.this, "Nosso Número '" + o.get("nossonumero") + "' Teste selciona ítem", Toast.LENGTH_LONG).show();
	            }
	        });        		
        }
    }     
}

Os erros estão nessas linhas aqui mas eu não tô conseguindo resolver;

ArrayList<hashmap<string, string="">> mylist = new ArrayList<hashmap<string, string="">>();
HashMap<string, string=""> map = new HashMap<string, string="">();
HashMap<string, string=""> o = (HashMap<string, string="">) lv.getItemAtPosition(position);

Qualquer ajuda é sempre bem vinda galera,
Abraços a todos.

3 Respostas

fabriciov

String em java não é primitiva, então escreve com S maiusculo
Nunca tentei/vi inicializar dessa forma , não sei se é coisa nova do java.

ArrayList<Hashmap<String, String>> mylist = new ArrayList<Hashmap<String, String>>();  

HashMap<String, String> map = new HashMap<String, String>();  

HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
L
fabriciov:
String em java não é primitiva, então escreve com S maiusculo Nunca tentei/vi inicializar dessa forma , não sei se é coisa nova do java.
ArrayList<Hashmap<String, String>> mylist = new ArrayList<Hashmap<String, String>>();  

HashMap<String, String> map = new HashMap<String, String>();  

HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);

Obrigado pela ajuda amigão, fiz mas não deu certo. Alguma ideia?

siloe

Se tiver usando o eclipese ctrl+shift+o, acho que pode ajudar!

Criado 26 de fevereiro de 2013
Ultima resposta 27 de fev. de 2013
Respostas 3
Participantes 3