gpt4 book ai didi

android - 在 Android 中的 AsyncTask 之后更新 CustomArrayAdapter

转载 作者:行者123 更新时间:2023-11-30 02:40:04 26 4
gpt4 key购买 nike

我有一个 CustomArrayAdapter,我用它来更新自定义 ListView。

这工作正常,但是当我尝试更新 View 时 在使用 notifyDataSetChanged 在 AsyncView onPostExecute 中获得结果后,它不会更新,也不会返回任何异常。

onCreate 的代码 -

public class MainActivity extends ListActivity {
String[] presidents = {"Dwight D. Eisenhower","John F. Kennedy","Lyndon B. Johnson","Richard Nixon","Gerald Ford","Jimmy Carter","Ronald Reagan","George H. W. Bush","Bill Clinton","George W. Bush","Barack Obama"};
String[] pics = {"5237","5438", "184", "184", "184", "184", "184", "184", "184", "184", "184", "184"};
private String[] names;
private String[] pid;
ListActivity obj;
CustomArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
obj = this;
try {
adapter =new CustomArrayAdapter(obj, presidents, pics);
setListAdapter(adapter);
new DownloadTextTask().execute("http://nvoids.com/rest/hello/users/");
} catch(Exception e) {
Log.d("Listview1", "Exception: " + e.getLocalizedMessage());
}

}

AsyncTask 的 onPostExecute -

protected void onPostExecute(String result) {

Toast.makeText(getBaseContext(), result.substring(1, 10) ,Toast.LENGTH_SHORT).show();
try
{
JSONArray jarr = new JSONArray(result);
names = new String[jarr.length() -1];
pid = new String[jarr.length() -1];
for (int i =0; i< jarr.length(); i++) {
JSONObject jObj = new JSONObject(jarr.get(i).toString());
names[i] = jObj.getString("value");
pid[i] = jObj.getString("pid");
}
super.onPostExecute(result);
adapter =new CustomArrayAdapter(obj, names, pid);
adapter.notifyDataSetChanged();
} catch(Exception e) {
Log.d("ListView1", "after getting string: " + e.getLocalizedMessage());
}
}

顺便说一句,如果需要,CustomArrayAdapter 类 -

public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] presidents;
private final String[] imageIds;

public CustomArrayAdapter(Activity context,String[] presidents, String[] imageIds) {
super(context, R.layout.lvrowlayout, presidents);
this.context = context;
this.presidents = presidents;
this.imageIds = imageIds;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
//---print the index of the row to examine---
Log.d("Listview1",String.valueOf(position));

LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.lvrowlayout, null, true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.txtPresidentName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

txtTitle.setText(presidents[position]);

//imageView.setImageResource(imageIds[position]);

new DownloadImageTask((ImageView) imageView )
.execute("http://nvoids.com/dir/image_" + imageIds[position] + ".jpeg");


return rowView;

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Log.d("CustomArrayGetImage Error", urldisplay);
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("CustomArrayGetImage Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}

编辑 -

正如答案中传递的参数也不起作用-

  try {
adapter =new CustomArrayAdapter(obj, presidents, pics);
setListAdapter(adapter);
new DownloadTextTask(adapter).execute( "http://nvoids.com/rest/hello/users/");
} catch(Exception e) {
Log.d("Listview1", "Exception: " + e.getLocalizedMessage());
}

在 DownloadAsyncTask 中 -

   private class DownloadTextTask extends AsyncTask<String, Void, String> {
CustomArrayAdapter ca;

public DownloadTextTask(CustomArrayAdapter ca) {
this.ca = ca;
}

......

protected void onPostExecute(String result) {
//Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), result.substring(1, 10) ,Toast.LENGTH_SHORT).show();
try
{
JSONArray jarr = new JSONArray(result);
names = new String[jarr.length() -1];
pid = new String[jarr.length() -1];
for (int i =0; i< jarr.length(); i++) {
JSONObject jObj = new JSONObject(jarr.get(i).toString());
names[i] = jObj.getString("value");
pid[i] = jObj.getString("pid");
ca.addAll(names[i] , pid[i]);
}
/*
super.onPostExecute(result);
adapter =new CustomArrayAdapter(obj, names, pid);
lv.setAdapter(adapter);
obj.setListAdapter(adapter);
*/

ca.notifyDataSetChanged();
} catch(Exception e) {
Log.d("ListView1", "after getting string: " + e.getLocalizedMessage());
}
}

最佳答案

因为,您正在 AsyncTask 的 onPostExecute()

中创建自定义适配器的新对象

喜欢,

adapter =new CustomArrayAdapter(obj, names, pid);
adapter.notifyDataSetChanged();

未设置为 ListView。因此,要么将 setListAdapter() 的语法写入 onPostExecute() 中的 ListView(为此,您需要在 AsyncTask 中引用 ListView)或使用您在 ListActivityonCreate() 中创建的相同对象,将该适配器对象传递到 AsyncTask 的构造函数中,并在 onPostExecute()

关于android - 在 Android 中的 AsyncTask 之后更新 CustomArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914136/

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