2017-04-27 30 views
-1

我試圖從一個適配器,這將使我的信息建立一個集電視節目cardview傳遞信息。無法從適配器傳遞的Intent中獲得額外功能?

我的價值觀。然而所有正在返回空。

這裏是我的適配器代碼(有問題的代碼是在第3個case語句MyMenuItemClickListener)

public class TvShowSeasonsAdapter extends RecyclerView.Adapter<TvShowSeasonsAdapter.MyViewHolder> { 

private Context mContext; 
private List<Season> seasonList; 

public class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView tvShowTitle,season, episodes; 
    public ImageView thumbnail, overflow; 

    public MyViewHolder(View view) { 
     super(view); 
     tvShowTitle = (TextView) view.findViewById(R.id.tvShowTitle); 
     season = (TextView) view.findViewById(R.id.Season); 
     episodes = (TextView) view.findViewById(R.id.Episodes); 
     thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
     overflow = (ImageView) view.findViewById(R.id.overflow); 
    } 
} 


public TvShowSeasonsAdapter(Context mContext, List<Season> seasonList) { 
    this.mContext = mContext; 
    this.seasonList = seasonList; 
} 

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

    return new MyViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position) { 
    Season tvshowSeason = seasonList.get(position); 
    holder.tvShowTitle.setText(tvshowSeason.getTvShowName()); 
    holder.season.setText("Season " + tvshowSeason.getSeasonNumber()); 
    holder.episodes.setText("No of Episodes :" + tvshowSeason.getEpisodeCount()); 

    // loading TV show poster using Glide library 
    Glide.with(mContext).load(tvshowSeason.getPosterPath()).into(holder.thumbnail); 

    holder.overflow.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showPopupMenu(holder.overflow, position); 
     } 
    }); 
} 

/** 
* Showing popup menu when tapping on 3 dots 
*/ 
private void showPopupMenu(View view, int position) { 
    // inflate menu 
    PopupMenu popup = new PopupMenu(mContext, view); 
    MenuInflater inflater = popup.getMenuInflater(); 
    inflater.inflate(R.menu.menu_tvshowseasonsadapter, popup.getMenu()); 
    popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position)); 
    popup.show(); 

} 

/** 
* Click listener for popup menu items 
*/ 
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener { 

    int position; 

    public MyMenuItemClickListener(int position) { 
     this.position=position; 
    } 

    @Override 
    public boolean onMenuItemClick(MenuItem menuItem) { 
     Intent intent; 

     switch (menuItem.getItemId()) { 
      case R.id.go_to_Popular: 
       Toast.makeText(mContext, "Go back to Popular", Toast.LENGTH_SHORT).show(); 
       intent = new Intent(mContext,PopularActivity.class); 
       mContext.startActivity(intent); 
       Toast.makeText(mContext, "Add to Favourites", Toast.LENGTH_SHORT).show(); 
       return true; 
      case R.id.back_to_favourites: 
       Toast.makeText(mContext, "Go to Favourites", Toast.LENGTH_SHORT).show(); 
       /* 
       TODO - Need to make sure there is at least one favourite selected before allowing the 
       the user to go to the favourite activity. */ 
       // Need to use mContext since in menu listener 
       intent = new Intent(mContext, FavouriteActivity.class); 
       mContext.startActivity(intent); 
       return true; 
      case R.id.view_episodes: 
       intent = new Intent(mContext, WhereWazzaEpisodes.class); 
       Intent intentContents = ((Activity) mContext).getIntent(); 
       int seasonForEpisodes = seasonList.get(position).getSeasonNumber(); 
       int amountOfEp = seasonList.get(position).getEpisodeCount(); 
       int tvShowId = intentContents.getIntExtra("favTvShowID",0); 
       intent.putExtra("seasonNumber", seasonForEpisodes); 
       intent.putExtra("tvShowId", tvShowId); 
       intent.putExtra("episodeCount", amountOfEp); 

       mContext.startActivity(intent); 
       return true; 
      default: 
     } 
     return false; 
    } 
} 


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

}

這裏是我的onCreate方法活動代碼:

super.onCreate(savedInstanceState); 
    setContentView(R.layout.episode_list); 
    Intent intent=getIntent(); 

    Bundle extras = getIntent().getExtras(); 

    String tvShowID = extras.getString("favTvShowID"); 
    Log.d("TVSHOW_ID", tvShowID); 
    String seasonNumber = extras.getString("seasonNumber"); 
    Log.d("TVSHOW_SEASONNUM", seasonNumber); 
    String episodeCount = extras.getString("episodeCount"); 
    Log.d("TVSHOW_EPISODE", episodeCount); 

我都試過包和意圖嘗試,看看是否有任何區別,但他們仍然返回null。

+0

th你的名單上的值是什麼意思? –

+0

我認爲你的演員陣容是空的。一旦嘗試使用靜態數據。 – Rajasekhar

+0

你得到任何錯誤? –

回答

1

只是

嘗試,並確保你通過保持日誌之前

Log.e("checksendingdata",seasonForEpisodes+"||"+tvShowId+"||"+amountOfEp); 
    intent.putExtra("seasonNumber", seasonForEpisodes); 
    intent.putExtra("tvShowId", tvShowId); 
    intent.putExtra("episodeCount", amountOfEp); 

傳遞數據,如果在你的Android監控日誌深藏不露那麼它的錯誤在你的傳球數據

如果每事情很好設置意圖額外嘗試這在你的活動

Intent intent=getIntent(); 
int tvShowID=intent.getIntExtra("favTvShowID",0); 
int seasonNumber=intent.getIntExtra("seasonNumber",0); 
int episodeCount=intent.getIntExtra("episodeCount",0); 
/// 0 mean to default values in that where nothing passes from intent it will take 0 value 
+0

我似乎具有獲取tvShowID在適配器在這條線的問題:INT tvShowId = intentContents.getIntExtra(「favTvShowID」,0); - 看來我不能,如果你的是發送啓動適配器 –

+0

活動訪問值整數然後寫as'int tvShowID = intent.getIntExtra(「favTvShowID」); int seasonNumber = intent.getIntExtra(「seasonNumber」); INT episodeCount = intent.getIntExtra( 「episodeCount」);'用它編輯的代碼更新嘗試了上述 –

+0

這工作!謝謝! –

相關問題