Android 在回收视图片段选项卡布局消息中填充 Firebase 数据:E/RecyclerView:未连接适配器;



我是android Firebase的新手。我的问题是我想在数据库中显示我的数据列表 回收视图。我有一个包含 3 个选项卡的选项卡布局。在第一个选项卡中,我想显示我的数据,我使用片段 其中我遇到了我的回收视图,我也是方法

块引用

addValueEventListener

块引用

来获取我的 Firebase 数据。每当我启动应用程序时,它都不会显示任何内容,我有此消息

块引用

E/RecyclerView:未连接适配器;跳过布局

块引用

.这是有人可以帮助我吗。提前致谢

我的 Firebase 数据[1]: https://i.stack.imgur.com/XvWem.jpg

家活动.java

package com.example.saincurin.htfacile;
import android.os.Build;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.saincurin.htfacile.Adapters.PageAdapter;
public class HomeActivity extends AppCompatActivity {
private Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
private DrawerLayout mDrawer;
private NavigationView nvDrawer;
private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
mDrawer = findViewById(R.id.drawer_layout);
nvDrawer = findViewById(R.id.nvView);
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
toolbar.setTitle(getResources().getString(R.string.app_name));
setSupportActionBar(toolbar);
// Set up Drawer View

PagerAdapter pagerAdapter = new PageAdapter(getSupportFragmentManager(), HomeActivity.this);
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(HomeActivity.this,
R.color.colorAccent));
}
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawer, toolbar,
R.string.drawer_open, R.string.drawer_close);
mDrawer.addDrawerListener(toggle);
toggle.syncState();
// give the Tablayout the viewPager
tabLayout.setupWithViewPager(viewPager);
//        setupDrawerContent(nvDrawer);
}
@Override
public void onBackPressed() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
//    private void setupDrawerContent(NavigationView navigationView) {
//        navigationView.setNavigationItemSelectedListener(
//                new NavigationView.OnNavigationItemSelectedListener() {
//                    @Override
//                    public boolean onNavigationItemSelected(MenuItem menuItem) {
//                        selectDrawerItem(menuItem);
//                        return true;
//                    }
//                });
//    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_products, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_more) {
Toast.makeText(this, "The more is selected", Toast.LENGTH_SHORT).show();
}else if (item.getItemId() == R.id.action_search) {
Toast.makeText(this, "The search is selected", Toast.LENGTH_SHORT ).show();
}else if (item.getItemId() == R.id.drawer_layout) {
mDrawer.openDrawer(GravityCompat.START);
}
return true;
}
}

我的模型

package com.example.saincurin.htfacile.ModelData;
public class DataModel {
private String  description;
private String name;
private String image;
private String price;
private String quantity;

//constructor
public DataModel() {
}
public DataModel(String name, /*String description,*/ String image, String price) {
this.name = name;
//        this.description = description;
this.image = image;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}

我的适配器

package com.example.saincurin.htfacile.Adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.saincurin.htfacile.ModelData.DataModel;
import com.example.saincurin.htfacile.R;
import com.squareup.picasso.Picasso;
import java.util.List;

// Create the basic adapter extending from RecyclerView.Adapter
// Note that we specify the custom ViewHolder which gives us access to our views
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductsviewHolder> {
private Context mcontext;
// Store a member variable for the contacts
private List<DataModel> mDataModel;
public ProductsAdapter(Context context, List<DataModel> mDataModel) {
mcontext = context;
this.mDataModel = mDataModel;
}

// Usually involves inflating a layout from XML and returning the holder
@NonNull
@Override
public ProductsviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//inflate the custom Layout
View dataView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
//return a new holder instance
return new  ProductsviewHolder(dataView);

}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(@NonNull ProductsviewHolder holder, int position) {
// Get the data model based on position
DataModel dataModel = mDataModel.get(position);
// Set item views based on your views and data model
holder.txtName.setText(dataModel.getName());
//        holder.txtDescription.setText(dataModel.getDescription());
holder.txtPrice.setText(dataModel.getPrice());
Picasso.with(mcontext)
.load(dataModel.getImage())
.fit()
.centerCrop()
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mDataModel.size();
}
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public class ProductsviewHolder extends RecyclerView.ViewHolder {
public TextView txtName;
//        public TextView txtDescription;
public TextView txtPrice;
public TextView txtQuantity;
public ImageView imageView;

// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ProductsviewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
txtName = itemView.findViewById(R.id.rvTitleTv);
//            txtDescription = itemView.findViewById(R.id.rDescriptionTv);
txtPrice = itemView.findViewById(R.id.rPrice);
imageView = itemView.findViewById(R.id.rImageView);
}
}

}

我的第一个片段选项卡

package com.example.saincurin.htfacile.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
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.Toast;
import com.example.saincurin.htfacile.Adapters.ProductsAdapter;
import com.example.saincurin.htfacile.ModelData.DataModel;
import com.example.saincurin.htfacile.R;
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;
/**
* A simple {@link Fragment} subclass.
*/
public class ProductsFragment extends Fragment {
private RecyclerView mRecycleView;
private ProductsAdapter mProductsAdapter;
private DatabaseReference mReference;
private List<DataModel> mDataModel;
View v;
public ProductsFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_products, container, false);
//RecycleView
mRecycleView = v.findViewById(R.id.recycleView);
mRecycleView.setHasFixedSize(true);
//set Layout as LinearLayout
mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
mDataModel = new ArrayList<>();

//send Query FirebaseDatabase
mReference = FirebaseDatabase.getInstance().getReference("uploads");
Log.e("DEBUG", "The Reference :"+mReference);
mReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
DataModel dataModel = postSnapshot.getValue(DataModel.class);
mDataModel.add(dataModel);
}
//                Log.e("DEBUG","The data model "+ mDataModel.toString());
mProductsAdapter = new ProductsAdapter(getContext(), mDataModel);
//                Log.e("DEBUG","The data model "+ mProductsAdapter);
//set the adapter to the recyclerview
mRecycleView.setAdapter(mProductsAdapter);
mProductsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});

return v;
}
}

我的第一个标签页的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".fragments.ProductsFragment">
<!--RecycleView-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

我的家庭活动的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
style="@style/myCustomTabLayout"
android:background="@color/colorPrimary"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

我的item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="match_parent"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
app:contentPadding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/rvTitleTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/rImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:background="@drawable/loading"
android:scaleType="center" />
<TextView
android:id="@+id/rPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="150 gdes"
android:textSize="20dp"
android:textStyle="bold" />
<!--<TextView-->
<!--android:id="@+id/rDescriptionTv"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="This is the post description that"-->
<!--android:textSize="20sp" />-->
</LinearLayout>
</android.support.v7.widget.CardView>

欢迎来到 StackOverflow。RecyclerView在设置方面非常具体。因此,您需要确保在设置布局管理器和 fixedSize 属性之前设置适配器。

尝试将onCreateView方法更改为:

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_products, container, false);
// Notice that I changed the order of RecyclerView setup
mRecycleView = v.findViewById(R.id.recycleView);
mDataModel = new ArrayList<>();
mProductsAdapter = new ProductsAdapter(getContext(), mDataModel);
mRecyclerView.setAdapter(mProductsAdapter);
mRecycleView.setHasFixedSize(true);
//set Layout as LinearLayout
mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
//send Query FirebaseDatabase
mReference = FirebaseDatabase.getInstance().getReference("uploads");
mReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
DataModel dataModel = postSnapshot.getValue(DataModel.class);
mDataModel.add(dataModel);
}
mProductsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
return v;
}

最新更新