Oscar Moreno Oscar Moreno - 2 months ago 30
Android Question

Android ArrayList Adapter doesn't show the ListView

I have an ArrayList to represent in a ListView by an Adapter:

private ArrayList <Contact> listContacts = new ArrayList <Contact> ();

This is the (simple) Contact Class:

public class Contact {
String pic;
String name;
String surname1;
String surname2;
String phonenumber;

And this is the Adapter Class:

public class ContactsAdapter extends ArrayAdapter<Contact> {

private static class ViewHolder {
ImageView pic;
TextView name;
TextView surname1;
TextView surname2;
TextView phonenumber;

Context context;

public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
this.context = context;

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.layout_contact, parent, false);

holder = new ViewHolder();
holder.pic= (ImageView) convertView.findViewById(R.id.pic);
holder.name= (TextView) convertView.findViewById(name);
holder.surname1= (TextView) convertView.findViewById(R.id.surname1);
holder.surname2= (TextView) convertView.findViewById(R.id.surname2);
holder.phonenumber= (TextView) convertView.findViewById(R.id.phonenumber);

} else {
holder = (ViewHolder) convertView.getTag();

Contact item = getItem(position);

if (item!= null) {
int idImage = context.getResources().getIdentifier(item.pic, "drawable", context.getPackageName());


return convertView;

The MainActivity is like this:

public class MainActivity extends ListActivity {

private ArrayList <Contact> listContacts = new ArrayList <Contact> ();
ContactsAdapter contactsAdapter;

protected void onCreate(Bundle savedInstanceState) {
boolean result = loadListFromFiles();
if (result) {

private void loadContacts() {

contactsAdapter = new ContactsAdapter(getApplicationContext(), R.layout.activity_main, listContacts);


The loadListFromFiles code:

private boolean loadListFiles(){

boolean result = false;
Context context = getApplicationContext();
File path = context.getFilesDir();

File[] files = path.listFiles();
Log.d("Files", "Size: "+ files.length);

for (int i = 0; i < files.length; i++)
Log.d("Files", "FileName:" + files[i].getName());
result = loadFile(files[i].getName());
return resultado;

private boolean loadFile(String fileName) {

String[] arrayContact = new String[4];

BufferedReader fin =
new BufferedReader(
new InputStreamReader(

int i = 0;

String line= "";
while ((line= fin.readLine()) != null) {
arrayContact[i] = linea;


Contact contact = new Contact();

contact.pic = arrayContact[3];
contact.name= arrayContact[0];
contact.surname1 = arrayContact[1];
contact.surname2 = arrayContact[2];
contact.phonenumber= arrayContact[3];


return true;

catch (Exception ex)
Log.e("Files", "Error to read file by memory");
return false;


The ArrayList is right, but the layout don't show anything.


Override getCount() and getItem() methods to return count and contact item.

public int getCount() {
    if(items == null)
        return 0;
    return items.size();

public Contact getItem(int i) {
    return items.get(i);

Also, create one variable items in Adapter class and assign it to parameter passed to adapter constructor.

ArrayList<Contact> items;

public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
    super(context, textViewResourceId, items);
    this.context = context;
    this.items = items;