problem adding more than one recycle view - android

hello hope you doing well !
I have problem hope find the solution with you
I have myadpter2 java
package com.example.adopt_pet_app;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
public class myadapter2 extends FirebaseRecyclerAdapter<Adopters,myadapter2.myviewholder>
{
public myadapter2(#NonNull FirebaseRecyclerOptions<Adopters> options1) {
super(options1);
}
#Override
protected void onBindViewHolder(#NonNull myviewholder holder, int position, #NonNull Adopters model)
{
holder.adopname.setText(model.getAdoname());
holder.adoptphone.setText(model.getAdophone());
holder.adopemail.setText(model.getAdoemail());
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow2,parent,false);
return new myviewholder(view);
}
class myviewholder extends RecyclerView.ViewHolder
{
TextView adopname,adoptphone,adopemail;
public myviewholder(#NonNull View itemView)
{
super(itemView);
adopname=(TextView)itemView.findViewById(R.id.nametext);
adoptphone=(TextView)itemView.findViewById(R.id.phonetext);
adopemail=(TextView)itemView.findViewById(R.id.emailtext);
}
}
}
and I want call it here ..
package com.example.adopt_pet_app;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
public class Adopters_u_d extends AppCompatActivity {
RecyclerView recview2;
myadapter myadapter2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adopters_u_d);
recview2=(RecyclerView)findViewById(R.id.recview2);
recview2.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<Adopters> options1;
options1 = new FirebaseRecyclerOptions.Builder<Adopters>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Adopters"), Adopters.class)
.build();
myadapter2=new myadapter(options1);
recview2.setAdapter(myadapter2);
}
}
but it gave me error when i try call options1 it say this
its becouse i made one before for my pet page and now i want make another for my Adopter i dont want change petinfo becouse i need it its same code but with name options instated options1 please help thank you

Related

Data not fetching in card view while retrieving data from firebase

