我有适配器和内部适配器有asyntask。当我滚动值随机变化时,显示布局徽章无法快速加载。
我的适配器
private class MyAdapter extends ArrayAdapter<MyClass> {
ConvertView holder = null;
public MyAdapter(Context context, int resource, int textViewResourceId, List<MyClass> objects) {
super(context, resource, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return myclassList.size();
}
public MyClass getItem(int position) {
return myclassList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView txtTestKode, txtTestName, txtTestStatus;
final RelativeLayout layoutBadge;
final MyClass data = getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_test_transaction, null, false);
holder = new ConvertView(convertView);
convertView.setTag(holder);
} else {
holder = (ConvertView) convertView.getTag();
}
txtTestKode = holder.getTxtTestKode();
txtTestName = holder.getTxtTestName();
txtTestStatus = holder.getTxtTestStatus();
new AsyncTask<String, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
String url = "my_url";
WebRequestPost req = new WebRequestPost(url);
req.execute();
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getResultStream()));
String result = reader.readLine();
reader.close();
req.closeStream();
return result;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (SQLiteConstraintException e) {
e.printStackTrace();
} catch (SQLiteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String results) {
super.onPostExecute(results);
if (results != null) {
if (Integer.parseInt(results) > 0) {
holder.getLayoutBadge().setVisibility(View.VISIBLE);
}
}
}
}.execute();
return convertView;
}
}
和
public static class ConvertView {
private View row;
private RelativeLayout layoutBadge;
public ConvertView(View row) {
this.row = row;
}
public RelativeLayout getLayoutBadge() {
if (layoutBadge == null) {
layoutBadge = (RelativeLayout) row.findViewById(R.id.layoutBadge);
}
return layoutBadge;
}
}
由于
答案 0 :(得分:0)
你不应该在适配器中执行这些任务。正在发生的事情是数据被异步获取,并且可能在以后的任何时间可用(更改数据)。适配器继续加载其他行数据,并且不要等待每行完成任务,因为你正在异步执行。
最佳做法是在创建适配器之前提供数据,并将数据作为列表发送到适配器作为构造函数参数。