SampathKumar
SampathKumar

Reputation: 2545

How to print a value in ListView in Android?

I am new to Android development. I am using ArrayList to store country names, then to display the country name in ListView, but the value are assign wrong, below attached the picture for your reference. How to display a order like Afghanistan (1st cell), Aland Island (2nd cell) etc...

Thanks

I tried the below code:

      private  ArrayList<String> Countryname = new ArrayList<String>(); 
      String dataAll;

      lv1 = (ListView)findViewById(R.id.ListView01);
      for(int i=0;i<allTitles.getLength();i++) {
            dataAll+=allTitles.item(i).getChildNodes().item(0).getNodeValue()+"\n";
            this.Countryname.add(dataAll);
       }

     lv1.setAdapter(new ArrayAdapter<String>(SampleAndroidActivity.
     this,android.R.layout.simple_list_item_1, Countryname));

enter image description here

Upvotes: 1

Views: 4294

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

try this:

     String dataAll="";

     lv1 = (ListView)findViewById(R.id.ListView01);
     for(int i=0;i<allTitles.getLength();i++)
     {
         dataAll=allTitles.item(i).getChildNodes().item(0).getNodeValue()+"\n";
         this.Countryname.add(dataAll);
     }

Because you are appending new values in dataAll so remove "+" sign from front.

dataAll=allTitles.item(i).getChildNodes().item(0).getNodeValue()+"\n";

Upvotes: 1

Related Questions