我正在学习Android,想用用户的名称和电子邮件填充Listview。名称和电子邮件存储在sqlite表中。我正在使用baseadapter将数据绑定到我的视图。

这是我的代码:

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


这里列表的大小= 4

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


但在网上:

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


该列表应显示如下:

Manish
Manish.com

John
John.com

XYZ
XYZ.com


感谢您的帮助。
日志猫:

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

最佳答案

这就是您的CustomAdapter类的外观

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);
        }
    }
}

关于java - Android Baseadapter填充ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38401558/

10-10 07:54