Oscar Moreno Oscar Moreno - 1 month ago 20
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;
}

@Override
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);

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

Contact item = getItem(position);

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

holder.name.setText(item.name);
holder.surname1.setText(item.surname1);
holder.surname2.setText(item.surname2);
holder.phonenumber.setText(item.phonenumber);
}

return convertView;
}
}


The MainActivity is like this:

public class MainActivity extends ListActivity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean result = loadListFromFiles();
if (result) {
loadContacts();
}
}

private void loadContacts() {

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

setListAdapter(contactsAdapter);
}


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];

try
{
BufferedReader fin =
new BufferedReader(
new InputStreamReader(
openFileInput(fileName)));

int i = 0;

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

fin.close();

Contact contact = new Contact();

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

listContacts.add(contact);

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.

Answer

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

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

@Override
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;
}
Comments