私はAPIからフェッチしたデータを表示しようとしています。私はデータをテキストビューとログに表示できますが、リストとして表示することはできません。リストビューでjsonデータを表示
私はエラー "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);
}
}
});
}
ArrayAdapterの "Mainactivity.this"に "this"を置き換えます。adapter = new ArrayAdapter (this、android.R.layout.simple_list_item_1、list); –
WritingForAnroid