当我点击了recyclelerView的新闻之一时,我正在将RSS源提取到我的APP中并尝试打开一个意图(Web浏览器),但它无法正常工作。那是我的代码:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<FeedItem> feedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
//Call Read rss asyntask to fetch rss
ReadRss readRss = new ReadRss(this, recyclerView);
readRss.execute();
recyclerView.setOnClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
**FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);**
}
});
}
}
我甚至尝试将相同的代码放在我的持有者类中,但在那里startActivity()方法不起作用: 上面的代码有点相同,但在另一个类中:
public class FeedsAdapter extends RecyclerView.Adapter<FeedsAdapter.MyViewHolder> implements View.OnClickListener {
ArrayList<FeedItem> feedItems;
Context context;
public FeedsAdapter(Context context, ArrayList<FeedItem>feedItems){
this.feedItems=feedItems;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
view.setClickable(true);
view.setOnClickListener(this);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
FeedItem current=feedItems.get(position);
holder.Title.setText(current.getTitle());
holder.Description.setText(current.getDescription());
holder.Date.setText(current.getPubDate());
Picasso.get().load(current.getThumbnailUrl()).into(holder.Thumbnail);
holder.cardView.setTag(position);
Toast.makeText(context,current.getLink() ,Toast.LENGTH_SHORT).show();
}
@Override
public int getItemCount() {
return feedItems.size();
}
@Override
public void onClick(View view) {
**int position = (int) view.getTag();
FeedItem current=feedItems.get(position);
Toast.makeText(context,"The Item Clicked is: "+current.getLink() ,Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);**
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title,Description,Date;
ImageView Thumbnail;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
Title= (TextView) itemView.findViewById(R.id.title_text);
Description= (TextView) itemView.findViewById(R.id.description_text);
Date= (TextView) itemView.findViewById(R.id.date_text);
Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
cardView= (CardView) itemView.findViewById(R.id.cardview);
}
}
}
此代码:
FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
是我在两个类中尝试的相同代码,第一个错误是因为RecyclerView,而在第二个类中它给出了错误,因为startActivity不能在该类中工作。 那么,如何激活点击进入浏览器时出现的可疑消息?
答案 0 :(得分:1)
从adapter class
使用
context.startActivity(intent);
(如果那是你的问题。)
我的建议
在onBindViewHolder(..)
holder.itemView.setOnClick....
中的点击监听器