rishav rishav - 22 days ago 6
Android Question

setting up firebase recycler view gives an error of cannot cast to string

i tried to fetch data from firebase database but the error i saying that i cannot cast to string.It was working fine but i tried to change something and it didnot work and i placed app in initial position where it was working. App turns on but when i scroll it then it stops. Help will be greatly appriciated

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
















}
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public DatabaseReference databaseReference;
public RecyclerView recyclerView;
int tabNumber;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


tabNumber = getArguments().getInt(ARG_SECTION_NUMBER);
databaseReference = FirebaseDatabase.getInstance().getReference();
View rootView = inflater.inflate(R.layout.loadfragment, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mainDisplay();
return rootView;
}

private void mainDisplay() {



getTotalList();

}
public void getTotalList() {

FirebaseRecyclerAdapter<mymodel, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<mymodel, BlogViewHolder>(
mymodel.class, R.layout.row, BlogViewHolder.class, getDatabaseReference()
) {


@Override
protected void populateViewHolder(BlogViewHolder viewHolder, mymodel model, int position) {

viewHolder.setMorning(model.getMorning(),getDay(position));
viewHolder.setEvening(model.getEvening(),getDay(position));
viewHolder.setDay(getDay(position));
viewHolder.setTotal(model.getTotal(),getDay(position));



}

};

recyclerView.setAdapter(firebaseRecyclerAdapter);


}

private String getDay(int position) {
String[] dayName={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
return dayName[position];

}


private DatabaseReference getDatabaseReference() {
DatabaseReference databaseReferenceGroup=databaseReference.child("LoadGroup"+tabNumber);
databaseReferenceGroup.keepSynced(true);
return databaseReferenceGroup;

}


/////////////////////////////////////////
public static class BlogViewHolder extends RecyclerView.ViewHolder
{
public View mview;
Calendar calendar = Calendar.getInstance();
int highlightDay = calendar.get(Calendar.DAY_OF_WEEK);
String[] dayName={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};



public BlogViewHolder(View itemView) {
super(itemView);
mview=itemView;
}


public void setMorning(String morning,String day)
{

TextView morningText= (TextView) mview.findViewById(R.id.mytext1);
if(dayName[highlightDay-1]==day)
{
morningText.setTextColor(Color.RED);
}
morningText.setText(morning);
}
public void setEvening(String evening,String day)
{
TextView eveningText= (TextView) mview.findViewById(R.id.mytext2);

if(dayName[highlightDay-1]==day)
{
eveningText.setTextColor(Color.RED);
}

eveningText.setText(evening);
}
public void setTotal(String total,String day)
{
TextView dayname= (TextView) mview.findViewById(R.id.totaltext);
if(dayName[highlightDay-1]==day)
{
dayname.setTextColor(Color.RED);
}

dayname.setText(total+" hours");



}




public void setDay(String day)
{



TextView dayname= (TextView) mview.findViewById(R.id.dayname);
if(dayName[highlightDay-1]==day)
{
dayname.setTextColor(Color.RED);
}

dayname.setText(day);
}

}}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
// Show 3 total pages.
return 7;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "GROUP 1";
case 1:
return "GROUP 2";
case 2:
return "GROUP 3";
case 3:
return "GROUP 4";
case 4:
return "GROUP 5";
case 5:
return "GROUP 6";
case 6:
return "GROUP 7";

}
return null;
}}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.smsSchedule) {

// AlertDialog.Builder alert= new AlertDialog.Builder(MainActivity.this);
// alert.setIcon(R.drawable.com_facebook_button_icon);
// alert.setCancelable(true);
// alert.setTitle("Share shedule by sms");
// final EditText input = new EditText(MainActivity.this);
// LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.MATCH_PARENT,
// LinearLayout.LayoutParams.MATCH_PARENT);
// input.setLayoutParams(lp);
// alert.setView(input);
// input.setInputType(InputType.TYPE_CLASS_NUMBER);
// alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
//
//int groupNum= Integer.valueOf(input.getText().toString());
// // String smsText=mylist.toString();
// Intent sendIntent = new Intent(Intent.ACTION_VIEW);
// sendIntent.setData(Uri.parse("sms:"));
// // sendIntent.putExtra("sms_body", smsText);
// startActivity(sendIntent);
//
//
//
// }
// });
// alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
// }
// });
// alert.show();
//
//





























} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}


the error that is shown is like this

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.shailendra.easyloadshedding.mymodel
at com.google.android.gms.internal.zzalq.zzd(Unknown Source)
at com.google.android.gms.internal.zzalq.zzb(Unknown Source)
at com.google.android.gms.internal.zzalq.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5822)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5855)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5091)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4967)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)
at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1596)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2704)
at android.view.View.dispatchTouchEvent(View.java:9998)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2828)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2499)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2831)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1863)
at android.app.Activity.dispatchTouchEvent(Activity.java:3046)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2792)
at android.view.View.dispatchPointerEvent(View.java:10233)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImp
11-17 15:50:32.173 7788-7788/com.example.shailendra.easyloadshedding D/AndroidRuntime: Shutting down VM
11-17 15:50:32.173 7788-7788/com.example.shailendra.easyloadshedding E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shailendra.easyloadshedding, PID: 7788
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.shailendra.easyloadshedding.mymodel
at com.google.android.gms.internal.zzalq.zzd(Unknown Source)
at com.google.android.gms.internal.zzalq.zzb(Unknown Source)
at com.google.android.gms.internal.zzalq.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5822)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5855)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5091)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4967)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)
at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1596)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2704)
at android.view.View.dispatchTouchEvent(View.java:9998)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2828)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2499)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)

Answer

So you have used a Firebase recycler adapter. Let me tell you how it works. You have given a name LoadGroup in firebase database whose reference is taken as databasereference. While displaying in a recycler view using a viewholder it (LoadGroup) should have another key value inside which the list exists. This error in logcat is not pointing to any code so may be inside that key value, you may have some unwanted items. Check the below list. I will show an example --LoadGroup1addclose -- unknown1 --day --morning -- unknown2 --day --morning

LoadGroup2 -- unknown1 --day --morning -- unknown2 --day --morning

You should have something like this i.e. the items are in unknown key.

So if there are unwanted items inside these unknown. This causes a problem. Happy coding.