求助:一个加载顺序的问题
程序员文章站
2022-06-05 08:51:45
...
我在做一个APP的用户列表绑定数据,我用测试数据进行绑定的时候是没有问题的,我一换上接口数据就会出现一个问题,就是他总是先进行了绑定数据的操作再去做获取数据的操作,就是说我第一次进入页面,绑定的数据为空,但是集合中获取到的是第一页的数据,然后上拉加载第二页的时候就绑定了第一页的数据,集合中获取到了两页的数据,Activity代码如下:
public class MypersonActivity extends BaseActivity{
private ListView listView;
private PtrClassicFrameLayout ptrClassicFrameLayout;
private PersonAdapter personAdapter;
private List<PersonBean> valueList = new ArrayList<>();
private int pages = 0;
private boolean isAll;
@Override
protected int setLayout() {
return R.layout.activity_myperson;
}
@Override
protected void initView() {
enableTitleDelegate();
getTitleDelegate().setTitle("我的用户");
listView = getViewById(R.id.person_listview);
//下拉框架
ptrClassicFrameLayout = getViewById(R.id.person_ptrclassicframelayout);
ptrClassicFrameLayout.setLastUpdateTimeRelateObject(this);
ptrClassicFrameLayout.setResistance(1.7f);
ptrClassicFrameLayout.setRatioOfHeaderHeightToRefresh(1.2f);
ptrClassicFrameLayout.setDurationToClose(200);
ptrClassicFrameLayout.setDurationToCloseHeader(1000);
ptrClassicFrameLayout.setPullToRefresh(false);
ptrClassicFrameLayout.setKeepHeaderWhenRefresh(true);
ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler2() {
@Override
public void onLoadMoreBegin(PtrFrameLayout frame) {
pages++;
if (!isAll) {
requestData();
} else {
ptrClassicFrameLayout.refreshComplete();
}
}
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
isAll = false;
valueList.clear();
pages = 1;
requestData();
}
});
personAdapter = new PersonAdapter(mContext, valueList);
requestData();
}
private void requestData() {
//接口数据
String token= PreferencesHelper.find("token","");
RequestUtils.getPersons(token, pages, 4, new HttpCallBack<String>(mContext) {
@Override
protected void onSuccess(Call call, Response response, String s) {
try {
JSONObject jsonObject= new JSONObject(s);
JSONArray jsonArray= (JSONArray) jsonObject.get("data");
for(int i=0;i<jsonArray.length();i++){
JSONObject json= (JSONObject) jsonArray.get(i);
PersonBean personBean=new PersonBean();
personBean.setImg(R.mipmap.icon_person);
personBean.setName((String) json.get("login_name"));
valueList.add(personBean);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onEror(Call call, int statusCode, String message) {
}
});
// 测试数据
// PersonBean personBean1 = new PersonBean();
// personBean1.setImg(R.mipmap.icon_person);
// personBean1.setName("张三");
//
// PersonBean personBean2 = new PersonBean();
// personBean2.setImg(R.mipmap.icon_person);
// personBean2.setName("李四");
//
// PersonBean personBean3 = new PersonBean();
// personBean3.setImg(R.mipmap.icon_person);
// personBean3.setName("王五");
//
// valueList.add(personBean1);
// valueList.add(personBean2);
// valueList.add(personBean3);
personAdapter.setValueList(valueList);
listView.setAdapter(personAdapter);
ptrClassicFrameLayout.refreshComplete();
}
@Override
protected void windowsDidloaded() {
}
PersonAdapter适配器代码如下:public class PersonAdapter extends BaseAdapter {
private List<PersonBean> valueList;
private Context context;
private LayoutInflater layoutInflater;
public PersonAdapter(Context context, List<PersonBean> valueList) {
this.valueList = valueList;
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public List<PersonBean> getValueList() {
return valueList;
}
public void setValueList(List<PersonBean> valueList) {
this.valueList = valueList;
}
@Override
public int getCount() {
return valueList.size();
}
@Override
public Object getItem(int i) {
return valueList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.adapter_persons, parent, false);
holder = new ViewHolder();
holder.personName = (TextView) convertView.findViewById(R.id.person_tv);
holder.personImg = (ImageView) convertView.findViewById(R.id.person_img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final PersonBean item = valueList.get(position);
holder.personImg.setImageResource(item.getImg());
holder.personName.setText(item.getName());
return convertView;
}
static class ViewHolder {
private TextView personName;
private ImageView personImg;
}
我认为可能是异步加载的原因导致方法的执行顺序有问题,下面是异步加载的代码://去进行网络 异步 请求
public void doRequest(Request request, boolean isShowAlert, String alertTitle, final BaseCallBack callBack) {
if (isShowAlert) {
callBack.OnRequestBefore(request, alertTitle);
}
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBackFailure(callBack, call, e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callBack.onResponse(response);
String result = response.body().string();
if (CheckUtils.isAvailable(result)) {
LogUtil.e(result);
}
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(result);
if (jsonObject.getInt("code") != 0) {
callBackError(callBack, call, response.code(), jsonObject.getString("message"));
return;
}
if (callBack.mType == String.class) {
// callBack.onSuccess(call, response, result);
callBackSuccess(callBack, call, response, result);
} else {
String data = jsonObject.getString("data");
Object object = JSON.parseObject(data, callBack.mType);//自动转化为 泛型对象
// callBack.onSuccess(call, response, object);
callBackSuccess(callBack, call, response, object);
}
} catch (JSONException e) {
e.printStackTrace();
callBackError(callBack, call, response.code(), "数据转换异常");
}
} else{
callBackError(callBack, call, response.code(), response.message());
}
}
});
}
求各位大佬帮帮忙,感谢!!!