listview没有使用notifyDataSetChanged()在android片段中刷新

时间:2014-06-18 17:38:22

标签: android-fragments android-listview refresh delete-row

在我的Android应用程序中,即使在Stack Overflow上尝试了这么多类似的解决方案后,我也无法解决这个问题。我用ArrayAdapter填充listview。长按一次列表视图项时,会出现一个对话框并要求确认。确认后,所选项目将从数组列表中删除,但在列表视图中,最后一项似乎已被删除。当您关闭片段并返回它时,您会看到正确的项目,但在关闭警报对话框后不会立即看到。

有人可以帮忙吗?谢谢!

适配器类

public class DuasListAdapter extends ArrayAdapter<String>{

// class variables
    private final Context context;// to save context
    private final List<String> duas;// to save list of stores
    LayoutInflater inflater;// so save layout inflater for the view

    public DuasListAdapter(Context ctx, List<String> duasList) {

        super(ctx, R.layout.adapter_list_duas, duasList);
        context = ctx;// save context
        duas = duasList;// save list of stores
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// save inflater layout
    }


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

        ViewHolder holder;


convertView=inflater.inflate(R.layout.adapter_list_duas, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
        holder.duaIndex = position;

if(SingletonClass.duasAra.get(holder.duaIndex)!=""){
            holder.tv_dua_arabic.setText(SingletonClass.duasAra.get(holder.duaIndex));
        }
        else{

                if(keyUrdSel && !keyEngSel)
                    holder.tv_dua_arabic.setText(SingletonClass.duasTitUrd.get(holder.duaIndex));
                else
                    holder.tv_dua_arabic.setText(SingletonClass.duasTitEng.get(holder.duaIndex));


            }
        holder.tv_dua_no.setText("  ("+ (holder.duaIndex+1));

return convertView;

}
static class ViewHolder {

         int duaIndex;
        LinearLayout background;// background to display color for read and unread messages
        ImageView iv_duatype;
        TextView tv_dua_arabic;// title of message
        TextView tv_dua_ref;// message by and message created on
        TextView tv_dua_no;

        /**
         * @description constructor to save context and list of stores views
         * @param v to refer the view
         * @return
         */
        public ViewHolder(View v) {
            // initialize their respective views
            background = (LinearLayout) v.findViewById(R.id.ll_duas_list);
            iv_duatype = (ImageView) v.findViewById(R.id.iv_duatype);
            tv_dua_arabic = (TextView) v.findViewById(R.id.tv_dua_arabic);
            tv_dua_no = (TextView) v.findViewById(R.id.dua_no);

        }
}

片段类

public class DuasListFragment extends Fragment  {
    Context context;

    LinearLayout ll_back_duas_list_header;
    TextView ll_back_duas_list_header_title;
    ListView lvDuaas;
public  DuasListAdapter duasAdapter;
int intListViewItemPosition = -1;
 ArrayList<String> duas;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = inflater.getContext();
        View view = inflater.inflate(R.layout.fragment_list_duas, container, false);

    // TODO Auto-generated method stub


        return view;
    }


@Override
    public void onActivityCreated(Bundle savedInstanceState) {


        ll_back_duas_list_header = (LinearLayout) getActivity().findViewById(R.id.ll_back_duas_list_header);
        ll_back_duas_list_header_title = (TextView) getActivity().findViewById(R.id.ll_back_duas_list_header_title);
        lvDuaas = (ListView) getActivity().findViewById(R.id.lv_duas);



        String verses = new SharedPreferencesSupplication().read(SingletonClass.keyListOfVerses, "a1");
        String[] versesList = verses.split(",");
        duas = new ArrayList<String>();
        for (int i = 0; i < versesList.length; i++) {
            if (versesList[i].length() > 0)
                duas.add(versesList[i]);
        }

        duasAdapter = new DuasListAdapter(context, duas);
        lvDuaas.setAdapter(duasAdapter);

    lvDuaas.setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(final AdapterView<?> parent,
                        View view, final int position, long id) {
                    // TODO Auto-generated method stub
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:




               duasAdapter = (DuasListAdapter) parent.getAdapter();
                    duas.remove(position);
                     duasAdapter.notifyDataSetChanged();



               ((MainActivity) context).removeItemFromList(position);

                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    // No button clicked
                                    break;
                            }
                        }
                    };


                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("Delete Dua from Favorites?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();


        return true;
                }
            }); 




        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        // duasAdapter.clear();
         //duasAdapter.addAll(duas);

            duasAdapter.notifyDataSetChanged();

    }

}

1 个答案:

答案 0 :(得分:1)

我也遇到了与notifydatasetchanged方法相同的问题。

我的问题是我附加到适配器的List正在更新,但notifydatasetchanged方法无效。

如果您的ListView设计为显示中等数量的项目,而不是调用notifydatasetchanged,您可以重新创建适配器。

它对我有用。我正在使用BaseAdapter。