当我向上和向下滚动时,我的列表视图正在更新,每次都会更新列表项中的图像。我怎样才能克服这个问题?
这是适配器类
public class CustomAdapter extends BaseAdapter {
ArrayList<String> p_id;
ArrayList<String> p_name;
Context context;
ArrayList<String> imageId;
ArrayList<String> wash_v;
ArrayList<String> dry_v;
ArrayList<String> iron_v;
Holder holder;
Typeface tf;
ProgressDialog dialog;
Intent i;
float washItemCount = 0.0f;
private static LayoutInflater inflater = null;
Handler handler;
Runnable myRunnable;
public CustomAdapter(Context con, ArrayList<String> pid,
ArrayList<String> pname, ArrayList<String> pimg,
ArrayList<String> wash, ArrayList<String> dry,
ArrayList<String> iron) {
p_id = pid;
context = con;
p_name = pname;
imageId = pimg;
wash_v = wash;
dry_v = dry;
iron_v = iron;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageId.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv1, tv2, tv3;
ImageView img;
LinearLayout ll1, ll2, ll3;
}
@SuppressLint("UseValueOf")
@Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
holder = new Holder();
final View rowView;
rowView = inflater.inflate(R.layout.dynamic, null);
holder.ll1 = (LinearLayout) rowView.findViewById(R.id.ll1);
holder.ll2 = (LinearLayout) rowView.findViewById(R.id.ll2);
holder.ll3 = (LinearLayout) rowView.findViewById(R.id.ll3);
holder.tv1 = (TextView) rowView.findViewById(R.id.tv1);
holder.tv2 = (TextView) rowView.findViewById(R.id.tv3);
holder.tv3 = (TextView) rowView.findViewById(R.id.tv5);
holder.img = (ImageView) rowView.findViewById(R.id.ivImage);
final TextView tvVal = (TextView) rowView.findViewById(R.id.tvVal);
final TextView tvVal1 = (TextView) rowView.findViewById(R.id.tvVal1);
final TextView tvVal2 = (TextView) rowView.findViewById(R.id.tvVal2);
holder.tv1.setText(wash_v.get(position));
holder.tv2.setText(dry_v.get(position));
holder.tv3.setText(iron_v.get(position));
holder.ll1.setTag(new Integer(position));
holder.ll2.setTag(new Integer(position));
holder.ll3.setTag(new Integer(position));
i = new Intent("com.example.safewash");
final String image = imageId.get(position).toString();
Log.i("Image in String", "" + image);
holder.ll1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(final View v) {
int count = 0;
int colorStart = Color.LTGRAY;
int colorEnd = 0;
ValueAnimator animator = ObjectAnimator.ofInt(v,"backgroundColor", colorStart, colorEnd);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(0);
animator.start();
String washItem = wash_v.get(position);
int washItems = Integer.parseInt(washItem);
if (washItems != 0) {
washItemCount = Float.parseFloat(washItem);
i.putExtra("washItemCount", washItemCount);
context.sendBroadcast(i);
tvVal.setVisibility(View.VISIBLE);
count = Integer.parseInt(tvVal.getText().toString());
int countVal = ++count;
tvVal.setText("" + countVal);
}
}
});
holder.ll2.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(final View v) {
int count = 0;
String washItem = dry_v.get(position);
int colorStart = Color.LTGRAY;
int colorEnd = 0;
ValueAnimator animator = ObjectAnimator.ofInt(v,
"backgroundColor", colorStart, colorEnd);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(0);
animator.start();
washItemCount = Float.parseFloat(washItem);
int washItems = Integer.parseInt(washItem);
if (washItems != 0) {
washItemCount = Float.parseFloat(washItem);
i.putExtra("washItemCount", washItemCount);
context.sendBroadcast(i);
tvVal1.setVisibility(View.VISIBLE);
count = Integer.parseInt(tvVal1.getText().toString());
int countVal = ++count;
tvVal1.setText("" + countVal);
}
}
});
holder.ll3.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(final View v) {
int count = 0;
int colorStart = Color.LTGRAY;
int colorEnd = 0;
ValueAnimator animator = ObjectAnimator.ofInt(v,
"backgroundColor", colorStart, colorEnd);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(0);
animator.start();
String washItem = iron_v.get(position);
washItemCount = Float.parseFloat(washItem);
int washItems = Integer.parseInt(washItem);
if (washItems != 0) {
washItemCount = Float.parseFloat(washItem);
i.putExtra("washItemCount", washItemCount);
context.sendBroadcast(i);
tvVal2.setVisibility(View.VISIBLE);
count = Integer.parseInt(tvVal2.getText().toString());
int countVal = ++count;
tvVal2.setText("" + countVal);
}
}
});
ImageDownloadTask imageDownloadTask = new ImageDownloadTask();
imageDownloadTask.execute(image);
return rowView;
}
public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
Log.i("BitMap in DoinBackGround::", "" + map);
}
return map;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(Bitmap result) {
BitmapDrawable ob = new BitmapDrawable(context.getResources(),
result);
holder.img.setBackgroundDrawable(ob);
super.onPostExecute(result);
}
}
public Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
private InputStream getHttpConnection(String urlString) throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
答案 0 :(得分:1)
错误的持有人模式
试试这段代码:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout...., null);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id....);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// set row code
}
你可以使用图片加载库。(快速和有用......)
如果你有imageurl,你可以替换为AUIL。 (而不是ImageDownloadTask)
Android-Universal-Image-Loader:https://github.com/nostra13/Android-Universal-Image-Loader
示例:
ImageLoader.getInstance().displayImage(imageId.get(position).toString(), holder.img, YOUOPTION);