Android Listview项目按钮,触发使用自定义光标或适配器滚动的其他Listview项目
也许我对BindView和getView之间的区别感到困惑,但它们都会导致相同的问题。我在每个listview项目上都有一个按钮,当按下该按钮时,应该会在其下方显示一个ImageView 我已经花了将近一个星期的时间试图弄清楚这一点,我学到的是,它与在滚动时回收视图有关,而getView应该可以解决这一问题。然而,更糟糕的是,它不仅还在下方的项目上显示图像,而且在我按下的项目上也显示了错误的图像 我在这里完全迷路了=( 这是我的光标适配器:Android Listview项目按钮,触发使用自定义光标或适配器滚动的其他Listview项目,android,android-listview,android-cursoradapter,Android,Android Listview,Android Cursoradapter,也许我对BindView和getView之间的区别感到困惑,但它们都会导致相同的问题。我在每个listview项目上都有一个按钮,当按下该按钮时,应该会在其下方显示一个ImageView 我已经花了将近一个星期的时间试图弄清楚这一点,我学到的是,它与在滚动时回收视图有关,而getView应该可以解决这一问题。然而,更糟糕的是,它不仅还在下方的项目上显示图像,而且在我按下的项目上也显示了错误的图像 我在这里完全迷路了=( 这是我的光标适配器: public class ItemAdapter ex
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);
}
});
}
}