也许我对BindView和getView之间的区别感到困惑,但它们都会导致同样的问题。我在每个listview项目上都有一个按钮,当按下它时,它应该在按下时显示在它下面的ImageView。
我一直试图解决这个问题差不多一个星期了,而且我学到的是它与滚动时的回收视图有关,并且getView应该可以解决这个问题。然而它更糟糕的是,它不仅在它下面的项目上显示图像,而且还在我按下的图像上显示错误的图像。
我完全迷失在这里。 =(
这是我的光标适配器:
public class ItemAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
ListView listView;
Bitmap myBitmap;
ImageView myImage;
Cursor cursor;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.item_layout, parent, false);
return v;
}
@Override
public void bindView(final View v, Context context, Cursor c) {
final DBAdapter dbAdapter = new DBAdapter(mContext);
listView = (ListView) v.findViewById(R.id.listViewFromDB);
final String description = c.getString(c.getColumnIndexOrThrow(DBAdapter.DBHelper.DESCRIPTION));
final String detail = c.getString(c.getColumnIndexOrThrow(DBAdapter.DBHelper.DETAIL));
final String importance = c.getString(c.getColumnIndexOrThrow(DBAdapter.DBHelper.IMPORTANCE));
final String id = c.getString((c.getColumnIndexOrThrow(DBAdapter.DBHelper.UID)));
final ImageButton trash = (ImageButton) v.findViewById(R.id.trash);
final ImageButton edit = (ImageButton) v.findViewById(R.id.editButton);
ImageButton expandButton = (ImageButton) v.findViewById(R.id.expand);
final ImageView drawnImage = (ImageView) v.findViewById(R.id.drawnImage);
final TextView idFlag = (TextView) v.findViewById(R.id.idFlag);
final File imgFile = new File("/data/data/com.eboodnero.memoir/files/" + id + ".png");
expandButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (trash.getVisibility() == View.INVISIBLE && id == idFlag.getText().toString()) {
trash.setVisibility(View.VISIBLE);
edit.setVisibility(View.VISIBLE);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
myImage = (ImageView) v.findViewById(R.id.drawnImage);
drawnImage.setVisibility(View.VISIBLE);
myImage.setImageBitmap(myBitmap);
myImage.setMaxWidth(50);
myImage.setMaxHeight(50);
}
} else if (trash.getVisibility() == View.VISIBLE) {
drawnImage.setVisibility(View.GONE);
trash.setVisibility(View.INVISIBLE);
edit.setVisibility(View.INVISIBLE);
}
}
});
TextView descriptionText = (TextView) v.findViewById(R.id.description);
Typeface helveticaLight = Typeface.createFromAsset(mContext.getAssets(), "fonts/HelveticaNeue-Light.ttf");
descriptionText.setTypeface(helveticaLight);
if (descriptionText != null) {
descriptionText.setText(description);
}
TextView detailText = (TextView) v.findViewById(R.id.detail);
detailText.setTypeface(helveticaLight);
detailText.setTextColor(Color.parseColor("#a5a5a5"));
if (detailText != null) {
detailText.setText(detail);
}
final TextView idFlagText = (TextView) v.findViewById(R.id.idFlag);
if (idFlagText != null) {
idFlagText.setText(id);
}
trash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder deleteDialog = new AlertDialog.Builder(mContext);
deleteDialog.setTitle("Delete Memoir");
deleteDialog.setMessage("Are you sure you want to delete this Memoir?");
deleteDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
drawnImage.setVisibility(View.GONE);
trash.setVisibility(View.INVISIBLE);
edit.setVisibility(View.INVISIBLE);
dbAdapter.remove(Long.parseLong(id));
notifyDataSetChanged();
cursor = dbAdapter.getAllRows();
changeCursor(cursor);
}
});
deleteDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
deleteDialog.show();
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, AddItem.class);
intent.putExtra("Description", description);
intent.putExtra("Detail", detail);
intent.putExtra("ImportanceLevel", importance);
intent.putExtra("Id", id);
mContext.startActivity(intent);
}
});
}
}