我有一个CustomArrayAdapter,我用它来更新自定义ListView。
这工作正常,但是当我尝试更新视图时 使用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());
}
}
答案 0 :(得分:1)
因为,您正在AsyncTask onPostExecute()
像,
adapter =new CustomArrayAdapter(obj, names, pid);
adapter.notifyDataSetChanged();
未设置为ListView
。所以要么在setListAdapter()
中为onPostExecute()
写入ListView的语法(为此你需要在AsyncTask中使用ListView引用)或
使用您在onCreate()
的{{1}}中创建的相同对象,在AsyncTask的构造函数中传递该适配器对象,并在ListActivity
中使用对象。
答案 1 :(得分:0)
adapter =new CustomArrayAdapter(obj, names, pid);
adapter.notifyDataSetChanged();
notifyDataSetChange通知观察者更新数据。
因此,在您的示例中,您通知新适配器更新其数据,但您没有将适配器附加到列表中。
但是每次新的适配器创建 - 不是一个好习惯,你必须更新适配器中的数据(在你的案例总统列表中),然后在你现有的适配器上调用notifyDataSetChange。
PS ArrayAdapter下方已有一个列表,您可以致电add而不需要存储总统名单。在你的情况下,我认为创建
更好 class PresidentsWrapper {
String name,
String photoUrl
}
create ArrayAdapter<PresidentWrapper>
在这种情况下,您不需要任何底层结构,可以这样做:
getItem(position);
add(new PresidentWrapper(...));