尝试填充列表视图时找不到合适的构造函数

时间:2016-03-01 05:20:44

标签: android listview

我试图将数据从sqlite db加载到listview。

以下是我用来填充列表视图的代码

List allPendingOrders = notificationDbHandler.getInstance(getActivity().getApplicationContext()).getAllPendingOrders();

ListView mListView = (ListView) mRootView.findViewById(R.id.list);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, allPendingOrders);
 mListView.setAdapter(adapter);

以下是我得到的错误消息,

Error:(180, 76) error: no suitable constructor found for ArrayAdapter(<anonymous Listener<JSONObject>>,int,List)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable
(actual argument <anonymous Listener<JSONObject>> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,Object[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,Object[]) is not applicable
(actual argument <anonymous Listener<JSONObject>> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(actual argument <anonymous Listener<JSONObject>> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable
(actual and formal argument lists differ in length)

2 个答案:

答案 0 :(得分:1)

您的构造函数中的this参数指向一个侦听器,而不是您正在使用它的活动。尝试使用

      ArrayAdapter adapter = new ArrayAdapter(YourActivityName.this,android.R.layout.simple_list_item_1, allPendingOrders); 

如果您在片段中使用适配器,您可以使用ρяσѕρєяK建议。

答案 1 :(得分:1)

试试这个

ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1, allPendingOrders);

mListView.setAdapter(适配器); 你缺少textview的id 有关详细信息,请参阅此处: - http://coderzpassion.com/android-working-with-listview/