在列表视图中显示 json 条数据
Display json data in listview
我正在尝试显示从 api 中获取的数据。我可以将数据显示为文本视图和日志,但我无法将其显示为列表。
我遇到错误“Cannot resolve constructor
”
Error:(72, 68) error: no suitable constructor found for ArrayAdapter(<anonymous FutureCallback<JsonArray>>,int,ArrayList<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
这是完整的错误代码
Error:(72, 68) error: no suitable constructor found for ArrayAdapter(<anonymous FutureCallback<JsonArray>>,int,ArrayList<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; <anonymous FutureCallback<JsonArray>> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; <anonymous FutureCallback<JsonArray>> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; <anonymous FutureCallback<JsonArray>> cannot be converted to Context)
public class MainActivity extends AppCompatActivity {
ListView lView;
TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lView = (ListView) findViewById(R.id.lv);
tView = (TextView) findViewById(R.id.textView);
Ion.with(this).
load("https://jsonplaceholder.typicode.com/users").
asJsonArray().
setCallback(
new FutureCallback<JsonArray>()
{
@Override
public void onCompleted(final Exception ex,
final JsonArray array)
{
if(ex != null)
{
Toast.makeText(MainActivity.this,
"Error: " + ex.getMessage(),
Toast.LENGTH_LONG).show();
}
else
{
ArrayList<String> list = new ArrayList<String>();
for(final JsonElement element : array)
{
final JsonObject json;
final JsonElement nameElement;
final JsonElement usernameElement;
final String name;
final String username;
json = element.getAsJsonObject();
nameElement = json.get("name");
usernameElement = json.get("username");
name = nameElement.getAsString();
username = usernameElement.getAsString();
Log.d("X",
name + " -> " + username);
list.add(name);
list.add(username);
tView.setText(name);
}
/*
for (int i = 0; i < array.size(); i++) {
list.add(array.get(i).toString());
}*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,list); //ERROR HERE
lView.setAdapter(adapter);
}
}
});
}
您必须传递一个 Context
作为 Adapter
构造函数的第一个参数。您传递了 this
,这不是 Context
。根据您的代码,MainActivity.this
可能有效
删除构造函数中的 this
并改用 MainActivity.this
。在您的情况下,这是匿名内部实例 class 而不是 Activity(context).
我正在尝试显示从 api 中获取的数据。我可以将数据显示为文本视图和日志,但我无法将其显示为列表。
我遇到错误“Cannot resolve constructor
”
Error:(72, 68) error: no suitable constructor found for ArrayAdapter(<anonymous FutureCallback<JsonArray>>,int,ArrayList<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
这是完整的错误代码
Error:(72, 68) error: no suitable constructor found for ArrayAdapter(<anonymous FutureCallback<JsonArray>>,int,ArrayList<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; <anonymous FutureCallback<JsonArray>> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; <anonymous FutureCallback<JsonArray>> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; <anonymous FutureCallback<JsonArray>> cannot be converted to Context)
public class MainActivity extends AppCompatActivity {
ListView lView;
TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lView = (ListView) findViewById(R.id.lv);
tView = (TextView) findViewById(R.id.textView);
Ion.with(this).
load("https://jsonplaceholder.typicode.com/users").
asJsonArray().
setCallback(
new FutureCallback<JsonArray>()
{
@Override
public void onCompleted(final Exception ex,
final JsonArray array)
{
if(ex != null)
{
Toast.makeText(MainActivity.this,
"Error: " + ex.getMessage(),
Toast.LENGTH_LONG).show();
}
else
{
ArrayList<String> list = new ArrayList<String>();
for(final JsonElement element : array)
{
final JsonObject json;
final JsonElement nameElement;
final JsonElement usernameElement;
final String name;
final String username;
json = element.getAsJsonObject();
nameElement = json.get("name");
usernameElement = json.get("username");
name = nameElement.getAsString();
username = usernameElement.getAsString();
Log.d("X",
name + " -> " + username);
list.add(name);
list.add(username);
tView.setText(name);
}
/*
for (int i = 0; i < array.size(); i++) {
list.add(array.get(i).toString());
}*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,list); //ERROR HERE
lView.setAdapter(adapter);
}
}
});
}
您必须传递一个 Context
作为 Adapter
构造函数的第一个参数。您传递了 this
,这不是 Context
。根据您的代码,MainActivity.this
可能有效
删除构造函数中的 this
并改用 MainActivity.this
。在您的情况下,这是匿名内部实例 class 而不是 Activity(context).