I'm trying to get some data from my firebase, so I created recycleview in activity to get the data, and it worked without problem.
But now, I added my recycle to a Fragment to get data, and it managed to get the data but I can't see it, the firenase (on success) tell me that it work, but I can't see the recycleview.
I searched more and more without finding any solution, there are another one have the same problem, and he didn't find any solution else, here .
RecyclerView doesn't appear in Fragment
I tried all the solution in this question but, it didn't work, now I spent more than two months without finding out the problem.
FriendsFragment
public class FriendsFragment extends Fragment {
DatabaseReference databaseReference;
RecyclerView recyclerView;
RecyclerView.Adapter adapter ;
ProgressDialog progressDialog;
List<ImageUploadInfo> list = new ArrayList<>();
private Context context;
public FriendsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(com.wanjy.dannie.rivchat.R.layout.fragment_people, container, false);
context = view.getContext();
recyclerView = (RecyclerView) view.findViewById(R.id.recycleListFriend);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading Images From Firebase.");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference("All_Image_Uploads_Database");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
list.add(imageUploadInfo);
}
adapter = new RecyclerViewAdapter(context, list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Hiding the progress dialog.
progressDialog.dismiss();
}
});
return inflater.inflate(R.layout.fragment_people, container, false);
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<ImageUploadInfo> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<ImageUploadInfo> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);
holder.imageNameTextView.setText(UploadInfo.getImageName());
//Loading image from Glide library.
Glide.with(context).load(UploadInfo.getImageURL()).into(holder.imageView);
}
#Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView imageNameTextView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageNameTextView = (TextView) itemView.findViewById(R.id.ImageNameTextView);
}
}
}
ImageUploadInfo
public class ImageUploadInfo {
public String imageName;
public String imageURL;
public ImageUploadInfo() {
}
public ImageUploadInfo(String name, String url) {
this.imageName = name;
this.imageURL= url;
}
public String getImageName() {
return imageName;
}
public String getImageURL() {
return imageURL;
}
}
recyclerview_items
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="5dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF"
android:padding="1dp">
<ImageView
android:layout_height="50dp"
android:layout_width="50dp"
android:src="#android:drawable/ic_delete"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:padding="3dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000000"
android:text="أخبار الدولة"
android:gravity="center"
android:id="#+id/myname"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="التاريخ"
android:id="#+id/date"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:src="#mipmap/ic_launcher"
android:background="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:layout_weight="1.0"
android:textSize="14sp"
android:textColor="#000000"
android:layout_gravity="right"
android:padding="4dp"
android:gravity="right"
android:id="#+id/ImageNameTextView"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF"
android:padding="2dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:gravity="center">
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:src="#android:drawable/ic_delete"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:textColor="#000000"
android:padding="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:gravity="center">
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:src="#android:drawable/ic_delete"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:textColor="#000000"
android:padding="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:gravity="center">
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:src="#android:drawable/ic_delete"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:textColor="#000000"
android:padding="2dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
fragment_people
<RelativeLayout 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.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleListFriend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
My build gradle
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.firebase:firebase-core:9.8.0'
compile 'com.google.firebase:firebase-database:9.8.0'
compile 'com.google.firebase:firebase-auth:9.8.0'
testCompile 'junit:junit:4.12'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.yarolegovich:lovely-dialog:1.0.4'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:percent:25.0.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
Note My recycle work in Activity without problem, but it doesn't appear in my fragment .
I hope someone find the problem in my code.
Can you make any view show in the fragment? I noticed your MyFragment class is initializing fragment_people.xml but your xml class is named Fragment.xml unless that is a typo. Verify you can show any type of view in your fragment before thinking it is a recycler view issue.
Also, in your adapter, you are using an item layout with name recyclerview_items.xml but you have the class named Recycle item unless that is a typo as well.
It work now, I just changed the fragment to this
public class FriendsFragment extends Fragment {
DatabaseReference databaseReference;
RecyclerView recyclerView;
RecyclerView.Adapter adapter ;
ProgressDialog progressDialog;
List<ImageUploadInfo> list = new ArrayList<>();
private Context context;
public FriendsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(com.wanjy.dannie.rivchat.R.layout.fragment_people, container, false);
context = view.getContext();
return inflater.inflate(R.layout.fragment_people, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
recyclerView = (RecyclerView) view.findViewById(R.id.recycleListFriend);
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading Images From Firebase.");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference("All_Image_Uploads_Database");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
list.add(imageUploadInfo);
}
adapter = new RecyclerViewAdapter(context, list);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Hiding the progress dialog.
progressDialog.dismiss();
}
});
super.onViewCreated(view, savedInstanceState);
}
}
I don't what was the problem but, I goggled for an example, and I found someone use method oncreateview
So I did like him , and I moved this line
recyclerView.setLayoutManager(new LinearLayoutManager(context));
to under this line
recyclerView.setAdapter(adapter);
Now it work without problem.
Related
I tried a lot of solutions but no solution has worked for me.
I am getting data from the server and showing it inside a recycleview. The data shows fine but shows this error in the logcat
Access denied finding property "persist.vendor.log.tel_dbg"
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
sometimes when I refresh again and again the recyclerview becomes empty.
I am using viewpager consisting of 2 fragments inside a parent fragment.
this recycleview is using in a nested scrollview
some time data not showing
Code snippet:
homebuyer_adapter_recycler=new homebuyer_adapter_recycle(getActivity(), items);
LinearLayoutManager home = new LinearLayoutManager(getActivity());
home.setOrientation(LinearLayoutManager.VERTICAL);
allitemsgrid.setLayoutManager(home);
allitemsgrid.setAdapter(r);
Here is more about
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,
final int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.items_view,parent,
false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(v);
HERE IS MY item 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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:cardCornerRadius="6dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:id="#+id/image_items"
android:scaleType="fitXY"
/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:background="#drawable/blackshade"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/items_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:fontFamily="sans-serif-smallcaps"
android:paddingLeft="20dp"
android:text="Message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Timing"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:fontFamily="#font/mylight"
android:layout_alignParentRight="true"
android:textSize="10dp"
android:id="#+id/shoptimming"/>
</RelativeLayout>
<TextView
android:id="#+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:fontFamily="#font/arimo_bold"
android:paddingLeft="20dp"
android:text="Name"
android:textColor="#color/black"
android:textSize="17dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/minumum_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:drawablePadding="10dp"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Minimum " />
<TextView
android:id="#+id/price_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:gravity="left"
android:paddingLeft="20dp"
android:text="charges"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is my adapter class
public class homebuyer_adapter_recycle extends RecyclerView.Adapter<homebuyer_adapter_recycle.ViewHolder> {
private ArrayList<seller_information> listData;
private LayoutInflater layoutInflater;
int lastpostition=-1;
Context context;
public homebuyer_adapter_recycle(Context aContext, ArrayList<seller_information> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder
(#NonNull ViewGroup parent, final
int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.items_view,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
Log.i("inadapter","calling time");
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phone_number_shop = listData.get(viewType).getPhonenumber();
homebuyer.Delivery_charges_shop =
listData.get(viewType).getDiliveryfee();
homebuyer.Shop_name = listData.get(viewType).getShopname();
homebuyer.minimum_order = listData.get(viewType).getMinorder();
//profession=items.get(i).getName();
// Toast.makeText(getActivity(),phn,Toast.LENGTH_SHORT).show();
all_and_cetegory_items items = new all_and_cetegory_items();
Bundle b = new Bundle();
b.putString("phone",homebuyer.phone_number_shop);
items.setArguments(b);
FragmentTransaction fragmentTransaction =
((AppCompatActivity)context).getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, items);
fragmentTransaction.addToBackStack(null).commit();
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(listData.get(position).getShopname());
//
Picasso.with(context).load(listData.get(position)
.getShopimage()).into(holder.shopimage);
Glide.with(context).load(listData.get(position).getShopimage())
.into(holder.shopimage);
holder.dilivery.setText("Rs "+listData.get(position).getDiliveryfee()+"
Delivery fee");
holder.minorder.setText("Rs "+listData.get(position).getMinorder()+" minimum");
holder.items.setText(listData.get(position).getShopmessage());
holder.time.setText("Service Available
"+listData.get(position).getStartingtime()+" to
"+listData.get(position).getEndingtime());
Log.i("inadapter","calling time"+listData.get(position).getShopname());
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView dilivery;
TextView items;
TextView minorder;
TextView time;
ImageView shopimage;
public ViewHolder(#NonNull View v) {
super(v);
name = (TextView) v.findViewById(R.id.name_item);
dilivery = (TextView) v.findViewById(R.id.price_item);
minorder = (TextView) v.findViewById(R.id.minumum_order);
items=(TextView)v.findViewById(R.id.items_all);
time=(TextView)v.findViewById(R.id.shoptimming);
shopimage=(ImageView) v.findViewById(R.id.image_items);
}
}
}
In error it is saying that the Adapter for RecylerView is not attached. So, try to add adapter to the layout using:
recyclerView.setAdapter(categoryAdapter);
I am assuming homebuyer_adapter_recycler is your adapter.
According to that, you haven't really set your adapter as the logcat is specifying.
Add the below code instead of allitemsgrid.setAdapter(r);
allitemsgrid.setAdapter(homebuyer_adapter_recycler);
If my assumption or suggestion is wrong, it is okay. It is probably because your question is not very clear and does not provide the necessary details. Please provide more details such as the adapter class code and what is allitemsgrid, homebuyer_adapter_recycler and r.
I'm new to android development, and I'm having issues with my app crashing when I'm trying to start it
This is the error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cybermath/com.example.cybermath.modules.MainActivity}: android.view.InflateException: Binary XML file line #25: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
And this is the XML file for the data within recycler viewer:
'''
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:gravity="center_vertical"
android:background="#color/colorPrimaryDark"
>
<TextView
android:id="#+id/account_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="70"
android:background="#color/colorPrimary"
android:lines="1"
android:padding="10dp"
android:text="#string/app_name"
android:textColor="#color/colorText" >
</TextView>
<TextView
android:id="#+id/account_progress"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="#color/colorPrimary"
android:text="Enter"
android:textSize="12sp"
android:textColor="#color/colorText"
>
</TextView>
'''
This is the XML for the MainActivity:
<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="MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="416dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/informationView"
android:id="#+id/recyclerView">
</androidx.recyclerview.widget.RecyclerView>
<!--<TextView
android:id="#+id/informationView"
android:layout_width="408dp"
android:layout_height="50dp"
android:foregroundTint="#00050000"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:text="Choose an account"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>-->
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the code for the MainActivity:
'''
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
// UI
private RecyclerView mRecyclerView;
// Variables
#NonNull
private ArrayList<Account> mAccounts = new ArrayList<>();
private AccountsRecyclerAdapter mAccountsRecyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_account_list);
mRecyclerView = findViewById(R.id.recyclerView);
initRecyclerView();
insertFakeAccounts();
}
private void insertFakeAccounts(){
for(int i = 0; i < 1000; i++){
Account account = new Account();
account.setName("title #" + i);
mAccounts.add(account);
}
mAccountsRecyclerAdapter.notifyDataSetChanged();
}
private void initRecyclerView(){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mAccountsRecyclerAdapter = new AccountsRecyclerAdapter(mAccounts);
mRecyclerView.setAdapter(mAccountsRecyclerAdapter);
}
'''
And my adapter public class AccountsRecyclerAdapter extends
RecyclerView.Adapter<AccountsRecyclerAdapter.ViewHolder> {
private ArrayList<Account> mAccounts = new ArrayList<>();
public AccountsRecyclerAdapter(ArrayList<Account> accounts) {
this.mAccounts = accounts;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_account_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull AccountsRecyclerAdapter.ViewHolder viewHolder, int i) {
viewHolder.name.setText(mAccounts.get(i).getName());
}
#Override
public int getItemCount() {
return mAccounts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.account_name);
}
}
Some if the thing are not as per requirement may cause an exception. Check for condition as
if(yourValue !=null && yourValue.equals("desired value"){
//do your stuff
}
I couldnt able to display my data from firebase to my listview in android application. Rather it only shows one data.
Heres the current data of my database:
Database
And this is my java:
public class scheduleList extends ArrayAdapter<Schedule> {
private Activity context;
private List<Schedule> scheduleList;
public scheduleList(Activity context, List<Schedule> scheduleList){
super(context, R.layout.listlayout, scheduleList);
this.context=context;
this.scheduleList=scheduleList;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listviewItem = inflater.inflate(R.layout.listlayout, null, true);
TextView textViewName = (TextView) listviewItem.findViewById(R.id.textViewName);
TextView textViewSchedule = (TextView) listviewItem.findViewById(R.id.textViewSchedule);
Schedule schedule = scheduleList.get(position);
textViewName.setText(schedule.getAppointer());
textViewSchedule.setText(schedule.getAppointment_schedule());
return listviewItem;
}}
My java file:
public class Appointment extends AppCompatActivity {
CalendarView calendarView;
TextView myDate;
private Button btn1;
ListView listViewSchedule;
List<Schedule> scheduleList;
DatabaseReference databaseAppointments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appointment);
databaseAppointments = FirebaseDatabase.getInstance().getReference("appointment");
calendarView = (CalendarView) findViewById(R.id.calendarView);
myDate = (TextView) findViewById(R.id.myDate);
listViewSchedule=(ListView) findViewById(R.id.listViewSchedule);
scheduleList = new ArrayList<>();
btn1 = (Button) findViewById(R.id.appt);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Appointment.this, appointments2.class);
Appointment.this.startActivity(myIntent);
}
});
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int i, int i1, int i2) {
String date = (i1 + 1) + "/" + i2 + "/" + i;
myDate.setText(date);
}
});
}
#Override
protected void onStart() {
super.onStart();
databaseAppointments.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
scheduleList.clear();
for(DataSnapshot scheduleSnapshot: dataSnapshot.getChildren()){
Schedule schedule = scheduleSnapshot.getValue(Schedule.class);
scheduleList.add(schedule);
}
scheduleList adapter = new scheduleList(Appointment.this, scheduleList);
listViewSchedule.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}}
and lastly my xml file containing the listview:
<ScrollView 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="com.example.cs409.instappoint.Appointment">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/myDate"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select the date"
android:textColor="#ee912a"
android:textAlignment="center"
android:layout_marginStart="125dp"
android:layout_marginTop="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="380dp"
android:layout_marginStart="10dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<CalendarView
android:id="#+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="320dp">
</CalendarView>
</LinearLayout>
<View
android:layout_width="500dp"
android:layout_height="1dp"
android:layout_gravity="center"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="0dp"
android:background="#ee912a" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Appointments"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"/>
<ListView
android:id="#+id/listViewSchedule"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/appt"
android:layout_width="450dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:layout_marginTop="130dp"
android:background="#drawable/bg_calen"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Appoint now"
android:textColor="#fff" />
</LinearLayout>
Im guessing that the scrollview layout affects the listview but im not sure. Also the listview display whatevers the first on the list.
use FirebaseRecyclerAdapter. search example of how to use FirebaseRecylerAdapter with example.
I want to create dynamic views with x number of rows for each CardView that gets created. To help illustrate my idea this image shows what I essentially want to do.
For each CardView I want to be able to put any amount of TableRows into the CardView. So one CardView could have 4 rows whereas the next CardView could have 2 rows and so on.
EDIT: Implementation
Essentially this view is replicated several times, I would like to minimise having to write seperate layouts for each CardView with x amount of rows like in the Card Storage implemenation which is more reusable than what I currently have. As I hit the limit here, I have posted a pastebin of the current layout with rows that I have
https://pastebin.com/9RFDGhXv
Layout: Card Storage
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="#style/CardStyle">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/MainConstraints" >
<TableRow
android:id="#+id/row_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/card_title"
style="#style/Title"/>
</TableRow>
<TableRow
android:id="#+id/row_two"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#+id/row_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout>
<ImageView
android:id="#+id/drawable"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
style="#style/RowDrawableElement"
/>
<TextView
android:id="#+id/percentage_descriptor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/drawable"
android:text="#string/percentage_descriptor"
style="#style/RowTextElement" />
<TextView
android:id="#+id/percentage_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
style="#style/RowTextElement" />
</android.support.constraint.ConstraintLayout>
</TableRow>
<TableRow
android:id="#+id/row_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/row_two"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
style="?android:attr/progressBarStyleHorizontal"
/>
</TableRow>
<TableRow
android:id="#+id/row_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/row_three"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/text_used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<TextView
android:id="#+id/text_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
</TableRow>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Adapter
public class StorageAdapter extends RecyclerView.Adapter<StorageAdapter.MyViewHolder> {
private Context mContext;
private List<StorageObjects> mList;
public StorageAdapter(Context context, List<StorageObjects> list) {
this.mContext = context;
this.mList = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_storage, parent, false);
return new MyViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
StorageObjects storageObjects = mList.get(position);
long used = storageObjects.getUsed();
long total = storageObjects.getTotal();
holder.mProgress.setMax(0);
holder.mProgress.setProgress(0);
holder.mTitle.setText(storageObjects.getTitle());
MiscUtils.testDrawableIdentifier(mContext.getApplicationContext(), holder.mImageViews, storageObjects.getDrawables());
holder.mPercentage.setText(storageObjects.getPercentage() + mContext.getString(R.string.percentage));
holder.mProgress.setMax((int) (total/100000));
holder.mProgress.setProgress((int) (used/100000));
holder.mUsed.setText(MiscUtils.humanReadableByteSize(used));
holder.mTotal.setText(MiscUtils.humanReadableByteSize(total));
}
#Override
public int getItemCount() {
return mList.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.card_title) TextView mTitle;
#BindViews({R.id.drawable}) List<ImageView> mImageViews;
#BindView(R.id.percentage_text) TextView mPercentage;
#BindView(R.id.progress_bar) ProgressBar mProgress;
#BindView(R.id.text_used) TextView mUsed;
#BindView(R.id.text_total) TextView mTotal;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
Class Storage
public class Storage extends Fragment {
private Unbinder mUnbinder;
private List<StorageObjects> storageList;
private StorageAdapter adapter;
private RecyclerView.LayoutManager mLayoutManager;
#BindArray(R.array.storage_ic_images)
String mDrawables[];
#BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
public Storage() {
// Required empty public constructor
}
public static Storage newInstance() {
return new Storage();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_storage, container, false);
mUnbinder = ButterKnife.bind(this, view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
initRecyclerView();
}
#Override
public void onDestroy() {
super.onDestroy();
mUnbinder.unbind();
}
private void initRecyclerView() {
storageList = new ArrayList<>();
adapter = new StorageAdapter(getActivity(), storageList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager = ViewUtils.getLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, ViewUtils.dpToPx(
getActivity(), 10), true));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
prepareView();
}
#SuppressLint("SetTextI18n")
private void prepareView(){
StorageObjects storage
= new StorageObjects(
getActivity().getString(R.string.int_storage_title),
StorageUtils.internalUsed(),
StorageUtils.internalTotal(),
StorageUtils.internalPercentage(),
mDrawables[0]);
storageList.add(storage);
storage = new StorageObjects (
getActivity().getString(R.string.ext_storage_title),
StorageUtils.externalUsed(getActivity()),
StorageUtils.externalTotal(getActivity()),
StorageUtils.externalPercentage(getActivity()),
mDrawables[1]
);
storageList.add(storage);
adapter.notifyDataSetChanged();
}
}
If number of rows per card view is less then you can inflate edit text and add it to linear layout.
Note : It's a bad practice to add scroll view inside a scroll view
This is my Activity:
public String id;
public String passPhrase;
public ArrayList<SongCell> songs;
private RecyclerView recyclerView;
private MyAdapter myAdapter;
private RecyclerView.LayoutManager layoutManager;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_view_layout);
Context context = this.getApplicationContext();
Bundle stateData = getIntent().getExtras();
try
{
id = stateData.getString("id");
passPhrase = stateData.getString("passPhrase");
ArrayList data;
recyclerView = (RecyclerView) findViewById(R.id.song_list);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<SongCell>();
for (int i=0; i<5;i++)
{
data.add(new SongCell("Song "+i,"Artist "+i, null));
}
myAdapter = new MyAdapter(data);
recyclerView.setAdapter(myAdapter);
}
catch(Exception e)
{
Log.d("Error: ", e.toString());
}
}
card.xml
<android.support.v7.widget.CardView android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dp"
>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/song_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/song_name"
android:textSize="30sp"
android:text="Song Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/vote_button"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="#drawable/arrow" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Activity.xml
<?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"
android:background="#2d2d2d">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:layout_marginBottom="10dp"
android:background="#222222"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/featSongImage1"
android:contentDescription="#string/newsongimage"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/songname"
android:id="#+id/featSongName1" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/featSongButt1"
android:background="#drawable/arrow"
android:contentDescription="#string/votearrow" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="fill_horizontal"
android:gravity="fill_horizontal|center">
<android.support.v7.widget.RecyclerView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
And this is my custom adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private ArrayList<SongCell> data;
public MyAdapter(ArrayList<SongCell> dataI)
{
data = dataI;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.song_card,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewholder, int pos) {
viewholder.songName.setText(data.get(pos).getName());
viewholder.artist.setText(data.get(pos).getArtist());
viewholder.image.setImageBitmap(data.get(pos).getArtwork());
}
#Override
public int getItemCount() {
return 0;
}
#Override
public int getItemViewType(int position) {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView songName;
TextView artist;
ImageView image;
ImageButton voteButton;
public ViewHolder(View v)
{
super(v);
this.songName = (TextView) v.findViewById(R.id.song_name);
this.image = (ImageView) v.findViewById(R.id.song_photo);
this.voteButton = (ImageButton) v.findViewById(R.id.vote_button);
}
}
}
I have looked at a ton of guides on how to get this working but it still doesn't work. Anyone have any clue what's going on? I'm on version 25.0.1, and have all the modules imported. But it's not adding a single card into the layout.
I thought it would be difficult to debug such a big code you uploaded. But luckily my eye went on this line
#Override
public int getItemCount() {
return 0;
}
return 0; in adapter.
Your list size is always 0.
instead write this
#Override
public int getItemCount() {
return data.size();
}
EDIT-1:
You will also get null pointer exception here
viewholder.artist.setText(data.get(pos).getArtist());
Because you are not initializing the artist variable in ViewHolder class.
Edit-2
#Override
public int getItemCount() {
return 0;
}
you can remove this particular code from your adapter. Because overriding the methods of class that we don't use might sometimes result in errors that you can't even imagine.