Manish Gupta Manish Gupta - 4 months ago 17
Android Question

Android Baseadapter to populate ListView

I am learning Android and want to populate

Listview
with Name & email of user. Name & email are stored in
sqlite
table. I am using a
baseadapter
to bind the data to my view.

Here is my code :

public class Contact {

int _id;
String _name;
String _email;
String _phone_number;
String _password;

public Contact () {

}

public Contact (int id, String name, String phone_number, String email, String password) {
this._id = id;
this._name = name;
this._email = email;
this._phone_number = phone_number;
this._password = password;
}

public Contact (String name, String phone_number, String email, String password ){
this._name = name;
this._phone_number = phone_number;
this._email = email;
this._password = password;
}

// GETTERS AND SETTERS
}


public class CustomAdapter extends BaseAdapter {

private static LayoutInflater inflater=null;
//Context context;
ArrayList list;

Contact contact;

public CustomAdapter (Context context, ArrayList lists) {
this.list = lists;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
int length = list.size();
return length;
}

@Override
public Object getItem(int position) {
return list.get(position);
}

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

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

if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_list_view,null);
}

contact = null;
contact = (Contact) list.get(position);

TextView name = (TextView) convertView.findViewById(R.id.user_names);
name.setText(contact.get_name());

TextView email = (TextView) convertView.findViewById(R.id.user_emails);
email.setText(contact.get_email());

TextView phone = (TextView) convertView.findViewById(R.id.user_phones);
phone.setText(contact.get_phone_number());

return convertView;
}
}


here list has size = 4

0 = Manish
1 = manish.com
2 = John
3 = john.com


but on line :

contact = (Contact) list.get(position);
the cast class exception is thrown


The list should be displayed like this :

Manish
Manish.com

John
John.com

XYZ
XYZ.com


Thanks for you help.
Log cat :

FATAL EXCEPTION: main
Process: com.example.manish.userdetail, PID: 21749
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.manish.userdetail.Contact
at com.example.manish.userdetail.CustomAdapter.getView(CustomAdapter.java:67)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1630)
at android.widget.AbsListView.onLayout(AbsListView.java:2087)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14860)
at android.view.ViewGroup.layout(ViewGroup.java:4643)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:73

Answer

This how your CustomAdapter class should look like

class CustomAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    ArrayList<Contact> list;

    public CustomAdapter(Context context, ArrayList<Contact> lists) {
        this.list = lists;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_list_view, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

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

        holder.name.setText(list.get(position).get_name());
        holder.email.setText(list.get(position).get_email());
        holder.phone.setText(list.get(position).get_phone_number());

        return convertView;
    }

    private static class ViewHolder {
        TextView name, email, phone;

        ViewHolder(View covertView) {
            name = (TextView) covertView.findViewById(R.id.user_names);
            email = (TextView) covertView.findViewById(R.id.user_emails);
            phone = (TextView) covertView.findViewById(R.id.user_phones);
        }
    }
}
Comments