1

I would like to populate a ListView from an ArrayList.

The ArrayList is filled with correct values. On the emulator, what I get is, the list item with value

packagename ClassName@someNumber

Does anybody has the same problem??

    public class ExchangeMoneyMKActivity extends Activity {

    Document dom;
    private static final String METHOD_NAME = "GetExchangeRate";

    private static final String NAMESPACE = "http://www.nbrm.mk/klservice/";
    private static final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
    private static final String URL = "http://www.nbrm.mk/klservice/kurs.asmx?kurs";
    ListView lw;

    ArrayList<String>currencyShortNames=new ArrayList<String>();
    ArrayList<String>currencyRates=new ArrayList<String>();
    ArrayList<ExchangeRate> currencyList=new ArrayList<ExchangeRate>();
    ArrayAdapter<ExchangeRate> aa;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        currencyShortNames.add("EUR");
        currencyShortNames.add("USD");
        currencyShortNames.add("GBP");
        currencyShortNames.add("CHF");
        currencyShortNames.add("SEK");
        currencyShortNames.add("NOK");
        currencyShortNames.add("JPY");
        currencyShortNames.add("DKK");
        currencyShortNames.add("CAD");
        currencyShortNames.add("AUD");


        lw=(ListView)this.findViewById(R.id.listView1);


        aa = new ArrayAdapter<ExchangeRate>(
                    this,android.R.layout.simple_list_item_1,
                    currencyList);
        lw.setAdapter(aa);

        callService();

    }

    private void callService() {
        try{

            SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);

            SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
            java.util.Date date = new java.util.Date();
            request.addProperty("StartDate",dateFormat.format(date));
            request.addProperty("EndDate",dateFormat.format(date));

            SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            //MyXmlParserHandler parser=new MyXmlParserHandler();
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive  result = (SoapPrimitive) envelope.getResponse();
             String resultData = result.toString();
            // System.out.println(resultData);
            int strStart=resultData.lastIndexOf("schema");
            int strStop=resultData.lastIndexOf("dsKurs");
            int strLength=strStop-strStart-10;
            String responseXML=resultData.substring(strStart+10,strLength);
            responseXML.replace("&lt;", "<");
            responseXML.replace("&gt;", ">");
            String xmlDocument="<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
                    "<dsKurs>" +
                    responseXML + 
                    "</dsKurs>";
            System.out.println(xmlDocument);

            XMLfromString(resultData);

        }catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void XMLfromString(String resultData) throws ParserConfigurationException, SAXException, IOException {

        // TODO Auto-generated method stub
        InputStream is = new ByteArrayInputStream(resultData.getBytes("UTF-8"));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();
        try{
            dom = db.parse(is);
        }catch(Exception e){
            System.out.println("Fatal error");
        }

        dom.getDocumentElement();
        NodeList nl = dom.getElementsByTagName("KursZbir");
        currencyList.clear();
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0 ; i < nl.getLength(); i++) {

                    Element kursZbir = (Element)nl.item(i);
                    Element sreden = (Element)kursZbir.getElementsByTagName("Sreden").item(0);
                    currencyRates.add(sreden.getFirstChild().getNodeValue());
            }
        }
        for(int i=0;i<currencyShortNames.size();i++){
            currencyList.add(new ExchangeRate(currencyShortNames.get(i).toString(),currencyRates.get(i).toString()));
            System.out.println(currencyList.get(i).shName.toString());
            System.out.println(currencyList.get(i).Currency.toString());

            aa.notifyDataSetChanged();
        }
    }
}
4
  • It's like you need a ListAdapter. This is a way to tell ListView what to display Commented May 31, 2012 at 22:34
  • 1
    You need to show your code - that would be helpful Commented May 31, 2012 at 22:39
  • post the code you are using, and if possible a screenshot of the current output. Commented May 31, 2012 at 22:40
  • In future, for your information - please ensure you have your code properly formatted! Commented Jul 15, 2012 at 0:21

3 Answers 3

2

You actually need a custom ArrayAdapter for ExchangeRate. Currently, your listview would not know which property of the ExchangeRate to display and is probably calling .toString() on each instance of Exchange Rate

http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

class ExchangeRateAdapter extends ArrayAdapter<ExchangeRate> {

    public ExchangeRateAdapter(Context context, ArrayList<ExchangeRate> rate) {
        super(context, android.R.layout.simple_spinner_item, rate);
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ExchangeRate rate = (ExchangeRate) getItem(position);
        TextView view = new TextView(this.getContext());
        view.setTextColor(0);
        view.setHeight(30);
        view.setText(rate.getAmount());
        return view;
    }
}

then

aa = new ExchangeRateAdapter();
list.setAdapter(aa);

in the getView (..) method, the code specifically tells ListView to show Amount and no more .toString()

Sign up to request clarification or add additional context in comments.

Comments

0

There's nothing out the box that does this. However, I created my own version of ListAdapater that implements java.util.List and allows you to set any implementation of java.util.List into it. Essentially it works like this:

 public class abstract ListAdapter<T> extends BaseAdapter implements List<T> {

     List<T> list;

     public ListAdapter( List<T> contents ) {
        list = contents;
     }

     public int getCount() {
        return list.size();
     }

     public T getItem(int i) {
        return list.get(i);
     }

     public long getItemId( int i ) {
        return i;
     }

     public int size() {
        return list.size();
     }

     public boolean isEmpty() {
        return list.isEmpty();
     }

     public boolean contains( Object o ) {
        return list.contains(o);
     }

     public boolean addAll( Collection<? extends T> c ) {
        boolean result = list.addAll(c);
        notifyDataSetChanged();
        return result;
     }

     // so on and so on essentially delegating every method in List 
     // interface to the underlying list.  Of course mutators will have to call 
     // notifyDataSetChanged()
 }

I also have several other improvements that make it easier to create and bind to the view using the prescribed mechanisms of view recycling as well. So you implement createView() and bindView() instead of implementing getView().

Finally, it makes create flexible lists a snap:

 public void onCreate( Bundle saved ) {

     ListAdapter<String> adapter = new ListAdapater<String>() { ... };
     adapter.add( "Foo" );
     adapter.add( "Bar" );
     adapter.add( "BaZ" );

     listView.setAdapter( adapter );
 }

Its vastly more productive than anything out of the box Android.

Comments

0
    ListView myListView = (ListView) findViewById(R.id.myListView);

    listAdapter = new ArrayAdapter<String>(this, 
                      android.R.layout.simple_list_item_1, 
                      myArrayList);
    myListView.setAdapter(listAdapter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.