2016-08-22 12 views
0

私はCabのコードを既存のRecyclerViewに追加しましたが、Androidスタジオではエラーは表示されず、エミュレータではOnLongClickは何も起こりません。どんな助けもありがとうございます。 コードは何もしないようです。CABはRecyclerViewに表示されません

public class LoaderActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Music>> { 

private RecyclerView mRecyclerView; 
private MusicAdapter musicAdapter; 

ActionMode actionMode; 
public static final String TAG = "YOUR-TAG-NAME"; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    onContentChanged(); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_loader); 

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerVw); 
    // Use a LinearLayoutManager to make the RecyclerView display the Music in a vertically scrolling list. 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    musicAdapter = new MusicAdapter(this, new ArrayList<Music>()); 
    mRecyclerView.setAdapter(musicAdapter); 

    mRecyclerView.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      if (actionMode!=null){ 
       return true; 
      } 
      else { 
       actionMode=LoaderActivity.this.startActionMode(callback); 
       return true; 
      } 

     } 
    }); 
    final LoaderManager supportLoaderManager = getSupportLoaderManager(); 
    supportLoaderManager.initLoader(1, null, this); 
    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 
private ActionMode.Callback callback=new ActionMode.Callback() { 
    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) 
    { 
     MenuInflater menuInflater=getMenuInflater(); 
     menuInflater.inflate(R.menu.menu_main,menu); 
     return true; 
    } 

    @Override 
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
     mode.setTitle("1 item is selected"); 
     return false; 
    } 

    @Override 
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     if (item.getItemId()==R.id.id_delete) 
     { 
      Toast.makeText(getApplicationContext(),"you really want to delete?",Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     else 
     if (item.getItemId()==R.id.id_share) { 
      Toast.makeText(getApplicationContext(), "you really want to share?", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     return false; 
    } 


    @Override 
    public void onDestroyActionMode(ActionMode mode) { 



    } 

    }; 

音楽アダプタ

public MusicAdapter(Context context, List<Music> music) { 
     mMusic = new ArrayList<>(); 
     if(music != null) { 
      mMusic.addAll(music); 
     } 
     mContext = context; 
     mPlaceholder = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_music_note_black_48dp); 
     // Get the maximum size of byte we are allowed to allocate on the VM head and convert it to bytes. 
     int maxSize = (int) (Runtime.getRuntime().maxMemory()/1024); 
     // Divide the maximum size by eight to get a adequate size the LRU cache should reach before it starts to evict bitmaps. 
     int cacheSize = maxSize/8; 
     mBitmapCache = new LruCache<Long, Bitmap>(cacheSize) { 

      @Override 
      protected int sizeOf(Long key, Bitmap value) { 
       // returns the size of bitmaps in kilobytes. 
       return value.getByteCount()/1024; 
      } 
     }; 
    } 
    /** 
    * Adds a {@link Music} item to the Adapter. 
    * @param 
    */ 
    /** 
    * Adds a {@link List} of {@link Music} to the adapters. 
    * This method replaces the current music items inside of the adapter with the specified music items. 
    * @param 
    */ 
    public void clearItem() { 
     mMusic.clear(); 
    } 
    public void addItems(List<Music> music) { 
     // Clear the old items. I only do this so that I don't have to do duplicating checks on the music items. 
     mMusic.clear(); 
     // Add the new music list. 
     mMusic.addAll(music); 
     notifyItemRangeInserted(0, music.size()); 
    } 
    /** 
    * Clears the {@link Music} items inside of this adapter. 
    */ 
    @Override 
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     View v = inflater.inflate(R.layout.list_item, parent, false); 
     MusicViewHolder musicViewHolder = new MusicViewHolder(v); 
     return musicViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(MusicViewHolder holder, final int position) { 
     final Music music = mMusic.get(position); 
     holder.itemView.setLongClickable(true); 
     // Check the Bitmap cache for the album art first.. 
     final Bitmap bitmap = mBitmapCache.get(music.getAlbumId()); 
     // If the bitmap is not null, then use the cached images. 
     if(bitmap != null){ 
      holder.icon.setImageBitmap(bitmap); 
     } 
     else { 
      // No album art could be found in the cache try reloading it. 
      // In a real work example you should check that this value is not some junk value indicating that their is no album artwork. 
      loadAlbumArt(holder.icon, music.getAlbumId()); 
     } 

     holder.artist.setText(music.getArtist()); 
     holder.title.setText(music.getTitle()); 
     final Uri trackuri= ContentUris.withAppendedId(
       android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music.getId()); 
     holder.itemView.setOnClickListener(new View.OnClickListener() { 
              @Override 
              public void onClick(View v) { 

               Intent intent = new Intent(mContext, Playrecord.class); 
               intent.setData(trackuri); 
               mContext.startActivity(intent); 


              } 
              }); 





    } 



    /** 
    * Helper method for asynchronously loading album art. 
    * @param icon 
    * @param albumId 
    */ 
    public void loadAlbumArt(ImageView icon, long albumId) { 
     // Check the current album art task if any and cancel it, if it is loading album art that doesn't match the specified album id. 
     if(cancelLoadTask(icon, albumId)) { 
      // There was either no task running or it was loading a different image so create a new one to load the proper image. 
      LoadAlbumArt loadAlbumArt = new LoadAlbumArt(icon, mContext); 
      // Store the task inside of the async drawable. 
      AsyncDrawable drawable = new AsyncDrawable(mContext.getResources(), mPlaceholder.getBitmap(),loadAlbumArt); 
      icon.setImageDrawable(drawable); 
      loadAlbumArt.execute(albumId); 
     } 
    } 

    /** 
    * Helper method cancelling {@link LoadAlbumArt}. 
    * 
    * @param icon 
    * @param albumId 
    * @return 
    */ 
    public boolean cancelLoadTask(ImageView icon, long albumId) { 
     LoadAlbumArt loadAlbumArt = (LoadAlbumArt) getLoadTask(icon); 
     // If the task is null return true because we want to try and load the album art. 
     if(loadAlbumArt == null) { 
      return true; 
     } 
     if(loadAlbumArt != null) { 
      // If the album id differs cancel this task because it cannot be recycled for this imageview. 
      if(loadAlbumArt.albumId != albumId) { 
       loadAlbumArt.cancel(true); 
       return true; 
      } 
     } 
     return false; 
    } 

    /** 
    * Helper method for extracting an {@link LoadAlbumArt}. 
    * @param icon 
    * @return 
    */ 
    public AsyncTask getLoadTask(ImageView icon) { 
     LoadAlbumArt task = null; 
     Drawable drawable = icon.getDrawable(); 
     if(drawable instanceof AsyncDrawable) { 
      task = ((AsyncDrawable) drawable).getLoadArtworkTask(); 
     } 
     return task; 
    } 

    public void remove(long itemId) { 
    } 


    private class LoadAlbumArt extends AsyncTask<Long, Void, Bitmap> { 

     // URI that points to the AlbumArt database. 
     private final Uri albumArtURI = Uri.parse("content://media/external/audio/albumart"); 
     public WeakReference<ImageView> mIcon; 
     // Holds a publicly accessible albumId to be checked against. 
     public long albumId; 
     public Context mContext; 
     int width, height; 

     public LoadAlbumArt(ImageView icon, Context context) { 
      // Store a weak reference to the imageView. 
      mIcon = new WeakReference<ImageView>(icon); 
      // Store the width and height of the imageview. 
      // This is necessary for properly scalling the bitmap. 
      width = icon.getWidth(); 
      height = icon.getHeight(); 
      mContext = context; 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      if(isCancelled() || bitmap == null){ 
       return; 
      } 
      // Check to make sure that the imageview has not been garbage collected as well as the 
      // LoadArtworkTask is the same as this one. 
      if(mIcon != null && mIcon.get() != null) { 
       ImageView icon = mIcon.get(); 
       Drawable drawable = icon.getDrawable(); 
       if(drawable instanceof AsyncDrawable) { 
        LoadAlbumArt task = ((AsyncDrawable) drawable).getLoadArtworkTask(); 
        // Make sure that this is the same task as the one current stored inside of the ImageView's drawable. 
        if(task != null && task == this) { 
         icon.setImageBitmap(bitmap); 
        } 
       } 
      } 
      mBitmapCache.put(albumId, bitmap); 
      super.onPostExecute(bitmap); 
     } 

     @Override 
     protected Bitmap doInBackground(Long... params) { 
      // AsyncTask are not guaranteed to start immediately and could be cancelled somewhere in between calling doInBackground. 
      if(isCancelled()){ 
       return null; 
      } 
      albumId = params[0]; 
      // Append the albumId to the end of the albumArtURI to create a new Uri that should point directly to the album art if it exist. 
      Uri albumArt = ContentUris.withAppendedId(albumArtURI, albumId); 
      Bitmap bmp = null; 

      return bmp; 
     } 
    } 
    /** 
    * Custom drawable that holds a LoadArtworkTask 
    */ 

    private static class AsyncDrawable extends BitmapDrawable { 
     WeakReference<LoadAlbumArt> loadArtworkTaskWeakReference; 

     public AsyncDrawable(Resources resources, Bitmap bitmap, LoadAlbumArt task) { 
      super(resources, bitmap); 
      // Store the LoadArtwork task inside of a weak reference so it can still be garbage collected. 
      loadArtworkTaskWeakReference = new WeakReference<LoadAlbumArt>(task); 
     } 

     public LoadAlbumArt getLoadArtworkTask() { 
      return loadArtworkTaskWeakReference.get(); 
     } 
    } 

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

    /** 
    * Custom ViewHolder that represents the List Item. 
    */ 
    public static class MusicViewHolder extends RecyclerView.ViewHolder { 

     ImageView icon; 
     TextView title; 
     TextView artist; 

     public MusicViewHolder(View itemView) { 
      super(itemView); 
      icon = (ImageView) itemView.findViewById(R.id.icon); 
      title = (TextView) itemView.findViewById(R.id.title); 
      artist = (TextView)itemView.findViewById(R.id.subtitle); 
     } 



} 
+0

郵便MusicAdapterコード –

+0

は、コードが、それはエラーを投げているローダアクティビティで –

答えて

1

問題は、あなたが持っているものアダプタの景色、長い項目のクリックリスナーではなくリサイクルビュー自体を設定しているということです、アダプターをアップロードしていただきありがとうございます行うには、私はあなたのアダプタにインターフェイスを追加しましたクリックでトリガーあなたが意図して行ったように長いクリックを聞くアダプタのインターフェイスを作成することです:

public class MusicAdaper { 

    public interface onLongItemClickLLsitener { 
     void onLonClick(View view); 
    } 

    private onLongItemClickLLsitener mCallback; 

    public MusicAdapter(Context context, List<Music> music, onLongItemClickLLsitener listener) { 

     mCallback = listener; 
     mMusic = new ArrayList<>(); 
     if(music != null) { 
      mMusic.addAll(music); 
     } 
     mContext = context; 
     mPlaceholder = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_music_note_black_48dp); 
     // Get the maximum size of byte we are allowed to allocate on the VM head and convert it to bytes. 
     int maxSize = (int) (Runtime.getRuntime().maxMemory()/1024); 
     // Divide the maximum size by eight to get a adequate size the LRU cache should reach before it starts to evict bitmaps. 
     int cacheSize = maxSize/8; 
     mBitmapCache = new LruCache<Long, Bitmap>(cacheSize) { 

      @Override 
      protected int sizeOf(Long key, Bitmap value) { 
       // returns the size of bitmaps in kilobytes. 
       return value.getByteCount()/1024; 
      } 
     }; 
    } 
    /** 
    * Adds a {@link Music} item to the Adapter. 
    * @param 
    */ 
    /** 
    * Adds a {@link List} of {@link Music} to the adapters. 
    * This method replaces the current music items inside of the adapter with the specified music items. 
    * @param 
    */ 
    public void clearItem() { 
     mMusic.clear(); 
    } 
    public void addItems(List<Music> music) { 
     // Clear the old items. I only do this so that I don't have to do duplicating checks on the music items. 
     mMusic.clear(); 
     // Add the new music list. 
     mMusic.addAll(music); 
     notifyItemRangeInserted(0, music.size()); 
    } 
    /** 
    * Clears the {@link Music} items inside of this adapter. 
    */ 
    @Override 
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     View v = inflater.inflate(R.layout.list_item, parent, false); 
     MusicViewHolder musicViewHolder = new MusicViewHolder(v); 
     return musicViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(MusicViewHolder holder, final int position) { 
     final Music music = mMusic.get(position); 
     holder.itemView.setLongClickable(true); 
     // Check the Bitmap cache for the album art first.. 
     final Bitmap bitmap = mBitmapCache.get(music.getAlbumId()); 
     // If the bitmap is not null, then use the cached images. 
     if(bitmap != null){ 
      holder.icon.setImageBitmap(bitmap); 
     } 
     else { 
      // No album art could be found in the cache try reloading it. 
      // In a real work example you should check that this value is not some junk value indicating that their is no album artwork. 
      loadAlbumArt(holder.icon, music.getAlbumId()); 
     } 

     holder.artist.setText(music.getArtist()); 
     holder.title.setText(music.getTitle()); 
     final Uri trackuri= ContentUris.withAppendedId(
       android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music.getId()); 

     holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       mCallback.onLonClick(v); 
       return false; 
      } 
     }); 

     holder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(mContext, Playrecord.class); 
       intent.setData(trackuri); 
       mContext.startActivity(intent); 


      } 
     }); 





    } 



    /** 
    * Helper method for asynchronously loading album art. 
    * @param icon 
    * @param albumId 
    */ 
    public void loadAlbumArt(ImageView icon, long albumId) { 
     // Check the current album art task if any and cancel it, if it is loading album art that doesn't match the specified album id. 
     if(cancelLoadTask(icon, albumId)) { 
      // There was either no task running or it was loading a different image so create a new one to load the proper image. 
      LoadAlbumArt loadAlbumArt = new LoadAlbumArt(icon, mContext); 
      // Store the task inside of the async drawable. 
      AsyncDrawable drawable = new AsyncDrawable(mContext.getResources(), mPlaceholder.getBitmap(),loadAlbumArt); 
      icon.setImageDrawable(drawable); 
      loadAlbumArt.execute(albumId); 
     } 
    } 

    /** 
    * Helper method cancelling {@link LoadAlbumArt}. 
    * 
    * @param icon 
    * @param albumId 
    * @return 
    */ 
    public boolean cancelLoadTask(ImageView icon, long albumId) { 
     LoadAlbumArt loadAlbumArt = (LoadAlbumArt) getLoadTask(icon); 
     // If the task is null return true because we want to try and load the album art. 
     if(loadAlbumArt == null) { 
      return true; 
     } 
     if(loadAlbumArt != null) { 
      // If the album id differs cancel this task because it cannot be recycled for this imageview. 
      if(loadAlbumArt.albumId != albumId) { 
       loadAlbumArt.cancel(true); 
       return true; 
      } 
     } 
     return false; 
    } 

    /** 
    * Helper method for extracting an {@link LoadAlbumArt}. 
    * @param icon 
    * @return 
    */ 
    public AsyncTask getLoadTask(ImageView icon) { 
     LoadAlbumArt task = null; 
     Drawable drawable = icon.getDrawable(); 
     if(drawable instanceof AsyncDrawable) { 
      task = ((AsyncDrawable) drawable).getLoadArtworkTask(); 
     } 
     return task; 
    } 

    public void remove(long itemId) { 
    } 


    private class LoadAlbumArt extends AsyncTask<Long, Void, Bitmap> { 

     // URI that points to the AlbumArt database. 
     private final Uri albumArtURI = Uri.parse("content://media/external/audio/albumart"); 
     public WeakReference<ImageView> mIcon; 
     // Holds a publicly accessible albumId to be checked against. 
     public long albumId; 
     public Context mContext; 
     int width, height; 

     public LoadAlbumArt(ImageView icon, Context context) { 
      // Store a weak reference to the imageView. 
      mIcon = new WeakReference<ImageView>(icon); 
      // Store the width and height of the imageview. 
      // This is necessary for properly scalling the bitmap. 
      width = icon.getWidth(); 
      height = icon.getHeight(); 
      mContext = context; 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      if(isCancelled() || bitmap == null){ 
       return; 
      } 
      // Check to make sure that the imageview has not been garbage collected as well as the 
      // LoadArtworkTask is the same as this one. 
      if(mIcon != null && mIcon.get() != null) { 
       ImageView icon = mIcon.get(); 
       Drawable drawable = icon.getDrawable(); 
       if(drawable instanceof AsyncDrawable) { 
        LoadAlbumArt task = ((AsyncDrawable) drawable).getLoadArtworkTask(); 
        // Make sure that this is the same task as the one current stored inside of the ImageView's drawable. 
        if(task != null && task == this) { 
         icon.setImageBitmap(bitmap); 
        } 
       } 
      } 
      mBitmapCache.put(albumId, bitmap); 
      super.onPostExecute(bitmap); 
     } 

     @Override 
     protected Bitmap doInBackground(Long... params) { 
      // AsyncTask are not guaranteed to start immediately and could be cancelled somewhere in between calling doInBackground. 
      if(isCancelled()){ 
       return null; 
      } 
      albumId = params[0]; 
      // Append the albumId to the end of the albumArtURI to create a new Uri that should point directly to the album art if it exist. 
      Uri albumArt = ContentUris.withAppendedId(albumArtURI, albumId); 
      Bitmap bmp = null; 

      return bmp; 
     } 
    } 
    /** 
    * Custom drawable that holds a LoadArtworkTask 
    */ 

    private static class AsyncDrawable extends BitmapDrawable { 
     WeakReference<LoadAlbumArt> loadArtworkTaskWeakReference; 

     public AsyncDrawable(Resources resources, Bitmap bitmap, LoadAlbumArt task) { 
      super(resources, bitmap); 
      // Store the LoadArtwork task inside of a weak reference so it can still be garbage collected. 
      loadArtworkTaskWeakReference = new WeakReference<LoadAlbumArt>(task); 
     } 

     public LoadAlbumArt getLoadArtworkTask() { 
      return loadArtworkTaskWeakReference.get(); 
     } 
    } 

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

    /** 
    * Custom ViewHolder that represents the List Item. 
    */ 
    public static class MusicViewHolder extends RecyclerView.ViewHolder { 

     ImageView icon; 
     TextView title; 
     TextView artist; 

     public MusicViewHolder(View itemView) { 
      super(itemView); 
      icon = (ImageView) itemView.findViewById(R.id.icon); 
      title = (TextView) itemView.findViewById(R.id.title); 
      artist = (TextView)itemView.findViewById(R.id.subtitle); 
     } 



    } 
} 

とであなたの活動は、あなたがこのような何かを行う必要があります: ``エラー:(51、24)エラー:クラスMusicAdapterでコンストラクタMusicAdapter

MusicAdaper adaper = new MusicAdaper(this, new ArrayList<Music>(), new onLongItemClickLLsitener() { 
      @Override 
      public void onLonClick(View view) { 
       // do something here 
      } 
     }); 
+0

を@JuanHurtado追加しました指定された型には適用できません。 が必要:コンテキストを、リスト、onLongItemClickLLsitener が見つかりました:LoaderActivity、ArrayListの 理由:実際の仮引数リストは今 –

+0

が異なりますコンテキスト、2.リスト、3. onLongItemClickLLsitener –

+0

1. A、recevideするコンストラクタだけでなく、アダプタを変更することを忘れないでください、あなたの編集を適用するlength' –

関連する問題