我尝试从Firebase数据库中获取数据,但错误我说我无法转换为字符串。它工作正常,但我试图改变一些东西,它没有工作,我把应用程序置于其工作的初始位置。应用程序打开,但当我滚动它然后停止。将非常感谢帮助。
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;
}
}
显示的错误是这样的
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)
答案 0 :(得分:0)
因此您使用了Firebase回收器适配器。 让我告诉你它是如何工作的。您在firebase数据库中给出了一个名称LoadGroup,其引用被视为databasereference。 使用视图在回收器视图中显示时,它(LoadGroup)应该具有列表所在的另一个键值。 logcat中的这个错误没有指向任何代码,因此可能在该键值内,您可能有一些不需要的项目。 检查以下列表。我会举一个例子 --LoadGroup1addclose - unknown1 - 天 - 早上 - unknown2 - 天 --morning
LoadGroup2 - unknown1 - 天 - 早上 - unknown2 - 天 --morning
你应该有这样的东西,即物品是未知的钥匙。
所以如果这些未知中有不需要的物品。这会导致问题。 快乐的编码。