【问题标题】:no suitable constructor found for ArrayAdapter找不到适合 ArrayAdapter 的构造函数
【发布时间】:2015-12-20 07:32:35
【问题描述】:

我收到以下错误:

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

【问题讨论】:

  • 您正试图在ArrayListString 中传递ObjectResponse&lt;List&lt;Store&gt;&gt;...您必须创建自己的扩展ArrayAdapter 的适配器类,然后使它像这样。默认构造函数需要Context、layoutId、StringArray...
  • 可以看到自定义适配器的实现here

标签: java android android-arrayadapter


【解决方案1】:

ArrayAdapter&lt;String&gt; 构造函数不接受 ListView 也不接受 Response&lt;List&lt;Store&gt;&gt; 并且在您的代码中您将它们都添加了

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

但它应该采用Contextlayout resource idList&lt;String&gt;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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2016-03-24
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    相关资源
    最近更新 更多