【发布时间】:2014-09-18 13:31:15
【问题描述】:
我有一个 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());
}
}
【问题讨论】:
标签: android android-listview android-asynctask