在Android中使用自定义适配器刷新ListView
我正在为在Android中使用自定义适配器刷新ListView,android,listview,baseadapter,Android,Listview,Baseadapter,我正在为列表视图使用自定义适配器。每个ListView项都包含ImageButton,该按钮将删除该项。现在,当有人点击ImageButton时,我想刷新完整的ListView。如何做到这一点? 还是要检查ImageButton单击ListView/McClickListener?我已经在自定义适配器中尝试了notifyDataSetChanged,但找不到任何更改。 自定义项目xml: <?xml version="1.0" encoding="utf-8"?> <Lin
列表视图使用自定义适配器。每个ListView
项都包含ImageButton
,该按钮将删除该项。现在,当有人点击ImageButton
时,我想刷新完整的ListView
。如何做到这一点?
还是要检查ImageButton
单击ListView/McClickListener
?我已经在自定义适配器中尝试了notifyDataSetChanged,但找不到任何更改。
自定义项目xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lightish"
android:orientation="vertical"
android:padding="10dp" >
<ImageButton
android:id="@+id/imgDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginBottom="-20dp"
android:background="@android:color/transparent"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_delete" />
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
public class MyAddressesAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
View vi;
HashMap<String, String> address;
public MyAddressesAdapter(Activity a,
ArrayList<HashMap<String, String>> arlData) {
activity = a;
data = arlData;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, final View convertView, ViewGroup parent) {
vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.my_address_view, parent, false);
final TextView tvName = (TextView) vi.findViewById(R.id.tvName);
address = new HashMap<String, String>();
address = data.get(position);
tvName.setText(address.get("PersonName"));
return vi;
}
myAddressesAdapter = new MyAddressesAdapter(
getActivity(), addressList);
lvMyAddresses.setAdapter(myAddressesAdapter);
将代码放入适配器的getview方法中
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//your code here
data.remove(position);
notifyDataSetChanged();
}
});
您可以在adaptertake image按钮的getview()内的imageButton上放置OnClickListener,然后设置notifyDataSetChanged()。单击按钮时显示删除项目的代码。OnClickListener在哪里,如何删除行?请分享相关代码!我将很快更新我的代码。发生了什么事?有什么问题吗?一些简短的说明会有帮助实际上,它确实在删除行,但它保持了行空间不变。@JeetenParmar首先使用视图保持器模式进行性能和垃圾收集。第二个检查项是否从hashmap中删除,b'z hashmap也允许空值,所以可能存在问题。我以前从未使用过视图持有者模式,但我发现了一些示例,所以让我更改代码。我很快会通知你的。我是来了解空白的。我在ScrollView中使用ListView,因此使用crack来指定高度。我如何防止这种情况?