I already ask similar question, When i click on textview(header_news from firebase), i switch to another activity and load data from Firebase. In another activity i get the same data(1 news by id). But i want to be displayed main_news. I know where problem, but i dont know how to fix it. I think problem in bindviewholder becouse i dont add myViewHolder.main_news.setText(Mnews.get(i).getMain_news()); But i cant to add, becouse its be displayed on header news.... P.S Sorry for my english i hope you understand me.
Adapter Code:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {
Context Mcontext;
ArrayList<News> Mnews;
public NewsAdapter(Context Mcontext, ArrayList<News> Mnews)
{
this.Mnews = Mnews;
this.Mcontext = Mcontext;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new MyViewHolder(LayoutInflater.from(Mcontext).inflate(R.layout.news_view, viewGroup, false));
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
final News news = Mnews.get(i);
myViewHolder.news_date.setText(Mnews.get(i).getDatenews());
myViewHolder.news_header.setText(Mnews.get(i).getHeader_news());
Picasso.get().load(Mnews.get(i).getImageURL()).into(myViewHolder.news_picture);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Mcontext, NewsActivity.class);
intent.putExtra("idnews", news.getIdnews());
Mcontext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return Mnews.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView news_date, news_header, main_news;
ImageView news_picture;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
news_date = (TextView) itemView.findViewById(R.id.news_date);
news_header = (TextView) itemView.findViewById(R.id.news_header);
news_picture = (ImageView) itemView.findViewById(R.id.news_picture);
}
}
}
And Activity code:
public class NewsActivity extends AppCompatActivity {
String idnews;
TextView main_news;
DatabaseReference reference;
TextView news_date;
RecyclerView recyclerView;
Intent intent;
ArrayList<News> Nlist;
NewsAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
main_news = findViewById(R.id.main_news);
intent = getIntent();
idnews = intent.getStringExtra("idnews");
Nlist = new ArrayList<News>();
reference = FirebaseDatabase.getInstance().getReference("News").child(idnews);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Nlist.clear();
News news = dataSnapshot.getValue(News.class);
main_news.setText(news.getNews_main());
Nlist.add(news);
adapter = new NewsAdapter(getApplicationContext(), Nlist);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(NewsActivity.this, "Что то не так", Toast.LENGTH_SHORT).show();
}
});
}
}
My database struct
I hope you can help me.
Related
I have a a view where a bunch of my Sqlite data is displayed. I recently added a column to my database called location_position. I have added a button to my relative layout so that when its clicked I want the data inside to be sorted in ascending format. I have tried following a few examples online but with the way my app is setup im struggling tom get it to work.
I have a routeView.java class and a routeAdapter
I would really apreciate if someone can show me how to have the content sorted when the button is clicked, or any resources with similar solutions
public class RouteView extends AppCompatActivity {
RecyclerView recyclerView;
routeAdapter routeAdapter;
ArrayList<String> location_id, location_name, location_county, location_description, location_route, location_position;
MyDatabaseHelper myDB;
Button createPDFButton, btnNorth, southBtn;
Context mContext = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_view);
Button southBtn = findViewById(R.id.button_south);
southBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Collections.sort(ArrayList);
}
});
// btnRemove = findViewById(R.id.btnRemove);
recyclerView = findViewById(R.id.recyclerView);
myDB = new MyDatabaseHelper(this);
createPDFButton = findViewById(R.id.createPDFButton);
location_id = new ArrayList<>();
location_name = new ArrayList<>();
location_county = new ArrayList<>();
location_description = new ArrayList<>();
location_route = new ArrayList<>();
location_position = new ArrayList<>();
Cursor cursor = myDB.readAllData();
createPDFButton.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onClick(View v) {
if (UserActivity.checkAppPermission(RouteView.this, "android.permission.WRITE_EXTERNAL_STORAGE", 1)){
generatePDF(recyclerView);
}
}
});
while (cursor.moveToNext()){
if(cursor.getString(4).equals("1")) {
location_id.add(cursor.getString(0));
location_name.add(cursor.getString(1));
location_county.add(cursor.getString(2));
location_description.add(cursor.getString(3));
location_route.add(cursor.getString(4));
location_position.add(cursor.getString(8));
}
}
routeAdapter = new routeAdapter(this, this, location_id, location_name, location_county, location_description, location_route, location_position);
recyclerView.setAdapter(routeAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
and this is the route adapter class
public class routeAdapter extends RecyclerView.Adapter<routeAdapter.MyViewHolder> {
//private final ClickListener listener;
private Context context;
MyDatabaseHelper myDB;
Activity activity;
private ArrayList location_id, location_name, location_county, location_description, location_position, location_route, location_image_url;
Button btnRemove;
String id, name, county, description;
routeAdapter(Activity activity, Context context, ArrayList location_id, ArrayList location_name, ArrayList location_county,
ArrayList location_description, ArrayList location_route, ArrayList location_position){
this.activity = activity;
this.context = context;
this.location_id = location_id;
this.location_name = location_name;
this.location_county = location_county;
this.location_description = location_description;
this.location_position = location_position;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.route_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.location_id_txt.setText(String.valueOf(location_id.get(position)));
holder.location_name_txt.setText(String.valueOf(location_name.get(position)));
holder.location_county_txt.setText(String.valueOf(location_county.get(position)));
holder.location_description_txt.setText(String.valueOf(location_description.get(position)));
holder.mainLayout2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, RouteView.class);
intent.putExtra("id", String.valueOf(location_id.get(position)));
intent.putExtra("name", String.valueOf(location_name.get(position)));
intent.putExtra("county", String.valueOf(location_county.get(position)));
intent.putExtra("description",String.valueOf(location_description.get(position)));
activity.startActivityForResult(intent, 2);
}
});
}
#Override
public int getItemCount() { return location_id.size(); }
class MyViewHolder extends RecyclerView.ViewHolder {
TextView location_id_txt, location_name_txt, location_county_txt, location_description_txt, added_to_route_txt, location_image_url;
LinearLayout mainLayout2;
Button btnRemove;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
location_id_txt = itemView.findViewById(R.id.location_id_txt);
location_name_txt = itemView.findViewById(R.id.location_name_txt);
location_county_txt = itemView.findViewById(R.id.location_county_txt);
location_description_txt = itemView.findViewById(R.id.location_description_txt);
mainLayout2 = itemView.findViewById(R.id.mainLayout2);
}
}
}
This is the basic concept:
On clicking a button or whatever to sort the things in a specific order, you sort the ArrayList that contains the values reflected in the RecyclerView. Then you update/ recreate the RecyclerView to register the changes.
I'm working on an application where I store the data on Firebase and retrieve it after the user manages to sign in. The application works fine I see this error occurs.
E/RecyclerView: No adapter attached; skipping layout
I'm loading an image together with long and short description. I'm trying to take the users into another Activity where they can the content more detailed. However. I am confused where I can set OnClickListener so that I can take the id of this post and load it in another activity.
this is the main activity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
homeAuth = FirebaseAuth.getInstance();
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true); // this will load it from the end and show last as first
progressDialog = new ProgressDialog(this);
recyclerView.setLayoutManager(layoutManager);
postModelList = new ArrayList<>();
pullPosts();
}
and this is the method where I pull the data from the DB :
void pullPosts() {
progressDialog.setMessage("Please wait for the pictures to load...");
progressDialog.show();
progressDialog.setCancelable(false);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
postModelList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()){
postModel = ds.getValue(PostModel.class);
postModelList.add(postModel);
postAdapter = new PostAdapter(HomeActivity.this , postModelList);
recyclerView.setAdapter(postAdapter);
}
progressDialog.dismiss();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
progressDialog.dismiss();
Toast.makeText(HomeActivity.this, ""+databaseError, Toast.LENGTH_SHORT).show();
}
});
}
I read other posts related to that error tried the options but the rest seems to me fine as the following is my Adapter :
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.MyHolder> {
Context context;
List<PostModel> postModelList;
public PostAdapter(Context context, List<PostModel> postModelList) {
this.context = context;
this.postModelList = postModelList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.home_post, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
String title = postModelList.get(position).getPostTitle();
String sDescription = postModelList.get(position).getPostShDescription();
String lDescription = postModelList.get(position).getPostLoDescription();
String image = postModelList.get(position).getPostImage();
holder.pulledTitle.setText(title);
holder.pulledShortDescription.setText(sDescription);
holder.pulledLongDescription.setText(lDescription);
Glide.with(context).load(image).into(holder.pulledImage);
//now we will add library to load image
}
#Override
public int getItemCount() {
return postModelList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView pulledImage;
TextView pulledTitle, pulledShortDescription, pulledLongDescription;
public MyHolder(#NonNull View itemView) {
super(itemView);
pulledImage = itemView.findViewById(R.id.pulled_image);
pulledTitle = itemView.findViewById(R.id.pulled_title);
pulledShortDescription = itemView.findViewById(R.id.pulled_short_description);
pulledLongDescription = itemView.findViewById(R.id.pulled_long_description);
}
}
}
Could you please help me with fixing the issue? I would appreciate your time and help.
E/RecyclerView: No adapter attached; skipping layout
that's not an error you can ignore it, it's happening because there's not adapter when the activity is created as you set the adapter after fetching the data.
About your question where to set onClickListener, you should do that in the adapter's viewHolder
class MyHolder extends RecyclerView.ViewHolder {
ImageView pulledImage;
TextView pulledTitle, pulledShortDescription, pulledLongDescription;
public MyHolder(#NonNull View itemView) {
super(itemView);
pulledImage = itemView.findViewById(R.id.pulled_image);
pulledTitle = itemView.findViewById(R.id.pulled_title);
pulledShortDescription = itemView.findViewById(R.id.pulled_short_description);
pulledLongDescription = itemView.findViewById(R.id.pulled_long_description);
itemView.setOnClickListner(v->{
//your implementation here f.e Id = postModelList.getId(); or and start another activity from here
});
}
}
I'm currently trying to implement a Firestore Recycler Adapter into my Android Studio Project but when I when I try to enter the activity with the recyclerView the app crashes. When I use the debugger it crashes when it reaches the .build() method of FireStoreRecyclerOptions. Is it something I'm doing, and if not, is there a workaround?
Here is the code for for the implementation of the RecyclerView:
public class SecurityCurrentRequests extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference requestRef = db.collection("Request");
private Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_security_current_requests);
setUpRecycler();
}
public void setUpRecycler()
{
Query query = requestRef.orderBy("priority", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
.setQuery(query, Request.class)
.build();
adapter = new Adapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart(){
super.onStart();
adapter.startListening();
}
#Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
}
and this is the Adapter Class:
public class Adapter extends FirestoreRecyclerAdapter<Request, Adapter.RequestViewHolder> {
public Adapter(#NonNull FirestoreRecyclerOptions<Request> options){
super(options);
}
#NonNull
#Override
public RequestViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//create new instance of the ViewHolder
// using request_cardview custom layout
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.request_cardview, parent, false);
return new RequestViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RequestViewHolder holder, int position, #NonNull Request requestObject) {
String firstText = requestObject.getName();
String secondText = requestObject.getMessage();
int thirdText = requestObject.getPriority();
String fourthText = requestObject.getCurrentDate();
holder.text1.setText(firstText);
holder.text2.setText(secondText);
holder.text3.setText(thirdText);
holder.text4.setText(fourthText);
}
public class RequestViewHolder extends RecyclerView.ViewHolder{
private TextView text1, text2, text3, text4;
RequestViewHolder(View itemView){
super(itemView);
text1 = itemView.findViewById(R.id.name);
text2 = itemView.findViewById(R.id.description);
text3 = itemView.findViewById(R.id.text_view_priority);
text4 = itemView.findViewById(R.id.date);
}
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
I have two recycler view one horizontal and one vertical
In my horizontal recycler view, there are categories and in vertical, there is items list. So I need to do that when I click on any category my vertical recycler view scroll to that specific item
Attaching the screenshot in the link - https://ibb.co/n67WdRn
https://ibb.co/P5CrjnH
I have tried using intent and it works but it starts a new activity
Please help
Mainactivity.java
package com.example.myapplication;
public class MainActivity extends AppCompatActivity {
DatabaseReference reference;
ArrayList<dish> list;
private static final String TAG = "MainActivity";
private ArrayList<String> mCategory = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getCategory();
menu();
}
private void getCategory(){
Log.d(TAG, "Preparing Categories");
mCategory.add("Vagetables");
mCategory.add("Indian Bread");
mCategory.add("Paranthas");
mCategory.add("Starters");
mCategory.add("Snacks");
mCategory.add("Salad");
mCategory.add("Noodles");
mCategory.add("Sandwiches");
mCategory.add("Rice");
mCategory.add("Soups");
mCategory.add("Beverages");
mCategory.add("Yoghurt");
mCategory.add("Papad");
initRecyclerView();
}
private void initRecyclerView(){
Log.d(TAG, "init RecyclerView");
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mCategory);
recyclerView.setAdapter(adapter);
}
private void menu(){
list = new ArrayList<dish>();
Log.d(TAG, "recycler MenuView : Firebase Initialized");
FirebaseApp.initializeApp(this);
reference = FirebaseDatabase.getInstance().getReference().child("dishes");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange : called");
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
Log.d(TAG,"Adding Menu");
dish p = dataSnapshot1.getValue(dish.class);
Log.d(TAG, "called : " + p);
Log.d(TAG, " : " + dataSnapshot + " : " + dataSnapshot1);
list.add(p);
}
getmenu();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, " Opps....Somethinf Went wrong!", Toast.LENGTH_LONG).show();
}
});
}
private void getmenu(){
Log.d(TAG,"Setting adapter");
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
RecyclerView recyclerMenuView = findViewById(R.id.recyclerMenuView);
recyclerMenuView.setLayoutManager(linearLayoutManager);
MenuAdapter adapter = new MenuAdapter(MainActivity.this, list);
recyclerMenuView.setAdapter(adapter);
}
}
RecyclerViewAdapter.java - Category recyclerview (Click on this item)
package com.example.myapplication;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
#NonNull
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mCategory = new ArrayList<>();
private Context context;
public RecyclerViewAdapter(Context context, ArrayList<String> category) {
this.context = context;
mCategory = category;
}
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG ,"On Create View Holder: called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Log.d(TAG, "OnBindViewHolder: called");
holder.category.setText(mCategory.get(position));
holder.category.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"onClick : Clicked on Text" + mCategory.get(position));
Toast.makeText(context, mCategory.get(position), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return mCategory.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView category;
public ViewHolder(#NonNull View itemView) {
super(itemView);
category = itemView.findViewById(R.id.category);
}
}
}
MenuAdapter.java - Scroll the item rom this recycler view
package com.example.myapplication;
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.menuViewHolder>{
Context context;
ArrayList<dish> dishes;
public MenuAdapter(Context c , ArrayList<dish> p)
{
context = c;
dishes = p;
}
#NonNull
#Override
public menuViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG ,"On Create View Holder: called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
return new MenuAdapter.menuViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull menuViewHolder holder, final int position) {
holder.dishname.setText(dishes.get(position).getDishname());
holder.dishrate.setText(dishes.get(position).getDishrate());
Picasso.get().load(dishes.get(position).getDishImage()).placeholder(R.drawable.ic_launcher_background).into(holder.dishImage);
boolean flag = dishes.get(position).getPermission();
if (flag){
Log.d(TAG," recommended : " + dishes.get(position).getDishname());
holder.recommended.setVisibility(View.VISIBLE);
}
else{
holder.recommended.setVisibility(View.INVISIBLE);
}
}
#Override
public int getItemCount() {
return dishes.size();
}
class menuViewHolder extends RecyclerView.ViewHolder {
TextView dishname, dishrate, recommended;
ImageView dishImage;
public menuViewHolder(#NonNull View itemView)
{
super(itemView);
dishname = (TextView) itemView.findViewById(R.id.dishname);
dishrate = (TextView) itemView.findViewById(R.id.dishrate);
dishImage = (ImageView) itemView.findViewById(R.id.dishImage);
recommended = (TextView) itemView.findViewById(R.id.recommended);
}
}
}
Try using RecyclerView's scrollTo(position) or smoothScrollTo(position)
I'm fetching RSS feed into my APP and trying to open an intent(Web browser) when a one of the news of recyclerView is clicked, but it dosn't work. and that's my code:
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);**
}
});
}
}
I did even try to put the same code in my holder class but in there the startActivity() method don't work:
the above code is kinda the same but in another class:
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);
}
}
}
this code:
FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
is the same code that I tried in two classes in the first the error is because of RecyclerView, And in the second one it gives error because startActivity don't work in that class.
So how do I activate the clicakble news that when clicked goes to the browser?
To start activity from within the adapter class use
context.startActivity(intent);
( if thats your question.)
My suggestion
Set the click listener in onBindViewHolder(..) of your adapter on holder.itemView.setOnClick....