我正在开发一个应用程序,我需要在点击一行时发出服务器请求,所有工作正常但我的代码不会等到响应到来所以没有设置为图像视图,是否存在任何面临同样问题并找到解决方案的人。
我的适配器代码是:
public class DocumentAdapter extends RecyclerView.Adapter<DocumentAdapter.ViewHolder> implements Response.ErrorListener{
Context context;
private ArrayList<FetchDocument> docDatalist;
private int expandedPosition = -1;
int customerSerial;
private int vinappserial;
ViewHolder holderNew;
UtilityOfActivity utilityOfActivity;
List<ImageFetchResponseDocumentList> list = new ArrayList<>();
public DocumentAdapter(ArrayList<FetchDocument> docDatalist, Context context,String vinappserial) {
this.docDatalist = docDatalist;
this.context = context;
utilityOfActivity = new UtilityOfActivity((Activity) context);
this.vinappserial= Integer.valueOf(vinappserial);
UserDetail userDetail = new Gson().fromJson(PrefrenceFile.getInstance().getString(Constant.PREF_KEY_USER_DATA), UserDetail.class);
customerSerial = userDetail.getLoginResponse().getCustInfo().getCustSerialNo();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_document_listview_adapter, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.doc_type.setText(docDatalist.get(position).getDocTypeDesc());
if (position == expandedPosition) {
holder.myDocumentlistexpeded.setVisibility(View.VISIBLE);
} else {
holder.myDocumentlistexpeded.setVisibility(View.GONE);
}
holder.myDocumentListCollapse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//by the below line I am making the request
imagerequestserver(docDatalist.get(position), vinappserial);
//here I am setting the image to the imageview this code execute before response come
for (int i = 0; i < list.size(); i++) {
NetworkImageView imageView = new NetworkImageView(context);
imageView.setAdjustViewBounds(true);
imageView.setImageUrl(list.get(i).getNewFilePath().concat(list.get(i).getNewFilename()), VolleyRequestQue.getmInstance(context).getImageLoader());
holder.horizontal_scrollView_linear.addView(imageView);
}
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
// Set the current position to "expanded"
expandedPosition = holder.getPosition();
notifyItemChanged(expandedPosition);
holder.myDocumentlistexpeded.setVisibility(View.VISIBLE);
}
});
holder.layoutAddDoc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.expanded_menu_cameraImage.setVisibility(View.VISIBLE);
}
});
}
@Override
public int getItemCount() {
return docDatalist.size();
}
public interface SendHoldertdata{
void setdata(ViewHolder holder, int position);
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView doc_type;
LinearLayout myDocumentlistexpeded;
LinearLayout myDocumentListCollapse;
LinearLayout horizontal_scrollView_linear;
LinearLayout expanded_menu_cameraImage;
RelativeLayout layoutAddDoc;
public ViewHolder(View itemView) {
super(itemView);
doc_type = (TextView) itemView.findViewById(R.id.doc_type);
myDocumentlistexpeded = (LinearLayout) itemView.findViewById(R.id.myDocumentlistexpeded);
myDocumentListCollapse = (LinearLayout) itemView.findViewById(R.id.myDocumentListCollapse);
horizontal_scrollView_linear = (LinearLayout) itemView.findViewById(R.id.horizontal_scrollView_linear);
expanded_menu_cameraImage = (LinearLayout) itemView.findViewById(R.id.expanded_menu_cameraImage);
layoutAddDoc = (RelativeLayout) itemView.findViewById(R.id.layoutAddDoc);
}
}
public void imagerequestserver(FetchDocument fetchDocument, int vinappserial) {
ImageFetchRequest imageFetchRequest = new ImageFetchRequest();
imageFetchRequest.setCustSerial(customerSerial);
imageFetchRequest.setDocGroup(fetchDocument.getDocTypeCode());
imageFetchRequest.setVehSerial(String.valueOf(vinappserial));
imageFetchRequest.setDocSerial("0");
String url = Constant.API_MAIN + Constant.API_DOCUMENT_IMAGEFETCH;
AbstractVolleyRequest jsonObjectrequest = new
AbstractVolleyRequest(url, this, new DocumentAdapter.DocumentImageFetchResponseListner(fetchDocument, vinappserial), imageFetchRequest, Request.Method.POST);
jsonObjectrequest.setTag("DocumentImageFetchRequest");
Log.v("DocumentFetchRequest", "DocumentImageFetchRequest" + jsonObjectrequest);
VolleyRequestQue.getmInstance(context.getApplicationContext()).addToRequestQueue(jsonObjectrequest);
}
@Override
public void onErrorResponse(VolleyError error) {
if (context != null) {
utilityOfActivity.progresDissmiss();
}
utilityOfActivity.toast(context.getString(R.string.connectivity_server_error));
}
public class DocumentImageFetchResponseListner extends MyListner<ImageFetchResponse> {
ImageFetchResponse response;
FetchDocument fetchDocument;
String vinappserial;
public DocumentImageFetchResponseListner(FetchDocument fetchDocument, int vinappserial) {
super(ImageFetchResponse.class);
this.vinappserial = String.valueOf(vinappserial);
this.fetchDocument = fetchDocument;
// this.moveFromDocList = moveFromDocList;
utilityOfActivity.progressDialogShow(context.getString(R.string.please_wait));
}
@Override
public void onResponse(MyResponse instaResponse) {
utilityOfActivity.progresDissmiss();
if (instaResponse != null) {
if (instaResponse instanceof ImageFetchResponse) {
response = (ImageFetchResponse) instaResponse;
if (response.getResult().getResultStatus().contains("SUCCESS")) {
list = response.getDocumentList();
Log.d("","");
if (list.size() > 0) {
} else {
// imageDelete.setVisibility(View.GONE);
// no_media.setVisibility(View.VISIBLE);
}
/*else {
if (moveFromDocList)
utilityOfActivity.moveFragment(FragmentAddDocument.newInstance(fetchDocument, vinappserial), Constant.TAG_GLOBAL_2, R.id.container);
}*/
} else {
utilityOfActivity.toast(context.getString(R.string.connectivity_server_error));
}
}
}
}
}
}
如果代码中有任何澄清,请问我:
答案 0 :(得分:0)
if (response.getResult().getResultStatus().contains("SUCCESS")) {
List<ImageFetchResponseDocumentList> newList = response.getDocumentList();
list.addAll(newList);
Log.d("","");
....
}
检查此代码。并且不要将请求api放到适配器上。 read the link