根据点击的内容更改图像

时间:2016-05-31 20:18:24

标签: android image navigation

我已经使用下面的代码成功传递了我的标题和描述,但是当我尝试传递我的标题图像时,没有任何反应。我的错误在哪里?

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;
}

}

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

1 个答案:

答案 0 :(得分:0)

请试试这个

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

如果它也无效,请打印您的     。getIntent()getExtras()getInt(" endpageHeader&#34)。  日志中的值,并检查它是`R.drawable.your_image`

的形式