自定义列表视图不会替换视图
Custom list view is not replacing the view
我正在使用自定义 Adapter
在 listView
中显示图像和文本。单击按钮后,我想用下一个 image.But 替换图像,它刚刚被添加在列表的末尾而不是 replacing.Below 是我的代码片段:
从sqlite获取图片的方法
public void BlockData() {
dataBase = dbHelper.getReadableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "+ dbHelper.TABLE_NAME + " WHERE " + dbHelper.KEY_HNUM+ "=" + id, null);
if (mCursor.moveToFirst()) {
do {
category_Id.add(mCursor.getString(mCursor.getColumnIndex(dbHelper.KEY_HNUMM)));
} while (mCursor.moveToNext());
lv.setAdapter(new BlockAdapter(getApplicationContext(),category_Id,prgmImages));
stopManagingCursor(mCursor);
mCursor.close();
}
}
在自定义视图中设置
public View getView(final int pos, View child, final ViewGroup parent) {
final Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.secondlist, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.testingg);
mHolder.tx_img = (ImageView) child.findViewById(R.id.sac_img);
mHolder.butnxt = (Button) child.findViewById(R.id.button2);
mHolder.butpre = (Button) child.findViewById(R.id.button1);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(category_Id.get(pos));
if (pos == 0) {
imgs = getResources().obtainTypedArray(R.array.sac_imgs);
mHolder.tx_img.setImageResource(imgs.getResourceId(id - 1, -1));
imgs.recycle();
}else{
imgs = getResources().obtainTypedArray(R.array.sac_imgs);
mHolder.tx_img.setImageResource(imgs.getResourceId(pos - 1, -1));
imgs.recycle();
}
mHolder.butnxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id++;
BlockData();
}
});
return child;
}
protected ViewGroup findViewById(int txtViewPop) {
return null;
}
public class Holder {
TextView txt_id;
Button butnxt;
Button butpre;
TextView txt_fDate;
ImageView tx_img;
}
}
每次点击按钮时如何替换视图?希望有人能提前指导我direction.thanks
What should i do to replace the view every time a button is clicked?
不要在 ListView 中添加新的 Adapter 对象:
1.在BlockAdapter
中创建方法清除所有数据:
public void refeshAdapterData(List category_Id,<Data_Type> prgmImages )
{
category_Id.clear();
// clear prgmImages
// add data to adapter
category_Id.addAll(category_Id);
// Add prgmImages
notifyDataSetChanged();
}
2. 从 BlockData()
方法调用 refeshAdapterData
:
refeshAdapterData(category_Id,prgmImages);
stopManagingCursor(mCursor);
mCursor.close();
我正在使用自定义 Adapter
在 listView
中显示图像和文本。单击按钮后,我想用下一个 image.But 替换图像,它刚刚被添加在列表的末尾而不是 replacing.Below 是我的代码片段:
从sqlite获取图片的方法
public void BlockData() {
dataBase = dbHelper.getReadableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "+ dbHelper.TABLE_NAME + " WHERE " + dbHelper.KEY_HNUM+ "=" + id, null);
if (mCursor.moveToFirst()) {
do {
category_Id.add(mCursor.getString(mCursor.getColumnIndex(dbHelper.KEY_HNUMM)));
} while (mCursor.moveToNext());
lv.setAdapter(new BlockAdapter(getApplicationContext(),category_Id,prgmImages));
stopManagingCursor(mCursor);
mCursor.close();
}
}
在自定义视图中设置
public View getView(final int pos, View child, final ViewGroup parent) {
final Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.secondlist, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.testingg);
mHolder.tx_img = (ImageView) child.findViewById(R.id.sac_img);
mHolder.butnxt = (Button) child.findViewById(R.id.button2);
mHolder.butpre = (Button) child.findViewById(R.id.button1);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(category_Id.get(pos));
if (pos == 0) {
imgs = getResources().obtainTypedArray(R.array.sac_imgs);
mHolder.tx_img.setImageResource(imgs.getResourceId(id - 1, -1));
imgs.recycle();
}else{
imgs = getResources().obtainTypedArray(R.array.sac_imgs);
mHolder.tx_img.setImageResource(imgs.getResourceId(pos - 1, -1));
imgs.recycle();
}
mHolder.butnxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id++;
BlockData();
}
});
return child;
}
protected ViewGroup findViewById(int txtViewPop) {
return null;
}
public class Holder {
TextView txt_id;
Button butnxt;
Button butpre;
TextView txt_fDate;
ImageView tx_img;
}
}
每次点击按钮时如何替换视图?希望有人能提前指导我direction.thanks
What should i do to replace the view every time a button is clicked?
不要在 ListView 中添加新的 Adapter 对象:
1.在BlockAdapter
中创建方法清除所有数据:
public void refeshAdapterData(List category_Id,<Data_Type> prgmImages )
{
category_Id.clear();
// clear prgmImages
// add data to adapter
category_Id.addAll(category_Id);
// Add prgmImages
notifyDataSetChanged();
}
2. 从 BlockData()
方法调用 refeshAdapterData
:
refeshAdapterData(category_Id,prgmImages);
stopManagingCursor(mCursor);
mCursor.close();