- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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)或使用您在 ListActivity
的 onCreate()
中创建的相同对象,将该适配器对象传递到 AsyncTask 的构造函数中,并在 onPostExecute()
。
关于android - 在 Android 中的 AsyncTask 之后更新 CustomArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914136/
大家好,我实现了自己的 ArrayAdapter,因为我在单个 ListView 项中有两个 TextView 。虽然过滤工作正常(它显示正确的项目),但当我删除我输入的用于过滤项目的文本时,List
谁能告诉我为什么我在构造函数中的 super 方法上遇到空指针异常? 在 super 中总是遇到空指针异常,但 context 和 HistoryItems 不为 null。 public class
我正在尝试使用自定义数组适配器在 Fragment 中填充 ListView。但是我在自定义数组适配器的 getView() 方法中的方法 findViewById() 上得到了 NullPointe
我正在关注以下tutorial 。我正在学习如何使用/创建自定义适配器。在下面的代码中, super 构造函数让我有点困惑。 public class UsersAdapter extends Arr
我的listview的每个项目都有一个图像按钮,如果您单击该按钮,我想删除与当前图像按钮关联或当前聚焦的项目。 listview = (QuickReturnListView) v.findViewB
我想知道是否可以在 onListItemClick 中的 View 之间添加逻辑\区分?我有一个 listFragment,其中包含多个 View ,并且想要执行不同的操作。我尝试在我的 View 上
我有一个 CustomArrayAdapter,我用它来更新自定义 ListView。 这工作正常,但是当我尝试更新 View 时 在使用 notifyDataSetChanged 在 AsyncVi
我想实现 CustomArrayAdapter,下面是我为自定义适配器编写的构造函数 public CustomUsersAdapter(Context context, ArrayList user
我是一名优秀的程序员,十分优秀!