I am trying to fetch database from firebase but i cant get any text in cardview. Below is the xml file of card view.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/colorAccent"
>
<TextView
android:id="#+id/viewname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textColor="#F44336"
android:textSize="132dp" />
<TextView
android:id="#+id/viewgenere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre"
android:textSize="32sp" />
</LinearLayout>
Below is the activity file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewUser">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is the activity file
package com.example.firebasedatabase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import com.example.firebasedatabase.Adapter.UserAdapter;
import com.example.firebasedatabase.Model.Showuser;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class ViewUser extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference userdatabase;
UserAdapter userAdapter;
private List<Showuser>listData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_user);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listData=new ArrayList<>();
userdatabase= FirebaseDatabase.getInstance().getReference("users");
userdatabase.keepSynced(true);
userdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot npsnapshot : dataSnapshot.getChildren()){
Showuser showuser=npsnapshot.getValue(Showuser.class);
listData.add(showuser);
}
userAdapter=new UserAdapter(listData);
recyclerView.setAdapter(userAdapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Below is the recycleradapter
package com.example.firebasedatabase.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.firebasedatabase.Model.Showuser;
import com.example.firebasedatabase.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import java.util.List;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder>{
Context context;
private List<Showuser>listData;
public UserAdapter(Context context,List<Showuser>listData)
{
this.context=context;
this.listData=listData;
}
public UserAdapter(List<Showuser> listData) {
}
//
//
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.usercard,parent,false);
// return new ViewHolder(view) {
//
// };
return new ViewHolder(LayoutInflater.from(context)
.inflate(R.layout.usercard,parent,false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.txtuser.setText(new StringBuilder(listData.get(position).getShowuserName()));
holder.txtgenre.setText(new StringBuilder(listData.get(position).getShowuserGenre()));
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtuser,txtgenre;
public ViewHolder(#NonNull View itemView) {
super(itemView);
txtuser=(TextView)itemView.findViewById(R.id.viewname);
txtgenre=(TextView)itemView.findViewById(R.id.viewgenere);
}
}
}
I cannot view any text coming from firebase database.The firebase database rules are true.The view is created; just as i enter 4 data in firebase then it shows 4 card view but no text is shown and there is space between 1 card & 2 card near about 4 blank rows.
please help me out
I see the issue.
userAdapter=new UserAdapter(listData);
recyclerView.setAdapter(userAdapter);
Here you are initializing your UserAdapter. Notice that you are calling the wrong constructor or perhaps you have not implemented it.
// You should call this instead
public UserAdapter(Context context,List<Showuser>listData)
{
this.context=context;
this.listData=listData;
}
// You are calling this empty constructor
public UserAdapter(List<Showuser> listData) {
}
You should initialize your adapter like below.
userAdapter=new UserAdapter(ViewUser.this, listData);
You miss setting the list in adapter constructor, so change below
public UserAdapter(List<Showuser> listData) {
}
With
public UserAdapter(List<Showuser> listData) {
this.listData=listData;
}
Also, the list might be null, so change the return value of getItemCount()
#Override
public int getItemCount() {
return listData.size();
}
with
#Override
public int getItemCount() {
return listData == null? 0: listData.size();
}

How to successfully create a dialog fragment with recyclerview?

'Here is my adapter class code and when i long pressed on an item it should show a alert dialog box to delete that item ..I want to call DialogFragmnent from recycled view. How to resolve this show method from Recyclerview adapter. How is it possible?
Adaptorclass
package com.example.recyclerview1;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.LinkedList;
import androidx.appcompat.app.AppCompatActivity;
public class MyAdaptor extends RecyclerView.Adapter<MyAdaptor.MyViewHolder>{
ArrayList<Student> l;
Context c;
public MyAdaptor (Context ctx,ArrayList<Student> list)
{
l=list;
c =ctx;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
strong textMyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
final Student s = l.get(position);
holder.t1.setText(s.getName());
holder.t2.setText(s.getCnic());
int currentposition=position;
final Student infodata= l.get(position);
holder.linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view)
{
Dialogclass dialog = new Dialogclass();
dialog.show(((AppCompatActivity)c).getSupportFragmentManager(), "Fragment");
removieitem(infodata);
Toast.makeText(c," Removed item "+position,Toast.LENGTH_LONG).show();
return false;
}
});
holder.linearLayout.setOnClickListener(new View.OnClickListener () {
#Override
public void onClick(View view)
{
Toast.makeText(c,s.getName()+" "+s.getCnic(),Toast.LENGTH_LONG).show();
}
});
}
private void removieitem(Student infodata) {
int position=l.indexOf(infodata);
l.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return l.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView t1,t2;
LinearLayout linearLayout;
public MyViewHolder(#NonNull View itemView)
{
super(itemView);
t1 = itemView.findViewById(R.id.txtname);
t2 = itemView.findViewById(R.id.txtcnic);
linearLayout = itemView.findViewById(R.id.mylayout);
}
}
}
Dialogclass
package com.example.recyclerview1;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;
public class Dialogclass extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
It show the Error as....java.lang.ClassCastException: android.app.Application cannot be cast to androidx.appcompat.app.AppCompatActivity
at com.example.recyclerview1.MyAdaptor$1.onLongClick(MyAdaptor.java:49)
MainActivity.java
package com.example.recyclerview1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Student> list= list = new ArrayList<>();
RecyclerView rc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rc = findViewById(R.id.myrec);
Student s1 = new Student("Ahmed","34198723482");
Student s2 = new Student("Ahmed","34198723482");
Student s3 = new Student("Ahmed","34198723482");
Student s4 = new Student("Ahmed","34198723482");
Student s5 = new Student("Ahmed","34198723482");
Student s6 = new Student("Ahmed","34198723482");
Student s7 = new Student("Ahmed","34198723482");
Student s8 = new Student("Ahmed","34198723482");
Student s9 = new Student("Ahmed","34198723482");
Student s0 = new Student("Ahmed","34198723482");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
list.add(s7);
list.add(s8);
list.add(s9);
list.add(s0);
MyAdaptor myAdaptor = new MyAdaptor(getApplicationContext(),list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rc.setLayoutManager(linearLayoutManager);
rc.setAdapter(myAdaptor);
}
}
You can create custom dialog.
Add RecyclerView in your XML file layout if don't have any layout create it.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fl_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="300dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Then initialize your RecyclerView and any other view in onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//TODO Change 'frag_mdialog' to your custom view XML layout
View v = inflater.inflate(R.layout.frag_mdialog, container, false);
view = v;
// Do all the stuff to initialize your custom view
RecyclerView recyclerView = view.findViewById(R.id.rv_dialog);
return v;
}
I hope this help you.
Problem at line below
dialog.show(((AppCompatActivity)c).getSupportFragmentManager()
c is Context so cannot be cast to AppCombatActivity
Try Sending Activity object in MyAdaptor class like below
public MyAdaptor (Activity ctx,ArrayList<Student> list)
and In MainActivity.java class you need to pass Activity object like this below -
MyAdaptor myAdaptor = new MyAdaptor(MainActivity.this,list);

RecyclerView not showing items inside view pager fragment

When I click the chat tab the recycler view doesn't show the items inside the fragment. It was working before when I had it in a activity. Now it doesnt seem to show the items when running the app. I have tried debugging and the likes and still can't seem to find out the problem. Any help will be grateful, thanks!
ChatFragment.java
package com.swampass.nauticalapp;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.swampass.nauticalapp.model.ActiveChatConvo;
import com.swampass.nauticalapp.model.User;
import java.util.ArrayList;
public class ChatsFragment extends Fragment {
private RecyclerView recyclerView;
private DatabaseReference mRef;
private LinearLayoutManager mLinearLayoutManager;
private ArrayList<User> users;
private FirebaseAuth mAuth;
private ActiveChatConvo adapter;
public ChatsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View penis = inflater.inflate(R.layout.fragment_chats, container, false);
// Inflate the layout for this fragment
mRef = FirebaseDatabase.getInstance().getReference("Users");
mRef.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
users = new ArrayList<>();
adapter = new ActiveChatConvo(users,getContext());
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
String name = postSnapshot.child("Name").getValue(String.class);
String email = postSnapshot.child("Email").getValue(String.class);
String pic = postSnapshot.child("image").getValue(String.class);
users.add(new User(name,email,pic));
}
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Recycler View
recyclerView = (RecyclerView) penis.findViewById(R.id.active_chats);
//ActiveChatConvo adapter = new ActiveChatConvo(users,this);
Context c = getContext();
mLinearLayoutManager = new LinearLayoutManager(getActivity());
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
recyclerView.setAdapter(adapter);
//adapter.notifyDataSetChanged();
return inflater.inflate(R.layout.fragment_chats, container, false);
}
}
ActiveChatConvo.java
package com.swampass.nauticalapp.model;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import com.swampass.nauticalapp.ChatConversationActivity;
import com.swampass.nauticalapp.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Peter on 6/6/2017.
*/
public class ActiveChatConvo extends RecyclerView.Adapter<ActiveChatConvo.ViewHolder> {
private ArrayList<User> mUsers;
private Context mContext;
public ActiveChatConvo(ArrayList<User> users, Context dick) {
mUsers = users;
mContext = dick;
}
private Context getContext() {
return mContext;
}
#Override
public ActiveChatConvo.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.chat_single_item, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Get the data model based on position
User user = mUsers.get(position);
// Set item views based on your views and data model
TextView name = holder.mItemName;
name.setText(user.getName());
TextView description = holder.mItemDescription;
description.setText(user.getEmail());
ImageView pic = holder.mItemImage;
Picasso.with(pic.getContext()).load(Uri.parse(user.getPic())).into(pic);
}
#Override
public int getItemCount() {
return mUsers.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mItemImage;
private TextView mItemName;
private TextView mItemDescription;
private LinearLayout layout;
final LinearLayout.LayoutParams params;
public ViewHolder(View v) {
super(v);
mItemImage = (ImageView) v.findViewById(R.id.chat_persion_image);
mItemName = (TextView) v.findViewById(R.id.chat_persion_name);
mItemDescription = (TextView) v.findViewById(R.id.chat_persion_email);
layout = (LinearLayout) itemView.findViewById(R.id.show_chat_single_item_layout);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Context context = itemView.getContext();
Intent intent = new Intent(context, ChatConversationActivity.class);
// intent.putExtra("image_id", );
intent.putExtra("descripion", mItemDescription.getText().toString());
intent.putExtra("name", mItemName.getText().toString());
context.startActivity(intent);
}
}
}
fragment_chat.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.swampass.nauticalapp.ChatsFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="#+id/active_chats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorAccent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
In the ChatFragment.java, you should be returning the view you have created - penis, and not inflater.inflate(R.layout.fragment_chats, container, false). In this case, an empty view is returned and not the one you are creating.

