behzad razzaqi
behzad razzaqi

Reputation: 93

Android Gson getch data and save into listview

I have this simple code to fetch data with GSON and save into the listview:

protected void onPostExecute(List<behzad> beh) {
    mcountryx = tours;
    if (mcountryTours != null) {
        ListView listView1 = (ListView) findViewById(R.id.listView1);
        String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items);
        listView1.setAdapter(adapter);
    }
}

This code leads to an error, MessageBox:

Can not resolve Constructor ArrayAdaptor...

Whereas using the following code:

ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items);
listView1.setAdapter(adapter);

into the onCreateMethod, that messageBox not show! Can you please explain me what happens? thanks for attention to my problem.

My behzad Class is:

import com.google.gson.annotations.SerializedName;

public class behzad {

    @SerializedName("tourcountryname")
    public String CountryNamme;
    public TourCountry() {

    }

}

Upvotes: 0

Views: 106

Answers (1)

Rami
Rami

Reputation: 7929

For the future users,

You need to pass a context to your Adapter constructor.

this in your onCreate() referes to the Activity, while this inside the onPostExecute() refers to the AsyncTask(it's not a context), this is why you have Can not resolve Constructor ArrayAdaptor...

Change this line:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, items);

to:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(AcivityName.this,android.R.layout.simple_expandable_list_item_1, items);

Upvotes: 1

Related Questions