Ravi Kanasagra
Ravi Kanasagra

Reputation: 611

Show 2 object data in single row in listview in Android

I have a requirement as shown in this image.

enter image description here

I have 2 show 2 object data on single row. Normally we show these information as one object in one row. How could we achieve it.

Upvotes: 0

Views: 122

Answers (3)

user7451086
user7451086

Reputation:

You need ListView xml, ListItem xml, ListItem class (object), ArrayAdaper class

Set a row view and items in that view in ListItem xml. Create ListItem class (object) with variables (Such as String, int) you needed. Bind the view and item in ArrayAdapter class.

Upvotes: 0

Sunisha Guptan
Sunisha Guptan

Reputation: 1605

You can use,

  1. GridView
  2. RecyclerView with GridLayoutManager

Upvotes: 2

Exigente05
Exigente05

Reputation: 2211

You can achieve it customizing getView() method in listview adapter. what getView() method do?

getView() normally generates view for each row in a general sense.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
    //inside this we populate data using position value
}

Here you need to show two object (sequence or random) data in a single row. You need to use another int variable to keep track about object size.

initialize it on top, int i = 0;

inside getView() you need to do something extra because you need to populate 2 object data on a single getView() method call.

  1. Create 2 object and populate data using i.
  2. increment i value after every object creation.
  3. Put an if condition do check i value is less than your array size. Its because getView() method will call based on your array size and you need to populate data half / less of that.

****Populate data inside above if condition.

*Better you try Gridview for this type of needs.

*Custom layout GridView example

Upvotes: 1

Related Questions