0

我正在尋找一種方法,當用戶使用caseR.id.delete:從數據庫中刪除項目時,會在顯示屏底部彈出一個Snackbox。下面我附上了片段的代碼。如果您需要更多來自不同地區的代碼,請告訴我。從數據庫中刪除項目後,在片段中顯示一個Snackbar?

/** 
* 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); 

    } 

} 
+0

您的佈局中是否有座標佈局? –

回答

0

在你的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/

0
  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是您片段的主要根部佈局。

相關問題