0

當我嘗試從應用程序中檢索Firebase中的數據到RecyclerView和我的應用程序崩潰時,我在logcat中收到此消息。以下是我用於從Firebase數據庫檢索數據的Java代碼。應用程序顯示空白屏幕,當我在真實設備上打開它時,它一直崩潰。有沒有其他方法可以做到這一點?當從Firebase數據庫中檢索數據到Recyclerview時,應用程序崩潰

的Android 4.1之前,方法INT android.support.v7.widget.ListViewCompat.lookForSelectablePosition(INT, 布爾)將錯誤地重寫程序包私有方法 在android.widget.ListView

public class Home extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    FirebaseDatabase database; 
    DatabaseReference category; 
    TextView txtFullName; 

    RecyclerView recycler_menu; 
    RecyclerView.LayoutManager layoutManager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("Menu"); 

     setSupportActionBar(toolbar); 

     //init firebase 
     database=FirebaseDatabase.getInstance(); 
     category=database.getReference("category"); 

     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); 

     // Set name for user 
     View headerView=navigationView.getHeaderView(0); 
     txtFullName=(TextView)headerView.findViewById(R.id.txtFullName); 
     txtFullName.setText(Common.currentUser.getName()); 

     //load menu 
recycler_menu=(RecyclerView)findViewById(R.id.recycler_menu); 
     recycler_menu.setHasFixedSize(true); 
     layoutManager=new LinearLayoutManager(this); 
     recycler_menu.setLayoutManager(layoutManager); 
     loadMenu(); 
    } 

    private void loadMenu() { 
     FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter=new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_item,MenuViewHolder.class,category) { 
      @Override 
      protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) { 
       viewHolder.txtMenuName.setText(model.getName()); 
       Picasso.with(getBaseContext()).load(model.getImage()) 
         .into(viewHolder.imageView); 

       final Category clickItem=model; 
       viewHolder.setItemClickListener(new ItemClickListener() { 
        @Override 
        public void onClick(View view, int position, boolean isLongClick) { 
         Toast.makeText(Home.this,""+clickItem.getName(),Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
     }; 
     recycler_menu.setAdapter(adapter); 
    } 

    @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.home, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     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_menu) { 
      // Handle the camera action 
     } else if (id == R.id.nav_cart) { 

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

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

     } 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; 
    } 
} 
+0

您可以加入該組up你有回收視圖的代碼? –

+0

我用java代碼編輯了我的問題。 –

回答

0

嘗試使用這個適配器在recyclerview

import android.content.Context; 
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.TextView; 
import android.widget.Toast; 

import com.google.firebase.database.ChildEventListener; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 

import java.util.ArrayList; 
import java.util.List; 

import illumine.ibecome.R; 

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { 

    private Context mContext; 
    private DatabaseReference mDatabaseReference; 
    private ChildEventListener mChildEventListener; 

    private List<String> mCommentIds = new ArrayList<>(); 
    private List<MyModel> mComments = new ArrayList<>(); 
    private static final String TAG = "ActivityName"; 

    public MyRecyclerViewAdapter(final Context context, DatabaseReference ref) { 
     mContext = context; 
     mDatabaseReference = ref; 

     ChildEventListener childEventListener = new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { 

       // A new comment has been added, add it to the displayed list 
       MyModel comment = dataSnapshot.getValue(MyModel.class); 
//    Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey()+" with module: "+comment.modules.get(0)); 
       // [START_EXCLUDE] 
       // Update RecyclerView 

       mCommentIds.add(dataSnapshot.getKey()); 
       mComments.add(comment); 
       notifyItemInserted(mComments.size() - 1); 

      } 

      @Override 
      public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) { 
       Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey()); 
       // A comment has changed, use the key to determine if we are displaying this 
       // comment and if so displayed the changed comment. 
       MyModel newComment = dataSnapshot.getValue(MyModel.class); 

       String commentKey = dataSnapshot.getKey(); 

       // [START_EXCLUDE] 
       int commentIndex = mCommentIds.indexOf(commentKey); 
       if (commentIndex > -1) { 
        // Replace with the new data 
        mComments.set(commentIndex, newComment); 

        // Update the RecyclerView 
        notifyItemChanged(commentIndex); 
       } else { 
        Log.w(TAG, "onChildChanged:unknown_child:" + commentKey); 
       } 
       // [END_EXCLUDE] 
      } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 
       Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey()); 

       // A comment has changed, use the key to determine if we are displaying this 
       // comment and if so remove it. 
       String commentKey = dataSnapshot.getKey(); 

       // [START_EXCLUDE] 
       int commentIndex = mCommentIds.indexOf(commentKey); 
       if (commentIndex > -1) { 
        // Remove data from the list 
        mCommentIds.remove(commentIndex); 
        mComments.remove(commentIndex); 

        // Update the RecyclerView 
        notifyItemRemoved(commentIndex); 
       } else { 
        Log.w(TAG, "onChildRemoved:unknown_child:" + commentKey); 
       } 
       // [END_EXCLUDE] 
      } 

      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { 
       Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey()); 


      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       Log.w(TAG, "postComments:onCancelled", databaseError.toException()); 
       Toast.makeText(mContext, "Failed to load interventions.", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     ref.addChildEventListener(childEventListener); 
     // [END child_event_listener_recycler] 

     // Store reference to listener so it can be removed on app stop 
     mChildEventListener = childEventListener; 
    } 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     View view = inflater.inflate(R.layout.item_view, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     MyModel comment = mComments.get(position); 

     holder.name_tv.setText(comment.name); 
     holder.details_tv.setText(comment.description); 
    } 

    @Override 
    public int getItemCount() { 
     return mComments.size(); 
    } 

    public void cleanupListener() { 
     if (mChildEventListener != null) { 
      mDatabaseReference.removeEventListener(mChildEventListener); 
     } 
    } 
    public MyModel getItem(int position){ 
     return mComments.get(position); 
    } 
    public class ViewHolder extends RecyclerView.ViewHolder { 
     TextView name_tv,details_tv; 
     View parent; 

     public ViewHolder(View rootView) { 
      super(rootView); 

      parent = rootView; 

      name_tv= (TextView) rootView.findViewById(R.id.name_tv); 
      details_tv = (TextView)rootView.findViewById(R.id.details_tv); 

     } 
    } 
} 
相關問題