我在listview中遇到了一个大问题。 问题是,当我点击按钮时,我会得到所有优惠详情。我在listview中展示了这个细节。 Listview包含从服务器加载图像的图像。它正确加载数据, 但问题是当我滚动我的列表视图时,图像会随着我滚动列表而改变。
这是我的代码
public class Offer_adapter extends BaseAdapter {
private Activity activity;
String[] offer_title;
String[] offerimg1;
String[] mrp;
String[] offerprice;
String[] you_save;
String[] imgURLArray;
Bitmap bitmap;
ImageView offerimg;
int a;
private static LayoutInflater inflater=null;
public Offer_adapter(Activity a, String[] offer_title, String[] offerimg1, String[] mrp, String[] you_save, String[] offerprice,String[] imgURLArray) {
activity = a;
this.offer_title = offer_title;
this.offerimg1 = offerimg1;
this.mrp = mrp;
this.offerprice = offerprice;
this.you_save = you_save;
this.imgURLArray = imgURLArray;
inflater = (LayoutInflater) activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mrp.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView offertitle;
public TextView offermrp;
public TextView offersave;
public TextView ofrprice;
public ImageView offerimg;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
//imgURLArray[position] = "http://www.surun.co/preost/mod_offer/images/" + offerimg1[position];
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.offer_list, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.offertitle = (TextView) vi.findViewById(R.id.ofrtitle);
holder.offermrp = (TextView) vi.findViewById(R.id.offeroriginal);
offerimg = (ImageView) vi.findViewById(R.id.ofr_img);
holder.offersave = (TextView) vi.findViewById(R.id.saveoffer);
holder.ofrprice = (TextView) vi.findViewById(R.id.priceoffer);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
holder.offertitle.setText(offer_title[position]);
holder.offermrp.setPaintFlags(holder.offermrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.offermrp.setText("Original Price : \u20B9" + mrp[position]);
holder.offersave.setText("You Save : \u20B9"+you_save[position]);
holder.ofrprice.setText("Offer Price : \u20B9"+offerprice[position]);
// ImageView image = holder.offerimg;
//if (holder.offerimg != null) {
// new ImageDownloaderTask(image).execute(imgURLArray[position]);
// }
Glide.with(activity).load(imgURLArray[position]).placeholder(activity.getResources().getDrawable(R.drawable.loadingoffer)).error(activity.getResources().getDrawable(R.drawable.loadingoffer)).into(offerimg);
return vi;
}
我从服务器获得的数据是
private class LoadImage extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.v(TAG, "In onPreExecute of the loading offers page.");
pDialog = new ProgressDialog(UserLogedIn.this);
pDialog.setMessage("Loading....");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.mp3));
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("userid", u_id));
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(URLOFFER, ServiceHandler.POST, param);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null)
{
try {
contacts1 = new JSONArray(jsonStr);
lengthofaffer = contacts1.length();
Log.v(TAG, ".................." + lengthofaffer);
if (lengthofaffer > 0) {
offer_title = new String[contacts1.length()];
imgURLArray=new String[contacts1.length()];
offer_expiry = new String[contacts1.length()];
available_city = new String[contacts1.length()];
mrp = new String[contacts1.length()];
offerprice = new String[contacts1.length()];
you_save = new String[contacts1.length()];
product_detail = new String[contacts1.length()];
key_feature = new String[contacts1.length()];
cod = new String[contacts1.length()];
emi = new String[contacts1.length()];
moneyback = new String[contacts1.length()];
delivery = new String[contacts1.length()];
terms = new String[contacts1.length()];
offerimg1 = new String[contacts1.length()];
offerimg2 = new String[contacts1.length()];
offerimg3 = new String[contacts1.length()];
offerimg4 = new String[contacts1.length()];
offerimg5 = new String[contacts1.length()];
date = new String[contacts1.length()];
for (int i = (lengthofaffer - 1), j = 0; i >= 0 && j < contacts1.length(); i--, j++)
{// for (int i = 0; i < contacts1.length(); i++) {
JSONObject c = contacts1.getJSONObject(i);
Log.v(TAG, "" + i);
String offer_title1 = c.getString(TAG_offer_title);
String offer_expiry1 = c.getString(TAG_offer_expiry);
String available_city1 = c.getString(TAG_available_city);
String mrp1 = c.getString(TAG_mrp);
String offerprice1 = c.getString(TAG_offerprice);
String you_save1 = c.getString(TAG_you_save);
String product_detail1 = c.getString(TAG_product_detail);
String key_feature1 = c.getString(TAG_key_feature);
String cod1 = c.getString(TAG_cod);
String emi1 = c.getString(TAG_emi);
String moneyback1 = c.getString(TAG_moneyback);
String delivery1 = c.getString(TAG_delivery);
String terms1 = c.getString(TAG_terms);
String offerimg11 = c.getString(TAG_offerimg1);
String offerimg21 = c.getString(TAG_offerimg2);
String offerimg31 = c.getString(TAG_offerimg3);
String offerimg41 = c.getString(TAG_offerimg4);
String offerimg51 = c.getString(TAG_offerimg5);
String date1 = c.getString(TAG_date);
offer_title[i] = offer_title1;
offer_expiry[i] = offer_expiry1;
available_city[i] = available_city1;
mrp[i] = mrp1;
offerprice[i] = offerprice1;
you_save[i] = you_save1;
product_detail[i] = product_detail1;
key_feature[i] = key_feature1;
cod[i] = cod1;
emi[i] = emi1;
moneyback[i] = moneyback1;
delivery[i] = delivery1;
terms[i] = terms1;
offerimg1[i] = offerimg11;
offerimg2[i] = offerimg21;
offerimg3[i] = offerimg31;
offerimg4[i] = offerimg41;
offerimg5[i] = offerimg51;
date[i] = date1;
imgURLArray[i] = "http://www.surun.co/preost/mod_offer/images/" + offerimg1[i];
Log.v(TAG,""+imgURLArray[i]);
}
}
} catch (JSONException e) {
System.out.print("hiiiiiiiiiiii");
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.v(TAG, "length of aaaaa" + lengthofaffer);
if ( offer_title!= null && offer_title.length > 0) {
Log.v(TAG,"abhijit");
Offer_adapter adapter = new Offer_adapter(UserLogedIn.this, offer_title,offerimg1,mrp,you_save,offerprice,imgURLArray);
offerlist.setAdapter(adapter);
//offerlist.getSelectedView().setBackgroundColor();
offerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent ii = new Intent(UserLogedIn.this, Single_offer.class);
ii.putExtra("offer_title", offer_title[i]);
ii.putExtra("offer_expiry", offer_expiry[i]);
ii.putExtra("available_city", available_city[i]);
ii.putExtra("mrp", mrp[i]);
ii.putExtra("cod", cod[i]);
ii.putExtra("emi", emi[i]);
ii.putExtra("offer_price", offerprice[i]);
ii.putExtra("you_save", you_save[i]);
ii.putExtra("product_detail", product_detail[i]);
ii.putExtra("key_feature", key_feature[i]);
ii.putExtra("moneyback", moneyback[i]);
ii.putExtra("delivery", delivery[i]);
ii.putExtra("terms", terms[i]);
ii.putExtra("date", date[i]);
ii.putExtra("offerimg1", offerimg1[i]);
ii.putExtra("offerimg2", offerimg2[i]);
ii.putExtra("offerimg3", offerimg3[i]);
ii.putExtra("offerimg4", offerimg4[i]);
ii.putExtra("offerimg5", offerimg5[i]);
ii.putExtra("userid",u_id);
startActivity(ii);
}
});
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(UserLogedIn.this);
builder.setTitle("Alert");
// builder.setIcon(R.drawable.unverified).show();
builder.setMessage("No Offer Present");
builder.setPositiveButton("Ok", null);
final AlertDialog alert = builder.create();
UserLogedIn.this.runOnUiThread(new java.lang.Runnable() {
public void run() {
//show AlertDialog
alert.show();
}
});
}
pDialog.dismiss();
pDialog.hide();
}//End of Inner class Load image
}
答案 0 :(得分:1)
基本上你的适配器可能没有配置。请尝试以下方法:
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response, String charset) {
try {
String json = "";
if (!response.notModified) {
json = new String(response.data, charset);
} else {
//if not modified -> strangely getCacheEntry().data always null
json = new String(getCacheEntry().data, charset);
}
return Response.success(
gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
有关详细信息,请参阅http://developer.android.com/guide/topics/ui/layout/listview.html