简体   繁体   中英

Display json data in listview

I am trying to display data fetched from an api. I can display the data as a textview andin a log but I cannot get it to display as a list.

I am getting an error " 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

Here is the full error code

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

                        }

                    });

}

You have to pass a Context as the first parameter of the Adapter constructor. You pass this , which is not a Context . According to your code, MainActivity.this might work

Remove this in constructor and use MainActivity.this instead. this in your case is instance of anonymous inner class not an Activity(context).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM