Nicolas Frbezar Nicolas Frbezar - 6 months ago 49
Android Question

Create a BaseAdapter from Arraylist of custom object

I'm building a SMS app and to display a conversation flux in a listview. My data will be registered in an ArrayList of message, a class i've made to reference a SMS message item for my app.

For test purpose, i've made this adapter :

package fr.nf.smsplus;

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 java.util.List;

/**
* Created by Nicolas on 20/05/2016.
*/
public class conversationAdapter extends BaseAdapter {

Context context;
Message[] conv;
private static LayoutInflater inflater = null;

public conversationAdapter(Context context, Message[] conv) {
// TODO Auto-generated constructor stub
this.context = context;
this.conv = conv;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return conv[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 vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.item_bubble_me, null);
TextView text = (TextView) vi.findViewById(R.id.txtMsg);
text.setText(conv[position].content);
return vi;
}

}


Here is the output of what i've made :

enter image description here

My adapter is working. But i need now to select which bubble to use in the getView of my adapter, so i can't use the ArrayAdapter. I will have to make my own adapter.

So i need to convert everything to a ArrayList of Message instead of Array, to make a dynamic allocation of ressource for the array.

How can i modify my adapter to use ArrayList of Message correctly to apply it in a listview ?

Answer
package fr.nf.smsplus;

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 java.util.List;

/**
 * Created by Nicolas on 20/05/2016.
 */
public class conversationAdapter extends BaseAdapter {

    Context context;
    ArrayList<Message> conv;
    private static LayoutInflater inflater = null;

    public conversationAdapter(Context context, ArrayList<Message> conv) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.conv = conv;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return conv.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
        Message mMessage=conv.get(position);
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.item_bubble_me, null);
        TextView text = (TextView) vi.findViewById(R.id.txtMsg);
        text.setText(mMessage.content);
        return vi;
    }
}

Note: Try using ViewHolder, that will increase performance of the app.