notifyOnDataSetChanged

notifyOnDataSetChanged

标题基本概括了所有内容。我有一个由简单的适配器和一个xml布局填充的listview,该布局根据listview项中存在的行数而变化。当我向活动中的项目之一添加数据时,调用notifyondatasetchanged不会更新列表,但是如果我退出活动并返回所有内容,则看起来应该是这样。刷新适配器时,getview也需要更新,该怎么办?

    public AlarmListAdapter(Context context, ArrayList<HashMap<String, String>> list, int textViewResourceId, String[] fields, int[] textViewId) {
    super(context, list, textViewResourceId, fields, textViewId);
    this.list = list;
    this.context = context;
}

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

View v = convertView;
if (v == null) {
    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.alarm_list_item, null);
}


if(list.get(position).get("alert") == null){
    v.findViewById(R.id.ListViewItemSub).setVisibility(View.GONE);
}

return super.getView(position, convertView, parent);

}


public void forceReload(){
    notifyDataSetChanged();
}


从我的活动中的弹出窗口中调用notifyondatasetchanged。该按钮的代码如下所示:

        addCustomAlertButton = (Button)pView.findViewById(R.id.AddCustomAlertButton);
    addCustomAlertButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            arrayList.get((int) customAlertId).setAlertDialog(customAlertInput.getText().toString());
            Toast.makeText(EditAlarmsActivity.this, "Custom Alert Added", Toast.LENGTH_SHORT).show();
            updateListArray();
            pw.dismiss();
        }

最佳答案

您可以尝试setListAdapter(yourAdapter);

listview.requestLayout();

如果这些方法不起作用,那么您也可以尝试通过滚动刷新列表视图,因为一旦滚动列表视图,就会调用getView()函数。

07-26 07:25