开发者

problem with list view in android using arraylist items

in my app i am using a list view. The data which i want to list out are from a database of my app and an xml server database.

For example i am getting some two set of data from server and store it in a array as follows

firstname = {arun, Arun, Rajesh} 
lastname  = {kumar, sundar, kannan}

Now again i am getting some data from my app database and store it in array as follows

first = {arul}
last  = {raj}

Now i have combined the array list together as follows

firstname.addAll(first);
lastname.addAll(last);

Now i have the output as

{arun, Arun, Rajesh, arul} 
{kumar, sundar, kannan, raj}

Now i want to list out these items as in the following image

problem with list view in android using arraylist items

how to do this, please help me....开发者_如何转开发..


If you finally get all your firstname and lastname in two separate array then it is very simple to display on ListView as you want

first create a layout which has a listview element lets us name main.xml

then create a another layout having two TextView in Horizontal layout orientation let us name mainitem.xml

so now you create a new activity then call setContentView(R.layout.main)

then retrive list view

and now create a adapter by following code

this code is in onCreate(Bundle b) method

ListView lv= (ListView)findViewById(R.id.listview1);

        // create the grid item mapping
        String[] from = new String[] {"col_1","col_2"};
        int[] to = new int[] { R.id.firstname, R.id.secondname};

        // prepare the list of all records
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i <firstname.length && lastname.length; i++)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("col_1",firstname[i]);
            map.put("col_2",lastname[i]);
            fillMaps.add(map);
        }

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.mainitem, from, to);
        lv.setAdapter(adapter);

create mainitem.xml file having two TextView which id are 1)R.id.firstname, 2) R.id.lastname in layout horizontal orientation


Create a bean to store data

Example

public class NameDetails {
    String firstName = null;
    String middleName = null;
NameDetails(String firstName, String middleName){
    this.firstName = firstName;
    this.middleName=middleName;
}
String getFirstName(){
    return firstName;
}
String getMiddleName(){
    return middleName;
}
}

Add data to that bean using

NameDetails name1 = new NameDetails("arun","kumar");
..
...

Create array of Bean Objects. use this array for listview

{name1 , name2, name3, name4} 


As per the snap you have attached in the question, you need to define the custom listview adapter for the same.

For defining a custom row with 2 columnar output, you need to define an xml layout file containing 2 textviews side by side. Don't be confused, just go through this example. In this example you need to modify the row layout file. Make a row file as such two textviews will be displayed side by side.


Are you looking for notifyDataSetChanged()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