1

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 enter image description here

how to do this, please help me......

4 Answers 4

2

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

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

1 Comment

thanx for your valuable answer. I am using the same idea in other activity where i am using array list. In array list i cant get the length. How can i do this....
0

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} 

Comments

0

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.

Comments

-1

Are you looking for notifyDataSetChanged()?

2 Comments

no...Actually the data get changed whenever i open and close the activity
@Siva K so what do u want actually...you have already created the custom ListView.

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.