gpt4 book ai didi

java - 无法在 onPostExecute 内更新/重绘 listView

转载 作者:太空狗 更新时间:2023-10-29 16:18:23 25 4
gpt4 key购买 nike

我有这个代码:

package com.gs.britishjokes;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.Application;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class TopJokes extends Activity {

public static class Globals extends Application{
String[] myStringArray = {"a","b","c","a","b","c","a","b","c","a","b","c","a","b","c","a","b","c"};

public String[] getMyStringArray() {
return myStringArray;
}

public void setMyStringArray(String[] myStringArray) {
this.myStringArray = myStringArray;
}

}

public static ArrayAdapter<String> adapter;
public static ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_jokes);

Globals globals = (Globals)getApplication();

new loadJson().execute();

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

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.top_jokes, menu);
return true;
}

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://site.com/android/britishJokes/showJokes.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) {
Globals globals = (Globals)getApplication();
JSONArray jsonArray = new JSONArray();
String[] newJokes = {};
String[] myStringArray = {"co","ao","bo","co","ao","bo","co","ao","bo","co"};
try {
jsonArray = new JSONArray(buffer);
} catch (JSONException e) {
e.printStackTrace();
}

for (int i = 0; i < jsonArray.length(); i++) {
try {
newJokes[i] = jsonArray.getJSONObject(i).toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
globals.setMyStringArray(myStringArray);
System.out.println("GLOBAL ARRAY: " + globals.getMyStringArray());

adapter.notifyDataSetChanged();
// listView.invalidate();
// listView.destroyDrawingCache();
// listView.setVisibility(ListView.INVISIBLE);
// listView.setVisibility(ListView.VISIBLE);

//listView.invalidateViews();

}

}
}

我正在尝试使用下面的代码更新 onPostExecute 中的 listView(也可以在上面的代码块中找到)

adapter.notifyDataSetChanged();
// listView.invalidate();
// listView.destroyDrawingCache();
// listView.setVisibility(ListView.INVISIBLE);
// listView.setVisibility(ListView.VISIBLE);

//listView.invalidateViews();

注释行是我尝试过但没有成功的东西。

我注意到代码运行正确,但 listView 没有更新。如果我按下后退按钮,然后再次打开相同的 Activity ,我可以看到正确的结果 myStringArray,但是一旦 asyncTask 完成,我该如何更新 listView?应该有办法做到这一点。我知道我错过了一些非常小的东西,但作为一个初学者,我无法发现它。请给我一个线索

编辑:

添加后目录异常

adapter.clear();
adapter.addAll(myStringArray);

02-10 13:50:09.594: E/AndroidRuntime(1460): FATAL EXCEPTION: main
02-10 13:50:09.594: E/AndroidRuntime(1460): Process: com.gs.britishjokes, PID: 1460
02-10 13:50:09.594: E/AndroidRuntime(1460): java.lang.UnsupportedOperationException
02-10 13:50:09.594: E/AndroidRuntime(1460): at java.util.AbstractList.remove(AbstractList.java:638)
02-10 13:50:09.594: E/AndroidRuntime(1460): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
02-10 13:50:09.594: E/AndroidRuntime(1460): at java.util.AbstractList.removeRange(AbstractList.java:658)
02-10 13:50:09.594: E/AndroidRuntime(1460): at java.util.AbstractList.clear(AbstractList.java:466)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258)
02-10 13:50:09.594: E/AndroidRuntime(1460): at com.gs.britishjokes.TopJokes$loadJson.onPostExecute(TopJokes.java:114)
02-10 13:50:09.594: E/AndroidRuntime(1460): at com.gs.britishjokes.TopJokes$loadJson.onPostExecute(TopJokes.java:1)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.os.AsyncTask.finish(AsyncTask.java:632)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.os.Handler.dispatchMessage(Handler.java:102)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.os.Looper.loop(Looper.java:137)
02-10 13:50:09.594: E/AndroidRuntime(1460): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-10 13:50:09.594: E/AndroidRuntime(1460): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 13:50:09.594: E/AndroidRuntime(1460): at java.lang.reflect.Method.invoke(Method.java:515)
02-10 13:50:09.594: E/AndroidRuntime(1460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-10 13:50:09.594: E/AndroidRuntime(1460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-10 13:50:09.594: E/AndroidRuntime(1460): at dalvik.system.NativeStart.main(Native Method)

最佳答案

执行以下操作:

 adapter.clear();
adapter.addAll(myStringArray);

如果低于 API 11:

执行以下操作:

 adapter.clear();
for(String s:myStringArray){
adapter.add(s);
}
adapter.notifyDataSetChanged();

重要的是你打电话的时候

  globals.setMyStringArray(myStringArray);

您正在更改数组全局字符串数组指向但不是一个适配器指向。

假设 A 和 B 分别是之前和之后的数组。最初:

全局数组-------->A

当你创建适配器时:

适配器------------>A

但是当您再次调用 setMyStringArray 时,您会:

全局数组------>B

编辑

改变:

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

到:

 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
adapter.clear();
adapter.addAll(globals.getMyStringArray());

关于java - 无法在 onPostExecute 内更新/重绘 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21685187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com