AsyncTask:
class mydata extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("Fetching");
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.setIndeterminate(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
Response = JSONfunctions.getJSONfromURL(Constantt.PendingOrderCustomerWiseORDERURL + "18");
rowItems = new ArrayList<PendingOrderDataModel>();
try {
dataModel = new PendingOrderDataModel();
_jsonarray = new JSONArray(Response);
for (int i = 0; i < _jsonarray.length(); i++) {
_jsobject = _jsonarray.getJSONObject(i);
dataModel.setCreatedOn(_jsobject.getString("CreatedOn"));
dataModel.setDeliveryAddress(_jsobject.getString("DeliveryAddress"));
dataModel.setDeliveryTime(_jsobject.getString("DeliveryTime"));
dataModel.setId(_jsobject.getString("Id"));
dataModel.setStatus(_jsobject.getString("Status"));
dataModel.setTotal(_jsobject.getString("Total"));
dataModel.setUserDetailId(_jsobject.getString("UserDetailId"));
rowItems.add(dataModel);
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d("Response", Response);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
PendingOrderAdapter adapter = new PendingOrderAdapter(getActivity(),R.layout.pendingorderlist_item ,rowItems);
listView.setAdapter(adapter);
pDialog.dismiss();
}
}
适配器类:
public class PendingOrderAdapter extends ArrayAdapter<PendingOrderDataModel> {
Context context;
List<PendingOrderDataModel> rowItems;
public PendingOrderAdapter(Context context, int resourceId,List<PendingOrderDataModel> items)
{
super(context, resourceId, items);
this.context = context;
this.rowItems = items;
}
private class ViewHolder {
TextView orderridtext;
TextView orderdatetext;
TextView orderamounttext;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.pendingorderlist_item, null);
holder = new ViewHolder();
holder.orderridtext = (TextView) convertView.findViewById(R.id.order_item_order_id);
holder.orderdatetext = (TextView) convertView.findViewById(R.id.order_item_order_date);
holder.orderamounttext = (TextView) convertView.findViewById(R.id.order_item_total_amount);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
PendingOrderDataModel rowItem = (PendingOrderDataModel) getItem(position);
holder.orderridtext.setText("BHM" +rowItem.getId());
holder.orderdatetext.setText(rowItem.getDeliveryTime());
holder.orderamounttext.setText(rowItem.getTotal());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
当我运行此AsyncTask时,会显示相同的项目。在后台,当我调试时,它设置不同的数据,但在适配器类中,我们能够获得相同的数据。请告诉我我做错了什么。为什么在列表视图中打印相同的数据?
答案 0 :(得分:1)
从
修改doInBackground()
内部try
广告代码
dataModel = new PendingOrderDataModel();
_jsonarray = new JSONArray(Response);
for (int i = 0; i < _jsonarray.length(); i++) {
_jsobject = _jsonarray.getJSONObject(i);
dataModel.setCreatedOn(_jsobject.getString("CreatedOn"));
dataModel.setDeliveryAddress(_jsobject.getString("DeliveryAddress"));
dataModel.setDeliveryTime(_jsobject.getString("DeliveryTime"));
dataModel.setId(_jsobject.getString("Id"));
dataModel.setStatus(_jsobject.getString("Status"));
dataModel.setTotal(_jsobject.getString("Total"));
dataModel.setUserDetailId(_jsobject.getString("UserDetailId"));
rowItems.add(dataModel);
}
到
_jsonarray = new JSONArray(Response);
for (int i = 0; i < _jsonarray.length(); i++) {
dataModel = new PendingOrderDataModel();
_jsobject = _jsonarray.getJSONObject(i);
dataModel.setCreatedOn(_jsobject.getString("CreatedOn"));
dataModel.setDeliveryAddress(_jsobject.getString("DeliveryAddress"));
dataModel.setDeliveryTime(_jsobject.getString("DeliveryTime"));
dataModel.setId(_jsobject.getString("Id"));
dataModel.setStatus(_jsobject.getString("Status"));
dataModel.setTotal(_jsobject.getString("Total"));
dataModel.setUserDetailId(_jsobject.getString("UserDetailId"));
rowItems.add(dataModel);
}
答案 1 :(得分:0)
尝试在getview
中设置数据,如下所示:
holder.orderridtext.setText("BHM" +this.rowItems.get(position).getId());
holder.orderdatetext.setText(this.rowItems.get(position).getDeliveryTime());
holder.orderamounttext.setText(this.rowItems.get(position).getTotal());
或更改此内容:
PendingOrderDataModel rowItem = rowItems.get(position);
holder.orderridtext.setText("BHM" +rowItem.getId());
holder.orderdatetext.setText(rowItem.getDeliveryTime());
holder.orderamounttext.setText(rowItem.getTotal());