gpt4 book ai didi

android - 自定义 ListView 更新、删除在 Android 中不反射(reflect)?

转载 作者:行者123 更新时间:2023-11-29 21:31:40 27 4
gpt4 key购买 nike

我正在使用自定义适配器来显示带有 TextView 和 ImageView 的 ListView 。

enter image description here

这是我的输出屏幕。现在我想从 ListView 中删除一个项目,我通过使用下面的代码实现了这一点

 holder.img_delete.setTag(tempValues.getCompanyName());
holder.img_delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
eventDB.open();
eventDB.DeleteByText(view.getTag().toString());
eventDB.close();
Toast.makeText(ctx, "Deleted", 10).show();
notifyDataSetChanged();


}
});

现在的问题是 listView 在执行删除后没有更新项目。请给我建议。

@但丁

public class CustomAdapter extends BaseAdapter   implements OnClickListener {

private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
Context ctx;
EventCate_DBAdapter eventDB;

public CustomAdapter(Context c, ArrayList d,Resources resLocal) {



data=d;
res = resLocal;
ctx=c;

inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
eventDB = new EventCate_DBAdapter(ctx);
}


public int getCount() {

if(data.size()<=0)
return 1;
return data.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}


public static class ViewHolder{

public TextView text;

public static ImageView img_edit,img_delete;

}


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

View vi=convertView;
ViewHolder holder;

if(convertView==null){


vi = inflater.inflate(R.layout.tabitem, null);


holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.tv_event_cat);
holder.img_edit=(ImageView)vi.findViewById(R.id.iv_edit);
holder.img_delete=(ImageView)vi.findViewById(R.id.iv_delete);

vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();

if(data.size()<=0)
{
holder.text.setText("No Data");

}
else
{

tempValues=null;
tempValues = (ListModel) data.get(position);


holder.text.setText(tempValues.getCompanyName());



vi.setOnClickListener(new OnItemClickListener(position));



}

holder.img_edit.setTag(tempValues.getCompanyName());
holder.img_edit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
Toast.makeText(ctx, "Edit = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();

}
});
holder.img_delete.setTag(tempValues.getCompanyName());
holder.img_delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
eventDB.open();
eventDB.DeleteByText(view.getTag().toString());
eventDB.close();
Toast.makeText(ctx, "Deleted", 10).show();
notifyDataSetChanged();
notifyDataSetInvalidated();


}
});

return vi;
}

@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
}


private class OnItemClickListener implements OnClickListener{
private int mPosition;

OnItemClickListener(int position){
mPosition = position;
}

@Override
public void onClick(View arg0) {
SettingsActivity sct = (SettingsActivity)ctx;
sct.onItemClick(mPosition);
}
}

}

删除项目工作正常。我需要另一种更新列表项的解决方案。

这是我的代码

eventDB.open();

eventDB.update(Id.get(0), etEventCategory.getText().toString());

eventDB.close();
data.set(pos,etEventCategory.getText().toString());
dialog.dismiss();

notifyDataSetChanged();
notifyDataSetInvalidated();

最佳答案

在调用 notifyDataSetChanged() 之前,您还应该从 data 数组中删除对象

更改此代码

holder.img_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
eventDB.open();
eventDB.DeleteByText(view.getTag().toString());
data.remove(position); // change position to final
eventDB.close();
Toast.makeText(ctx, "Deleted", 10).show();
notifyDataSetChanged();
//notifyDataSetInvalidated();


}
});

关于android - 自定义 ListView 更新、删除在 Android 中不反射(reflect)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19312304/

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