2016-05-31 119 views
0

我已经成功地通过了我的标题和描述,但是当我尝试传递我的标题图片时,没有任何反应。我的错误在哪里?根据点击的内容更改图片

MyPageHolder.java

public abstract class MyPageHolder extends RecyclerView.ViewHolder{ 

protected final Activity activity; 
protected MyPageAdapter adapter; 
public TextView txtTitle, txtDescription; 
public ImageView imgImage; 
public View view; 


public MyPageHolder(View v, Activity activity, MyPageAdapter adapter) { 
    super(v); 
    this.activity = activity; 
    this.adapter = adapter; 

    imgImage = (ImageView) v.findViewById(R.id.cardview_image); 
    txtTitle = (TextView) v.findViewById(R.id.cardview_title); 
    txtDescription = (TextView) v.findViewById(R.id.cardview_description); 
    view = (CardView) v.findViewById(R.id.card_view); 

    view.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      /*/ this is where the magic happens when clicked /*/ 


     } 
    }); 
} 


public void bind(MenuPageItems dataModel, Activity activity, final int position) { 
    final MenuPageItems m = (MenuPageItems)dataModel; 
    imgImage.setImageResource(m.image); 
    txtTitle.setText(m.title); 
    txtDescription.setText(m.description); 

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

      Intent cvIntent = new Intent(view.getContext(), EndpageActivity.class); 


      cvIntent.putExtra("endpageHeader", m.image); 


      // text to pass to endpage activity 
      cvIntent.putExtra("endpageTitle", m.title); 
      cvIntent.putExtra("endpageTheContent", m.description); 
      view.getContext().startActivity(cvIntent); 
     } 

    }); 

} 


} 

EndpageActivity.java

public class EndpageActivity extends AppCompatActivity { 

TextView t1; 
ImageView iv1; 


private void loadBackdrop() { 
    String uri = "@drawable/p400x200"; 
    // where backdropX.jpg (without the extension) is the file 
    int imageResource = getResources().getIdentifier(uri, null, getPackageName()); 
    final ImageView imageView = (ImageView) findViewById(R.id.endpage_header_image); 
    Drawable res = getResources().getDrawable(imageResource); 
    imageView.setImageDrawable(res); 
} 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.endpage_layout); 

    Intent intent = getIntent(); 


    String sTitle = intent.getStringExtra("endpageTitle"); 
    String sTheContent = intent.getStringExtra("endpageTheContent"); 

    CollapsingToolbarLayout collapsingToolbar = 
      (CollapsingToolbarLayout) findViewById(R.id.endpage_header); 
    collapsingToolbar.setTitle(sTitle); 


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // this adds the back button arrow to the header 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    iv1 = (ImageView) findViewById(R.id.endpage_header_image); 
    iv1.setImageDrawable(getResources().getDrawable(getIntent().getIntExtra("endpageHeader", 0))); 

    t1 = (TextView)findViewById(R.id.endpage_the_content); 
    t1.setText(sTheContent); 

    loadBackdrop(); 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.sample_actions, menu); 
    return true; 
} 

} 

的应用程序运行,但只有我的文字变化,ImageView的仍然显示我的图像占位符。

回答

0

请试试这个

iv1.setImageResource(getIntent().getExtras().getInt("endpageHeader")); 

如果还没有工作,打印您的 getIntent()。getExtras()。getInt( “endpageHeader”) 值记录,并检查它的形式`R.drawable.your_image`