2017-02-06 67 views
0
 @Override 
     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 
      Collections.swap(queuee, viewHolder.getAdapterPosition(), target.getAdapterPosition()); 

      // and notify the adapter that its dataset has changed 
      adapterr.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); 
      return true; 
     } 

與上述代碼我可以交換recyclerview中的項目,但現在的問題是我也想對數據庫做相同的相應更改。 怎麼走?集成回收站與數據庫

queuee從數據庫 queuee接收所有的數據是用來設置recyclerview適配器

問候 由於

+0

這很大程度上取決於您擁有哪種數據庫以及您的數據寫入方法是什麼 –

回答

0

適配器一個數組列表:

public class QueueAdapter extends RecyclerView.Adapter<QueueAdapter.MyViewHolder> { 
    private Context mContext; 
    public ArrayList<Music> queue; 
    MediaPlayer mPlayer = new MediaPlayer(); 



    public class MyViewHolder extends RecyclerView.ViewHolder { 


     public TextView title,tt,artist; 
     public Button play,stop; 
     public ImageButton option; 

     public MyViewHolder(View view) { 
      super(view); 
      title = (TextView) view.findViewById(R.id.songtitle1); 
      artist = (TextView)view.findViewById(R.id.artist1); 
      // play = (Button) view.findViewById(R.id.play); 
      // stop = (Button) view.findViewById(R.id.stop); 
      // tt = (TextView) view.findViewById(R.id.song_name); 
      option = (ImageButton) view.findViewById(R.id.option1); 




     } 
    } 

    public QueueAdapter(Context mContext, ArrayList<Music> queue) { 
     this.mContext = mContext; 
     this.queue = queue; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.queue_card, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 
     Music mu1 = queue.get(position); 
     holder.title.setText(mu1.getTitle()); 
     holder.artist.setText(mu1.getArtist()); 
     final String link =mu1.getUrl(); 
     final String SongName = mu1.getTitle(); 


    } 




    @Override 
    public int getItemCount() { 
     return queue.size(); 
    } 



    public void removeItem(int position) { 
     queue.remove(position); 
     notifyItemRemoved(position); 
     notifyItemRangeChanged(position, queue.size()); 
    } 


} 

活性:

recyclerView = (RecyclerView) findViewById(R.id.recyclerqueue); 
    recyclerView.setHasFixedSize(true); 

    layoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(layoutManager); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 





    adapterr = new QueueAdapter(this,queuee); 
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 

    // adapterr. 
    recyclerView.setAdapter(adapterr); 
    initSwipe(); 
0

在我看來,你可以在你的Activity中使用OnDestroy方法。具體而言,首先,您可以從適配器獲取所有數據,然後刪除數據庫中的數據,最後,可以再次保存從適配器獲取的數據。在這一刻,數據的順序就是你想要的,這種方式很容易編碼。我寧願你不要 在onMove期間更新你的數據庫,因爲在拖動過程中它太頻繁了,並且操作所有數據比數據庫中的兩個數據更容易。