我使用 ImageView 和 TextView 制作了一个 ListView 。 当我点击图片然后使用意图时,我将图片uri 和文字传递给下一个活动。现在首先listView的四个单元格我正在获取下一个活动的正确数据。但是当我滚动listView 然后在listview的某个单元格上单击然后错误的数据将被发送。如果有人知道解决方案,那么请发布它... 提前致谢
这是我的适配器类代码..
public class Adapter extends BaseAdapter {
private static final String TAG = "Adapter";
private Activity mActivity;
public ArrayList<Data> mObjects;
// /private final Context context;
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;
if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
ViewHolder 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.title.setText(item.getmTitle());
holder.name.setText(item.getmConcatinate());
holder.review.setText(item.getmreview());
holder.icon.setBackgroundResource(R.drawable.ic_ab);
// holder.photo.setBackgroundResource(0);
holder.mTask = new DownloadImageTask(item.getmImageUrl(), holder.icon);
if (!holder.mTask.isCancelled()) {
holder.mTask.execute();
}
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");
String title = item.getmConcatinate();
String picture=item.getmImageUrl();
Intent intent = new Intent(v.getContext(), LargeView.class);
Log.d("Parsing JSON Data", "Before user try444444");
intent.putExtra("data", title);
Log.d("Parsing JSON Data", "Before user try555555");
intent.putExtra("image3",picture);
Log.d("Parsing JSON Data", "Before user try66666666");
v.getContext().startActivity(intent);
Log.d("Parsing JSON Data", "Before user try7777");
}
});
// holder.mTask1 = new DownloadImageTask1(item.getmImageUrl1(),
// holder.photo);
// if (!holder.mTask1.isCancelled()) {
// holder.mTask1.execute();
// }
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;
// DownloadImageTask1 imagetask1 = viewHolder.mTask1;
if (imagetask != null) {
imagetask.cancel(true);
}
// if (imagetask1 != null) {
// // imagetask1.cancel(true);
// }
}
};
}
下一个活动的代码......
public class LargeView extends Activity {
//ImageView image4;
//Bitmap bm1;
//String image_url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_large_view);
TextView text=(TextView)findViewById(R.id.textView1);
ImageView image4=(ImageView)findViewById(R.id.largeimage);
//Button button=(Button)findViewById(R.id.back99);
Intent intent2 = getIntent();
Log.d("Parsing JSON Data", "Before user try12121212112");
String easyPuzzle = intent2.getExtras().getString("data");
String image_url= intent2.getExtras().getString("image3");
text.setText(easyPuzzle);
BitmapFactory.Options bmOptions1;
bmOptions1 = new BitmapFactory.Options();
bmOptions1.inSampleSize = 1;
LargeImage large=new LargeImage();
Bitmap bm = large.LoadImage(image_url, bmOptions1);
image4.setImageBitmap(bm);
Log.d("Parsing JSON Data", image_url);
}
}
答案 0 :(得分:2)
在这种方法中:
@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");
String title = item.getmConcatinate();
String picture=item.getmImageUrl();
Intent intent = new Intent(v.getContext(), LargeView.class);
Log.d("Parsing JSON Data", "Before user try444444");
intent.putExtra("data", title);
Log.d("Parsing JSON Data", "Before user try555555");
intent.putExtra("image3",picture);
Log.d("Parsing JSON Data", "Before user try66666666");
v.getContext().startActivity(intent);
Log.d("Parsing JSON Data", "Before user try7777");
}
项目将是您设置的任何内容。
您需要将ViewHolder存储在其他地方,并使用视图标记来存储该项目。然后你可以在这个例程中使用item = v.getTag。
或者,您可以对视图进行子类化并让它存储标记以便在onclick方法中使用。
答案 1 :(得分:1)
//我修改了getView()它会产生欲望结果 //将位置设置为视图的标记并在onclick中检索数据
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Data item = mObjects.get(position);
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
ViewHolder 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.title.setText(item.getmTitle());
holder.name.setText(item.getmConcatinate());
holder.review.setText(item.getmreview());
holder.icon.setBackgroundResource(R.drawable.ic_ab);
// holder.photo.setBackgroundResource(0);
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);
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);
Log.d("Parsing JSON Data", "Before user try555555");
intent.putExtra("image3",picture);
Log.d("Parsing JSON Data", "Before user try66666666");
v.getContext().startActivity(intent);
Log.d("Parsing JSON Data", "Before user try7777");
}
});
// holder.mTask1 = new DownloadImageTask1(item.getmImageUrl1(),
// holder.photo);
// if (!holder.mTask1.isCancelled()) {
// holder.mTask1.execute();
// }
return rowView;
}
答案 2 :(得分:0)
您的问题是将onClickListener定义为匿名类。您在课堂上使用的项目可以是任何内容。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Data item = mObjects.get(position);
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) rowView.findViewById(R.id.image);
viewHolder.icon.setTag(position);
// 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.title.setText(item.getmTitle());
holder.name.setText(item.getmConcatinate());
holder.review.setText(item.getmreview());
holder.icon.setBackgroundResource(R.drawable.ic_ab);
// holder.photo.setBackgroundResource(0);
holder.mTask = new DownloadImageTask(item.getmImageUrl(), holder.icon);
if (!holder.mTask.isCancelled()) {
holder.mTask.execute();
}
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 pos = Integer.parseInt(v.getTag());
Data itm = mObjects.get(pos);
String title = itm.getmConcatinate();
String picture=itm.getmImageUrl();
Intent intent = new Intent(v.getContext(), LargeView.class);
Log.d("Parsing JSON Data", "Before user try444444");
intent.putExtra("data", title);
Log.d("Parsing JSON Data", "Before user try555555");
intent.putExtra("image3",picture);
Log.d("Parsing JSON Data", "Before user try66666666");
v.getContext().startActivity(intent);
Log.d("Parsing JSON Data", "Before user try7777");
}
});