Home » Android » android – how to add customised layout to Arrayadapter?

android – how to add customised layout to Arrayadapter?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to create a ListView with customized layout. each item in the listView should look like as shown in the item.xml posted below.

in the code, i used

adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);

but it is not working because the constructor of the ArrayAdapter<T> accepts the second parameter as int something like

android.R.layout.simple_list_item_1

, and in my case it is customized layout which is

R.layout.listi_tems_layout

which adapter should i use or how to solve this. thanks

Item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">

<TextView 
    android:id="@+id/tvlist_topic"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<ImageView 
    android:id="@+id/ivList_delete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/delete_icon"
    android:contentDescription="icon to delete item from the Listview"
    android:layout_weight="1"/>
<CheckBox 
    android:id="@+id/cbList_hook"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:checked="false"
    android:layout_weight="1"/>

mainlayout:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
....
....
....

<ListView 
    android:id="@+id/lvEco_topics"
    android:layout_width="match_parent"
    android:layout_height="470dp"
    android:layout_below="@id/tvEco_topic"
    android:layout_marginTop="30dp"
    android:scrollbars="vertical"
    android:divider="@android:drawable/alert_light_frame"></ListView>
<Button 
    android:id="@+id/btEco_save"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/lvEco_topics"
    android:gravity="center"
    android:text="Save"/>

code:

public class MainActivity extends Activity {

private ArrayList<String> topicsList;
private ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    topicsList = new ArrayList<String>();
    topicsList.add("topic1");
    topicsList.add("topic2");
    topicsList.add("topic3");
    topicsList.add("topic4");
    topicsList.add("topic5");
    topicsList.add("topic6");

    adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);
How to&Answers:

ArrayAdapter accepts the second parameter as int something like
android.R.layout.simple_list_item_1

When not customizing getView method of ArrayAdapter then custom layout require one TextView with android:id="@android:id/text1" id and show value in one TextView.

To run application with current code add android:id="@android:id/text1" for TextView in R.layout.listi_tems_layout layout.

Because R.layout.listi_tems_layout layout contains other views also with TextView so create custom Adapter by extending ArrayAdapter class to access other views also.

See following example:Custom ArrayAdapter for a ListView (Android)

Answer:

create a class and extends with base adapter like this and then set this adapter

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.itoks.model.ClientDetails;

public class ClientListAdapter extends BaseAdapter {
    // ArrayList<String> name, company, email, id, status;
    ArrayList<ClientDetails> clientArrayList;
    Context c;

    public ClientListAdapter(Context c, ArrayList<ClientDetails> list) {
        clientArrayList = list;
        this.c = c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return clientArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return clientArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = null;
        LayoutInflater inflater = (LayoutInflater) c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            row = inflater.inflate(R.layout.listview_item_clients, parent,
                    false);
        } else {
            row = convertView;
        }
        ClientDetails detail = clientArrayList.get(position);
        TextView name = (TextView) row.findViewById(R.id.tvClientFullName);
        name.setText(detail.name);
        TextView email = (TextView) row.findViewById(R.id.tvClientEmail);
        email.setText(detail.email);
        TextView id = (TextView) row.findViewById(R.id.tvClientID);
        id.setText("ID : " + detail.id);
        TextView company = (TextView) row
                .findViewById(R.id.tvClientCompanyName);
        company.setText(detail.company);
        TextView status = (TextView) row.findViewById(R.id.tvClientStatus);
        status.setText("Status:" + detail.status);
        return row;
    }

}

Answer:

I’m not sure if it’s still relevant,
from the source ArrayAdapter seem to have constructor that accept textview resource in 3rd parameter, not sure if it’s juts newly added in 2017 :D,

ArrayAdapter adapter = new ArrayAdapter<>(context,YourCustomLayoutID,TextViewIDinYourLayout,ListData);

and from the source, if you didn’t supply textview layout id, it will assume that the whole view is a textview, so I think it’s possible to use custom view without extending/creating new adapter (I know, I hate to create one myself), but you need to provide textview used for the text

Answer:

As I understand you. You are trying to have custom layout in ArrayAdapter. It’s not possible. Yout need to write your own custom adapter extending arrayAdapter class where you define how to deal with your custom layout using your topicsList. Something like this:

public class ChatUserAdapter extends ArrayAdapter<UserChat> {

private Activity context;
private List<UserChat> userList;
private int ilayout;

public ChatUserAdapter(Activity context, int layout, List<UserChat> userList) {
    super(context, layout, userList);
    this.context = context;
    this.userList = userList;
    this.ilayout = layout;
}

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

static class ViewHolder {
    public TextView tvEmail;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    View rowView = convertView;
    if (rowView == null) {

        LayoutInflater layoutInflater = context.getLayoutInflater();
        rowView = layoutInflater.inflate(ilayout, null, true);
        viewHolder = new ViewHolder();
        viewHolder.tvEmail = (TextView) rowView.findViewById(R.id.tv_email);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();
    }

    UserChat item = userList.get(position);
    viewHolder.tvEmail.setText(item.getName() + " " + item.getSurname());
    return rowView;
}
}

Answer:

Please create your own ItemAdapter which extends ArrayAdapter<>.

public class ItemAdapter extends ArrayAdapter<Item> {
private int resource;
private List<Item> items;

public ItemAdapter(Context context, int resource, List<Item> items) {
    super(context, resource, items);
    this.resource = resource;
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout itemView;
    final Item item = getItem(position);

    if (convertView == null) {
        itemView = new LinearLayout(getContext());

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        layoutInflater.inflate(resource, itemView, true);
    }
    else {
        itemView = (LinearLayout) convertView;
    }

And load your item.xml in MainActivity onCreate()

public class MainActivity{
    private List<Item> items;
    private ItemAdapter itemAdapter;
    @Override
      protected void onCreate(Bundle savedInstanceState) {
      itemAdapter = new ItemAdapter(this, R.layout.item, items);