自定义适配器的构造函数不匹配

时间:2016-11-17 16:29:51

标签: java android listview android-fragments android-asynctask

我正在尝试创建我的第一个自定义适配器,以便为我的Android应用生成列表视图。我从Api调用中获取数据,然后处理它并将其存储在arraylist中: -

class Person{
    String bioguide;
    String image;
    String lastname;
    String firstname;
    String district;
    String state;
    String party;}

public static ArrayList<Person> personData = new ArrayList<Person>();

现在在onpostexecute部分我试图创建一个listview和自定义适配器来显示我的数据,如下所示: -

ListView yourListView = (ListView) rootView.findViewById(R.id.state_listView);
ListAdapter customAdapter = new ArrayAdapter<Person>(ByState.this, R.layout.bystate_itemview,personData);    
            yourListView .setAdapter(customAdapter);   
        }
    }
     public class ListAdapter extends ArrayAdapter<Person> {    
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }    
        public ListAdapter(Context context, int resource, ArrayList<Person> items) {
            super(context, resource, items);
        }    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {    
            View v = convertView;    
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.bystate_itemview, null);
            }    
            Person p = getItem(position);    
            if (p != null) {
                TextView tt1 = (TextView) v.findViewById(R.id.last_name);
                TextView tt2 = (TextView) v.findViewById(R.id.first_name);    
                if (tt1 != null) {
                    tt1.setText(p.getLastname());
                }    
                if (tt2 != null) {
                    tt2.setText(p.getFirstname());
                }
            }    
            return v;
        }    
    }
}

我在一些互联网教程之后得到了上面的代码。问题是我在第一次使用customadapter来调用自定义适配器的构造函数的行中出现错误。它说无法解析构造函数。有人可以帮助我理解这一点。我知道我没有为我的案例定义合适的构造函数,请让我知道更改。我在片段中创建listview,片段类名是ByState。

1 个答案:

答案 0 :(得分:0)

在第二行替换

ListAdapter customAdapter = new ArrayAdapter<Person>(ByState.this, R.layout.bystate_itemview,personData);  

通过

ListAdapter customAdapter = new ListAdapter(ByState.this, R.layout.bystate_itemview, personData);

您可以创建自己的适配器类,但可以调用标准ArrayAdapter

相关问题