【问题标题】:Display a Snackbar in a fragment after deleting an item from the database?从数据库中删除项目后,在片段中显示 Snackbar?
【发布时间】:2016-06-07 03:46:59
【问题描述】:

当用户使用caseR.id.delete: 从数据库中删除一个项目时,我正在寻找一种在显示屏底部弹出 Snackbox 的方法。下面我附上了片段中的代码。如果您需要更多来自不同领域的代码,请告诉我。

/**
 * A simple {@link Fragment} subclass.
 */
public class MainActivityListFragment extends ListFragment {
    private ArrayList<Note> notes;
    private NoteAdapter noteAdapter;
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        /*
        String[] values = new String[] {"Android", "iPhone", "Windows", "WebOS", "Android", "iPhone", "Windows", "WebOS" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);

        setListAdapter(adapter);

        */

        NotesDbAdapter dbAdapter = new NotesDbAdapter(getActivity().getBaseContext());
        dbAdapter.open();
        notes = dbAdapter.getAllNotes();
        dbAdapter.close();

        noteAdapter = new NoteAdapter(getActivity(), notes);

        setListAdapter(noteAdapter);

        getListView().setDivider(null);
        getListView().setDividerHeight(0);

        registerForContextMenu(getListView());



    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        super.onListItemClick(l, v, position, id);

        launchNoteDetailActivity(MainActivity.FragmentToLaunch.VIEW, position);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);

        MenuInflater menuInflater = getActivity().getMenuInflater();
        menuInflater.inflate(R.menu.long_press_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int rowPosition = info.position;
        Note note = (Note) getListAdapter().getItem(rowPosition);

        switch (item.getItemId()){
            case R.id.edit:
                launchNoteDetailActivity(MainActivity.FragmentToLaunch.EDIT, rowPosition);
                Log.d("menu clicks", "we pressed edit");
                return true;
            case R.id.delete:
                NotesDbAdapter dbAdapter = new NotesDbAdapter(getActivity().getBaseContext());
                dbAdapter.open();
                dbAdapter.deleteNote(note.getId());

                notes.clear();
                notes.addAll(dbAdapter.getAllNotes());
                noteAdapter.notifyDataSetChanged();

                dbAdapter.close();
        }

        return super.onContextItemSelected(item);

    }

    private void launchNoteDetailActivity(MainActivity.FragmentToLaunch ftl, int position){

        Note note = (Note) getListAdapter().getItem(position);

        Intent intent = new Intent(getActivity(), NoteDetailActivity.class);

        intent.putExtra(MainActivity.NOTE_TITLE_EXTRA, note.getTitle());
        intent.putExtra(MainActivity.NOTE_MESSAGE_EXTRA, note.getMessage());
        intent.putExtra(MainActivity.NOTE_CATEGORY_EXTRA, note.getCategory());
        intent.putExtra(MainActivity.NOTE_DATE_EXTRA, note.getDate());
        intent.putExtra(MainActivity.NOTE_ID_EXTRA, note.getId());

        switch(ftl){
            case VIEW:
                intent.putExtra(MainActivity.NOTE_FRAGMENT_TO_LOAD_EXTRA, MainActivity.FragmentToLaunch.VIEW);
                break;
            case EDIT:
                intent.putExtra(MainActivity.NOTE_FRAGMENT_TO_LOAD_EXTRA, MainActivity.FragmentToLaunch.EDIT);
        }

        startActivity(intent);

    }

}

【问题讨论】:

  • 您的布局中是否存在坐标布局?

标签: android android-fragments android-snackbar snackbar android-data-usage


【解决方案1】:

在你的 build.gradle 添加最新的设计库。

compile 'com.android.support:design:X.X.X' // where X.X.X version

然后,在你的片段中做:

Snackbar  
    .make(view, "Item deleted",Snackbar.LENGTH_SHORT)
    .show();

参数view 可以是片段的根布局。你只需要它的参考。

欲了解更多信息,请参阅http://www.materialdoc.com/snackbar/

【讨论】:

    【解决方案2】:
    1. 添加设计库

         Compile 'com.android.support:design:X.X.X'
      
    2. 代码:

         case R.id.delete:
              NotesDbAdapter dbAdapter = new NotesDbAdapter(getActivity().getBaseContext());
              dbAdapter.open();
              dbAdapter.deleteNote(note.getId());
      
              notes.clear();
              notes.addAll(dbAdapter.getAllNotes());
              noteAdapter.notifyDataSetChanged();
      
              dbAdapter.close();
      
              // Show SNACK Bar
              mRoot = (RelativeLayout) view.findViewById(R.id.mainrl);
      
              Snackbar snackbar = Snackbar.make(mRoot , "Item Deleted", Snackbar.LENGTH_LONG);
              snackbar.show();
      

    这里 mRoot 是片段的主根布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2021-08-28
      • 2014-03-14
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多