notifyDataSetChanged() 从 DialogFragment 到片段中的 GridView 适配器

notifyDataSetChanged() from DialogFragment to GridView adapter in a fragment

我有一个 Main activity,它包含 NavigationDrawerFragment(取自 android studio 模板) GridViewAdapter 有 OnLongClickListener,它通过 Main:

中的回调运行 SettingsFragment

主要内容:

  public void ShowSettings(Drink drink, int position) {
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        // Create and show the dialog.
        DialogFragment settings = FragmentSettings.newInstance(drink, position);

        settings.show(ft, "FragmentSettings");

网格视图适配器:

public class GridViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private ArrayList<Drink> listDrink = new ArrayList<Drink>();
    private Activity activity;
    private Context mContext;
    private GridViewAdapterCallback callback;

    public GridViewAdapter(Activity activity, ArrayList<Drink> listDrink) {
        super();
        this.listDrink = listDrink;
        this.activity = activity;
    }

    public GridViewAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listDrink.size();
    }

    @Override
    public Drink getItem(int position) {
        // TODO Auto-generated method stub
        return listDrink.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }




    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.grid_item, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvName);

            view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);

            view.txtViewCount = (TextView) convertView.findViewById(R.id.tvCount);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listDrink.get(position).getProductName());
        switch (listDrink.get(position).getType()) {
            case (1):
                view.imgViewFlag.setImageResource(R.drawable.btn_beer);
                break;
            case (2):
                view.imgViewFlag.setImageResource(R.drawable.btn_dark);
                break;
            default:
                view.imgViewFlag.setImageResource(R.drawable.btn_shot);
                break;


        }

        int count = callback.SqliteCountProducts(listDrink.get(position).getProductName());
        view.txtViewCount.setText(String.valueOf(count));
//        notifyDataSetChanged();

        return convertView;
    }

    public void setCallback(GridViewAdapterCallback callback) {

        this.callback = callback;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Log.d("My_Log", "On_click:" + position);
        callback.SqliteSave(listDrink.get(position));
        notifyDataSetInvalidated();


    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//        Log.d("My_Log", "On_long_click:" + position);
        callback.ShowSettings(listDrink.get(position), position);
        notifyDataSetInvalidated();
        return true;
    }

    public void UpdateGrid() {
        notifyDataSetInvalidated();

    }


    public interface GridViewAdapterCallback {

        public int SqliteCountProducts(String product);

        public void SqliteSave(Drink drink);

        public void ShowSettings(Drink drink, int position);
    }

    public static class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
        public TextView txtViewCount;
    }


}

GridFragment,包含 GridViewAdapter

public class GridFragment extends Fragment {
    //    ArrayList<Drink> listDrink = new ArrayList<Drink>();
    public GridViewAdapter mAdapter;

    public GridFragment() {
        // Required empty public constructor
    }

    public static GridFragment newInstance() {
        GridFragment fragment = new GridFragment();
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_grid, container, false);
        // prepared arraylist and passed it to the Adapter class
        mAdapter = new GridViewAdapter(getActivity(), ((Main) getActivity()).PrepareDefaultArray());
        GridView gridView = (GridView) view.findViewById(R.id.gvMain);

        mAdapter.setCallback((GridViewAdapter.GridViewAdapterCallback) view.getContext());
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(mAdapter);
        gridView.setOnItemLongClickListener(mAdapter);



        return view;
    }

    @Override
    public void onResume() {

        super.onResume();
        UpdateArray();


    }
    public void UpdateArray() {
        mAdapter.notifyDataSetChanged();

    }

onResume 有效,但仅在 GridView 项目滑到屏幕后方并返回后才有效

我不知道如何让它正常工作。 :/

内容提供商?

我无法对 main 进行另一个回调 - 因为它会导致循环

感谢您的帮助

为什么将 UpdateArray() 放在 onCreate() 中? mAdapter 尚未实例化。

@Override public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    UpdateArray();


}

所以在 gridView.setAdapter(mAdapter);

之后将 UpdateArray()onCreate() 移动到 onCreateView()


更新:

并评论 notifyDataSetInvalidated (),因为:

public void notifyDataSetInvalidated (): Notifies the attached observers that the underlying data is no longer valid or available. Once invoked this adapter is no longer valid and should not report further data set changes.