Home » Android » android – How to pass multiple array values in a Custom Adapter class for Custom List View?

android – How to pass multiple array values in a Custom Adapter class for Custom List View?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a list view with an image icon on the left and three text views on the right in a single row. I have created the layout, but while creating the adapter class I am facing some issues. Here is the code:

import android.app.Activity;
import android.widget.ArrayAdapter;

public class CustomList extends ArrayAdapter<String>{

    private final Activity context;
    private final String[][] listRow;
    private final Integer[] imageId;

    public CustomList(Activity context, String[][] listRow, Integer[] imageId){
        super(context, R.layout.rowlayout, listRow);
    }
}

First I created three 1D arrays for the three textviews, but in the constructor I was not able to pass them all.

So I had to create a 2D array where every row in the table would represent information about a single list view item.

But I am not able to do so. I get an error:

Cannot resolve method super(android.app.Activity, int, java.lang.String[][]) with 2D arrays

How do I solve this issue?

How to&Answers:

After trying for a long time, I found the solution. I created a separate class to store the data values for each individual row and created an array of objects of the same.

Here is the sample code:

import android.app.Activity;
import android.widget.ArrayAdapter;

class ListItem
{
    public String textview1;
    public String textview2;
    public String textview3;
    public int imageId;

    public ListItem(String t1, String t2, String t3, int imageId)
    {
        this.textview1 = t1;
        this.textview2 = t2;
        this.textview3 = t3;
        this.imageId = imageId;
    }
}

public class CustomList extends ArrayAdapter<Object>
{
    private Activity context;
    private ListItem listItem[];

    public CustomList(Activity context, ListItem li[])
    {
        super(context, R.layout.rowlayout, li);    
    }
}

Answer:

Create a model class(separately) which contains all the three fields which are required. Then create arraylist in your activity and add items to it. pass this arraylist to adapter it works.

Answer:

In my case, I changed ArrayAdapter and It is working
You can change this code

import android.app.Activity;
import android.widget.ArrayAdapter;

public class CustomList extends ArrayAdapter<String[]>{

    private final Activity context;
    private final String[][] listRow;
    private final Integer[] imageId;

    public CustomList(Activity context, String[][] listRow, Integer[] imageId){
        super(context, R.layout.rowlayout, listRow);
    }
}

My code sample

public class CustomAdepter extends ArrayAdapter<String[]> {
    public CustomAdepter(@NonNull Context context, String[][] message) {
        super(context, R.layout.message_list, message);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.message_list, parent, false);
        String singleMessage = getItem(position)[2];
        TextView textView = (TextView) customView.findViewById(R.id.firstLine);
        textView.setText(singleMessage);
        return customView;
    }
}

See the change in ArrayAdapter<String[]>, String[][] message and String singleMessage = getItem(position)[2];