Unable to update Android widget list in onPostExecute

I have this in my method onCreate:

String[] myStringArray = {"a","b","c","a","b","c","a","b","c","a","b","c","a","b","c","a","b","c"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_top_jokes);        

    new loadJson().execute();

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

    ListView listView = (ListView) findViewById(R.id.topJokesList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}

The above code fills the listView with content myStringArraywhile everything is in order. The problem occurs when I call new loadJson().execute();, it runs fine, and here is the method code:

public class loadJson extends AsyncTask<Void, Integer, String>{

@Override
protected String doInBackground(Void... params) {
    URL u;
    StringBuffer buffer = new StringBuffer();
    try {
    u = new URL("https://website.com/content/showContent.php");
    URLConnection conn = u.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                                conn.getInputStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        buffer.append(inputLine);
    in.close();

    }catch(Exception e){
        e.printStackTrace();
    }
    return buffer.toString();
}

protected void onPostExecute(String buffer) {
    JSONArray jsonArray = new JSONArray();
    try {
        jsonArray = new JSONArray(buffer);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("JSONarray: " + jsonArray);
    String[] newArray = null;
    for (int i = 0; i < jsonArray.length(); i++) {
        try {

        newArray[i] = jsonArray.getJSONObject(i).toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    myStringArray = newArray;
 }

}

As you can see, I am updating hardcoded content myStringArraywith the new values ​​obtained in newArray. Now I do not see new content. I know it there, but how can I say the current activity: "Hey, I updated your array, please show it!"

I know that I am missing something very small, but as a beginner, I can’t notice it.

+4
9

, clear addAll ( AsyncTask ).

+2

onPostExecute :

ListView listView = (ListView) findViewById(R.id.topJokesList);
listView.getAdapter().notifyDataSetChanged();
+2
protected void onPostExecute(String buffer) {
JSONArray jsonArray = new JSONArray();
try {
    jsonArray = new JSONArray(buffer);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("JSONarray: " + jsonArray);
String[] newArray = null;
for (int i = 0; i < jsonArray.length(); i++) {
    try {

    newArray[i] = jsonArray.getJSONObject(i).toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
myStringArray = newArray;

}

, (doInBackgroud()), doInBackgound. onPostExecute listview arrayadapter oncreate, .. Global

listView = (ListView) findViewById(R.id.topJokesList);
listView.getAdapter().notifyDataSetChanged();

, ...

+2

ArrayList adapter.notifyDataSetChanged ( ArrayList, , ArrayAdapter).

( onCreate) AsyncTask.onPostExecute:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, newArray);
listView.setAdapter(adapter);
+1

, . , , .

- AsyncTask:

private ListView listView;
private Activity activity;

AsyncTask, :

  public loadJson(Activity activity) {
        this.activity = activity;
    }

, AsyncTask execute(), ( AsyncTask).

new loadJson(this).execute(param);

, AsyncTask, postExecute(), :

protected void onPostExecute(String output) {
        listView = (ListView) activity.findViewById(R.id.targetListView);
        ArrayAdapter<String> adapter = (ArrayAdapter<String>)listView.getAdapter();
        adapter.add(output);
        adapter.notifyDataSetChanged();
    }

, findViewById. , targetListView ListView, .

AsyncTask, . notifyDataSetChanged(), .

, .

+1

: A. Strings.xml B. -

<string name="example1">hi</string>
<string name="example2">hello</string>

strings.xml :

label.setText("@string/example2");

1. , , ...

0

asynctask:

:

    LoadJson lj = new Json(adapter);
    lj.execute();

LoadJson:

private  ArrayAdapter<String> mAdapter;

:

public LoadJson(ArrayAdapter<String> adapter){
      mAdapter = adapter;
}

, postExecute . , !

0

loadJson ListActivity ( Activity , )

0

AsyncTask:

public class loadJson extends AsyncTask<Void, Integer, String>{

    ArrayAdapter<String> adapter;

    public loadJson(ArrayAdapter<String> adapter) {
        this.adapter = adapter;
    }

    //...

    protected void onPostExecute(String buffer) {
        if (this.adapter != null) {
            // update adapter
        }
    }

}

new loadJson(adapter).execute();  

Adapter onPostExecute

0
source

Source: https://habr.com/ru/post/1526023/


All Articles