How to add a second layout in RecyclerView?

I've got some problem with my RecyclerView. I want to add a bubble widget that creates a new circle with message (just like in messanger). Problem is that bubble must be placed in separate XML layout file so I cannot add it to the "item" layout that I have in my adapter. Inside bubble layout I have simply TextView, however I don't want to display normal text - it would be too easy ;) I want to display data I get from API. Another words - I need access to TextView that is inside Bubble layout in my adapter. Please, help! :)
Here's my Adapter class
package pl.krakowskascenamuzyczna.ksmcalendar.Adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.w3c.dom.Text;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.txusballesteros.bubbles.BubbleLayout;
import com.txusballesteros.bubbles.BubblesManager;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import pl.krakowskascenamuzyczna.ksmcalendar.Activities.CalendarActivity;
import pl.krakowskascenamuzyczna.ksmcalendar.Activities.DisplayConcertActivity;
import pl.krakowskascenamuzyczna.ksmcalendar.MySingleton;
import pl.krakowskascenamuzyczna.ksmcalendar.R;
import pl.krakowskascenamuzyczna.ksmcalendar.data.Concert;
import pl.krakowskascenamuzyczna.ksmcalendar.data.Thumbnail;
public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.MyViewHolder> {
private LayoutInflater inflater;
private Context context;
NetworkImageView mNetworkImageView;
private MySingleton mySingleton;
private List<Concert> concertList = new ArrayList();
public ConcertAdapter(Context context, List<Concert> concerts) {
this.inflater = LayoutInflater.from(context);
this.concertList = concerts;
this.context = context;
}
public void setListConcert(ArrayList<Concert> concertList) {
this.concertList = concertList;
notifyItemRangeChanged(0, concertList.size());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.concert_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final float screenWidthPx = holder.itemView.getResources().getDisplayMetrics().widthPixels;
Concert current = concertList.get(position);
Log.d("mLog", current.getUrl());
holder.image.setImageUrl(current.getUrl(), MySingleton.getInstance().getImageLoader());
holder.image.getLayoutParams().height = (int) (screenWidthPx * 0.75);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date concertDate = new Date();
try {
concertDate = format.parse(current.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
DateTime dt = new DateTime();
DateTime currentDate = dt.withZone(DateTimeZone.forID("Europe/Warsaw"));
int days = Days.daysBetween(new DateTime(currentDate), new DateTime(concertDate)).getDays();
holder.date_btn.setText(String.valueOf(days));
}
#Override
public int getItemCount () {
return concertList.size();
}
public void showDisplay(int position){
Intent intent = new Intent(context, DisplayConcertActivity.class);
intent.putExtra("position", position);
intent.putExtra("content", concertList.get(position).getContent());
intent.putExtra("date", concertList.get(position).getDate());
intent.putExtra("url", concertList.get(position).getUrl());
context.startActivity(intent);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private NetworkImageView image;
private Button date_btn;
public MyViewHolder(View itemView) {
super(itemView);
image = (NetworkImageView) itemView.findViewById(R.id.concerts_niv);
date_btn = (Button) itemView.findViewById(R.id.date_btn);
image.setOnClickListener(this);
}
#Override
public void onClick(View v) {showDisplay(getAdapterPosition());
}
}
}
And here's my Bubble layout:
<com.txusballesteros.bubbles.BubbleLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/bubble_tv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/profile_decorator"
android:scaleType="centerCrop"
android:src="#drawable/profile_decorator"
android:textColor="#fff"
/>
</com.txusballesteros.bubbles.BubbleLayout>
You can pass your bubble TextView as aprameter to the adapter constructor.
private TextView mBubbleTextView;
public ConcertAdapter(Context context, List<Concert> concerts, TextView bubbleTextView) {
this.inflater = LayoutInflater.from(context);
this.concertList = concerts;
this.context = context;
this.mBubbleTextView = bubbleTextView;
}
Now you have the bubble TextView in your adapter, so can update it from any method.

how to call RecyclerView in the first fragment of the navigation drawer? I've followed a tutorial by slidenerd but getting one error

This is the Java file of the fragment I wish to insert recycler view. Please dont worry about the package name which I deleted for some reasons.
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class Dashboard extends Fragment {
private RecyclerView recyclerView;
private RecyclerAdapter adapter;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static Dashboard newInstance() {
Dashboard fragment = new Dashboard();
return fragment;
}
public Dashboard () {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_dashboard, container,
false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.drawerlist);
adapter=new RecyclerAdapter(getActivity().getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
public static List<Information> getData(){
List<Information> data=new ArrayList<>();
String[] titles={"Rooms Occupied","RoomsVacant","Check-In","Check-Out","Extensions","Confirmations","Cancellations"};
for(int i=0;i<titles.length;i++)
{
Information current =new Information();
current.title=titles[i];
data.add(current);
}
return data;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
This is my Adapter class
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by gowtham on 6/13/2015.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<Information> data= Collections.emptyList();
public RecyclerAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data=data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_row, parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current=data.get(position);
holder.title.setText(current.title);
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.viewText);
}
}
}
This is Layout file of the fragment in which I want to use the recycler view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewImage"
android:background="#drawable/ic_dashboard"
android:layout_gravity="center_vertical"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/viewText"
android:text="Dummy Text"
android:textStyle="bold"
android:layout_gravity="left"
android:gravity="center" />
</LinearLayout>
Since I'm new to stackoverflow I don't have enough reputations to add image. So I will attach the screenshot in the comments below. Please take a look at it to know what is happening here exactly. Thanks!
Why you call getActivity().getData(), just call getData().
And in your Adapter class:
#Override
public int getItemCount() {
return data.size();
}
It can't just return zero, you should return the data size.

Categories

Resources