链接到GITHUB REPO https://github.com/shenoyabhijith/Notifyme2
在这里,我想将它连接到firebase数据库并将数据加载到片段
注意IM使用FIREBASEUI
自定义recyclelerAdapter
我有3个片段 我想在所有3个片段中加载不同的数据 例如我有一个来自tablayout的片段
当我点击片段为空并获得空白屏幕时
这里我想从firebaseDatabase加载数据 CompanyFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
/**
* Created by USER on 4/8/2017.
*/
public class CompanyFragment extends Fragment {
public CompanyFragment() {
// Required empty public constructor
}
@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 rootView = inflater.inflate(R.layout.fragment_blank, container, false);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
FirebaseRecyclerAdapter < Company, ChatHolder > mAdapter = new FirebaseRecyclerAdapter < Company, ChatHolder > (
Company.class,
android.R.layout.activity_list_item,
ChatHolder.class,
databaseReference
) {
@Override
protected void populateViewHolder(ChatHolder viewHolder, Company model, int position) {
viewHolder.setCompanyName(Company.getmCompanyName());
viewHolder.setCompanyPackage(Company.getmCompanyPackage());
}
};
rv.setAdapter(mAdapter);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
}
用于使用getter和setter存储和检索数据的firebaseRecycler适配器的类
Company.java
public class Company {
private static String mCompanyName;
private static String mCompanyDescription;
private static String mCompanyPackage;
public Company() {
}
public Company(String CompanyName, String CompanyDescription, String CompanyPackage) {
mCompanyName = CompanyName;
mCompanyDescription = CompanyDescription;
mCompanyPackage = CompanyPackage;
}
public static String getmCompanyName() {
return mCompanyName;
}
public void setmCompanyName(String mCompanyName) {
this.mCompanyName = mCompanyName;
}
public String getmCompanyDescription() {
return mCompanyDescription;
}
public void setmCompanyDescription(String mCompanyDescription) {
this.mCompanyDescription = mCompanyDescription;
}
public static String getmCompanyPackage() {
return mCompanyPackage;
}
public void setmCompanyPackage(String mCompanyPackage) {
this.mCompanyPackage = mCompanyPackage;
}
}
firebaseRecyclerAdapter的ViewHolder类
public class ChatHolder extends RecyclerView.ViewHolder {
private final TextView mNameField;
private final TextView mTextField;
public ChatHolder(View itemView) {
super(itemView);
mNameField = (TextView) itemView.findViewById(R.id.tv_blah);
mTextField = (TextView) itemView.findViewById(R.id.tv_text);
}
public void setCompanyName(String CompanyName) {
mNameField.setText(CompanyName);
}
public void setCompanyPackage(String CompanyPackage) {
mTextField.setText(CompanyPackage);
}
}
答案 0 :(得分:0)
尝试在setAdapter之前设置布局管理器。在创建视图的适配器时,适配器不知道布局管理器。这就是为什么你什么也看不见