gpt4 book ai didi

android - ListView 图像在滚动时发生变化

转载 作者:行者123 更新时间:2023-11-29 00:29:48 25 4
gpt4 key购买 nike

我正在 解析 json listview(imageview 和 textview) 中的数据。对于 ListView 的前 四个项目 image 和textview 已正确 添加,但是当我滚动 listview 时,imageview 中的图像正在发生变化。这是我的代码..

适配器.java

 public class Adapter extends BaseAdapter {

private static final String TAG = "Adapter";
private Activity mActivity;
public ArrayList<Data> mObjects;

Context context;

static class ViewHolder {
static ImageView icon;
TextView title;
TextView name;
TextView review;
DownloadImageTask mTask;
String ab[];
// DownloadImageTask1 mTask1;
// ImageView photo;
}

public Adapter(Activity activity, Context context, ArrayList<Data> mObjects) {

this.mActivity = (Activity) activity;
this.context = context;
this.mObjects = mObjects;

}

public void setObjects(ArrayList<Data> mObjects) {
this.mObjects = mObjects;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

final Data item = mObjects.get(position);
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
viewHolder = new ViewHolder();

viewHolder.icon = (ImageView) rowView.findViewById(R.id.image);
// viewHolder.photo = (ImageView) rowView.findViewById(R.id.photo);
viewHolder.title = (TextView) rowView.findViewById(R.id.title);
viewHolder.name = (TextView) rowView.findViewById(R.id.name);

viewHolder.review = (TextView) rowView.findViewById(R.id.status);
rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();

holder.icon.setTag(position);

holder.title.setText(item.getmTitle());
// holder.name.setTag(position);
holder.name.setText(item.getmConcatinate());

holder.review.setText(item.getmreview());
holder.icon.setBackgroundResource(R.drawable.ic_ab);
holder.mTask = new DownloadImageTask(item.getmImageUrl(),

holder.icon);
if (!holder.mTask.isCancelled()) {
holder.mTask.execute();
}

ViewHolder.icon.setTag(position);
// }// update position because list view recycle

ViewHolder.icon.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Log.d("Parsing JSON Data", "Before user try122222"); // perform
// Object tag= v.getTag();
Log.d("Parsing JSON Data", "abc");

int clickedPosition = (Integer) v.getTag();

Data clickedItem = mObjects.get(clickedPosition);
// Object vi=row.getTag();

String title = clickedItem.getmConcatinate();
String picture = clickedItem.getmImageUrl();

Intent intent = new Intent(v.getContext(), LargeView.class);
Log.d("Parsing JSON Data", "Before user try444444");
intent.putExtra("data", title);

intent.putExtra("image3", picture);

v.getContext().startActivity(intent);
Log.d("Parsing JSON Data", "Before user try7777");
}
});

return rowView;
}

@Override
public int getCount() {

return (this.mObjects.size());
}

@Override
public Object getItem(int position) {

return (this.mObjects.get(position));
}

@Override
public long getItemId(int position) {

return (position);
}

public AbsListView.RecyclerListener mRecyclerListener = new RecyclerListener() {

public void onMovedToScrapHeap(View view) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
DownloadImageTask imagetask = viewHolder.mTask;

if (imagetask != null) {
imagetask.cancel(true);
}

}

};

主.java

     @Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectAll().penaltyLog().build();
StrictMode.setThreadPolicy(policy);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile3);

name = (TextView) findViewById(R.id.textView1);
bmImage2 = (ImageView) findViewById(R.id.imageView1);
address = (TextView) findViewById(R.id.textView2);
gender = (TextView) findViewById(R.id.textView3);
loyalitypoints = (TextView) findViewById(R.id.textView7);
followers = (TextView) findViewById(R.id.textView8);
following = (TextView) findViewById(R.id.textView9);
// list13 = new ArrayList<HashMap<String, Object>>();
mListView = (ListView) findViewById(android.R.id.list);
mListView.setClickable(true);






// mListView=(ListView)findViewById(R.id.list);
mAdapter = new Adapter(this,c,mSource );
mListView.setAdapter(mAdapter);
Log.w("Parsing JSON Data", "Before Item click");


mListView.setRecyclerListener(mAdapter.mRecyclerListener);

最佳答案

首先,您已在 ViewHolder 中将 imageView 引用设置为静态。你不应该那样做,因为每个 getView 都会简单地覆盖它,你将始终让每个 View 的 ViewHolder 只引用一个 ImageView。第二,你应该始终记住 ListView 是回收 View ,所以如果你开始为一个 View 加载图像然后滚动它,那么在加载图像的那一刻,相同的 View 可能已经代表另一个 ListView 项目。总的来说,在 ListView 中有效地显示位图是一项相当复杂的任务,涉及很多变量。我建议你阅读 this并下载他们提供的代码示例。这是一个很好的起点,在简单的情况下,复制粘贴可能就足够了。

关于android - ListView 图像在滚动时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16540582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com