找不到适合ArrayAdapter的构造函数

时间:2015-12-19 23:10:04

标签: java android android-arrayadapter

我收到以下错误:

Error:(34, 53) error: no suitable constructor found for ArrayAdapter(AllStores,ListView,Response<List<Store>>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List<String>) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(actual argument ListView cannot be converted to int by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(actual argument ListView cannot be converted to int by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(actual argument ListView cannot be converted to int by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable
(actual and formal argument lists differ in length)

这就是我的尝试:

        ArrayAdapter<String> stringArrayAdapter=new ArrayAdapter<String>(
                AllStores.this,
                lv,
                subprises);

我还尝试将ArrayAdapter的第一个参数替换为thisgetApplicationContext()context。不幸的是它没有用。我真的不知道我做错了什么。

如果你想看到它,你可以在下面看到我的代码:

AllStores.java:

public class AllStores extends Activity {
    private ListView lv;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_stores);
        lv = (ListView) findViewById(R.id.store_list);
        Response<List<Store>> subprises;
        try {
            subprises = new StoreService().getSubprises();
            Iterator it = subprises.body().iterator();
            ArrayAdapter<String> stringArrayAdapter=new ArrayAdapter<String>(
                    AllStores.this,
                    lv,
                    subprises);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

ArrayAdapter<String>构造函数不接受ListViewResponse<List<Store>>,并且在您的代码中添加了它们

ArrayAdapter<String> stringArrayAdapter=new ArrayAdapter<String> AllStores.this, lv, subprises);

但它应该采用Contextlayout resource idList<String>String[]

是一个简单的适配器
ArrayAdapter<String> adapter = new ArrayAdapter<>(AllStores.this, android.R.layout.simple_list_item_1, your_string_array_or_list);

然后将该适配器设置为ListView

lv.setAdapter(adatper);

如果您想要更复杂的适配器,则应创建自定义ArrayAdapter。看看这个链接
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter