在我的应用程序中,我使用RecaylerView小部件开发了一个新闻功能。现在我想把这个recyclerView的CoverImage和Date传递给详细的新闻页面。我可以成功通过新闻日期。但封面图像未显示在详细信息页面中。我在android监视器窗口的logcat中找到了0个图像。我无法确定这里会出现什么问题。
我的模特新闻类是 -
public class NewsModel extends RealmObject {
@PrimaryKey
private int image;
private String title;
private String date;
private String detail ;
public NewsModel() {
}
public NewsModel(int image, String title, String date) {
this.image = image;
this.title = title;
this.date = date;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
......
}
我的新闻页面是
public class NewsAdapter extends
RecyclerView.Adapter<NewsAdapter.NewsHolder> {
private List<NewsModel> newsObject;
private Context context;
public NewsAdapter(Context context, List<NewsModel> newsObject) {
this.context = context;
this.newsObject=newsObject;
}
@Override
public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_row_layout,parent,false);
return new NewsHolder(view);
}
@Override
public void onBindViewHolder(NewsAdapter.NewsHolder holder, int position) {
final NewsModel newsModel=newsObject.get(position);
holder.newsImage.setImageResource(newsModel.getImage());
holder.newsHeadline.setText(newsModel.getTitle());
holder.newsDate.setText(newsModel.getDate());
holder.cardView.setTag(position);
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = (int)view.getTag();
openDetailNews(Integer.toString(newsModel.getImage()),newsModel.getDate());
}
});
}
//open Detail News Page
private void openDetailNews(String...details) {
Intent i=new Intent(context,DetailNews.class);
i.putExtra("image",details[0]);
i.putExtra("date",details[1]);
context.startActivity(i);
}
@Override
public int getItemCount() {
return newsObject.size();
}
public class NewsHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public ImageView newsImage;
public TextView newsHeadline;
public TextView newsDate;
public NewsHolder(View itemView) {
super(itemView);
newsImage=(ImageView)itemView.findViewById(R.id.news_picture);
newsHeadline=(TextView)itemView.findViewById(R.id.news_headline);
newsDate=(TextView) itemView.findViewById(R.id.news_date);
cardView=(CardView)itemView.findViewById(R.id.cardview_news);
}
}
}
我希望从新闻页面获取封面图片和日期的详细新闻页面代码是 -
public class DetailNews extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<NewsImageModel> newsObject;
ImageView _coverImage;
TextView _description;
TextView _newsDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
// Showing and Enabling clicks on the Home/Up button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
newsObject=getAllImageList();
// setting up views and stuff
setUpUIViews();
Intent intent = getIntent();
//RECEIVE DATA
Log.e("_coverImage",""+_coverImage);
Integer coverImage=intent.getExtras().getInt("image");
Log.e("coverImage",""+coverImage);
String newsDate=intent.getExtras().getString("date");
//BIND DATA
_coverImage.setImageResource(coverImage);
Log.e("coverImage",""+coverImage);
_newsDate.setText(newsDate);
}
private void setUpUIViews() {
_coverImage=(ImageView)findViewById(R.id.news_cover);
_description=(TextView)findViewById(R.id.news_description);
_newsDate=(TextView)findViewById(R.id.news_date);
recyclerView = (RecyclerView)findViewById(R.id.image_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(DetailNews.this);
recyclerView.setLayoutManager(layoutManager);
adapter = new NewsDetailAdapter(this,newsObject );
recyclerView.setAdapter(adapter);
}
private List<NewsImageModel> getAllImageList() {
List<NewsImageModel> images = new ArrayList<NewsImageModel>();
......
return images;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
}
答案 0 :(得分:0)
试试吧!
UserRepository
答案 1 :(得分:0)
使用此:
public void onClick(View view) {
int pos = (int)view.getTag();
Intent i=new Intent(context,DetailNews.class);
i.putExtra("image",newsModel.getImage());
i.putExtra("date",newsModel.getDate());
context.startActivity(i);
}
然后在你的活动中:
Intent intent = getIntent();
int imageId = intent.getIntExtra("image",0);
然后使用此资源ID设置您的图片
_coverImage.setImageResource(imageId);
答案 2 :(得分:0)
您无法在图片视图中直接显示字符串网址或任何整数类型。您需要添加Image lib,如Picaso或glide。 按照以下步骤:
1.添加依赖项:
compile 'com.squareup.picasso:picasso:2.5.2'
2.在您的适配器中,用旧代码替换为
Picasso.with(context).load(newsModel.getImage()).into(holder.newsImage);
你的问题将得到解决。
答案 3 :(得分:0)
private int image;
您为图像设置了什么整数数据?它是资源图像吗?