Recycler视图不显示

时间:2017-07-26 07:53:02

标签: android firebase firebase-realtime-database

我刚刚将MyConsumptionList活动转换为片段。在活动中,Recycler视图显示完美,但在片段中,它没有显示。编译时没有错误,应用程序正常运行。 我想将消费列表显示为导航栏中的项目。请帮帮我。

MyConsumptionList.java的代码

public class MyConsumptionList extends Fragment{
DatabaseReference db;
FirebaseHelper helper;
RecyclerViewAdapter adapter;
RecyclerView rv;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_my_consumption_list,container,false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Consumption List");
    startList();
}

@Override
public void onStart() {
    super.onStart();
    startList();


}

@Override
public void onResume() {
    super.onResume();
    startList();
}

public void startList(){
    //initialize rv
    rv=(RecyclerView)getView().findViewById(R.id.recyclerView);
    rv.setLayoutManager(new LinearLayoutManager(getActivity()));

    //initialize fb
    db= FirebaseDatabase.getInstance().getReference().child("My consumption");
    helper=new FirebaseHelper(db);

    //adapter
    adapter=new RecyclerViewAdapter(getActivity(),helper.retrieve());
    rv.setAdapter(adapter);
}

回收站视图适配器的代码。

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {
static String key_UID=null;
protected Context c;
private ArrayList<SaveData>sd;
FirebaseDatabase fb;


public RecyclerViewAdapter(Context c, ArrayList<SaveData> sd) {
    this.c = c;
    this.sd = sd;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(c).inflate(R.layout.recycler_view_row,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.ItemnameTxt.setText(sd.get(position).getName());
    holder.BrandNameTxt.setText(sd.get(position).getBrand_Name());
    holder.CaloriesTxt.setText("Calories: " + sd.get(position).getSave_calories());
    holder.FatTxt.setText("Fat: " + sd.get(position).getSave_fat());
    holder.Servingtxt.setText(sd.get(position).getServing_Size());

    //db = FirebaseDatabase.getInstance().getReference();


    //key_UID=sd.get(position).getName();

    // holder.deleteButton.setOnClickListener(new View.OnClickListener() {
    // @Override
    //public void onClick(View v) {
    //});

}


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

Firebase Helper.java的代码

public class FirebaseHelper {
DatabaseReference db;
ArrayList<SaveData> sd=new ArrayList<>();

//pass database reference
public FirebaseHelper(DatabaseReference db){
    this.db=db;
}

//read by hooking onto database operation callbacks
public ArrayList<SaveData>retrieve(){
    db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            SaveData saves=dataSnapshot.getValue(SaveData.class);
            sd.add(saves);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            SaveData saves=dataSnapshot.getValue(SaveData.class);
            sd.add(saves);

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {



        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return sd;
}

我的content_navigationBar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:context="nutritionapp.listview.bio.ayushsharma.nutritionapp.
    Navigationbar"
    tools:showIn="@layout/app_bar_navigationbar">

</RelativeLayout>

我的navigationBar.java

public class Navigationbar extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigationbar);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


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

    FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_main, new MainActivity());
    ft.commit();

}

@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.navigationbar, 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();
    Fragment frag=null;

    if (id == R.id.nav_home) {
        // Handle the input action
        frag=new MainActivity();

    } else if (id == R.id.nav_consuptionList) {
        frag=new MyConsumptionList();

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

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

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

    }

    if(frag != null){
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_main,frag);
        ft.commit();
    }

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

consumptionList.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="nutritionapp.listview.bio.ayushsharma.nutritionapp.
MyConsumptionList">

<TextView
    android:id="@+id/consTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Consumption List"
    android:layout_centerHorizontal="true"
    android:textSize="15sp"
    android:textStyle="bold"
    android:textColor="#dc0d28"/>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView"
    android:layout_below="@+id/consTitle"
    android:layout_marginTop="10dp"
    >

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

适配器中的ViewHolder类在哪里? 在哪里初始化

holder.ItemnameTxt
holder.BrandNameTxt
holder.CaloriesTxt

答案 1 :(得分:0)

我认为你的问题就在你做的时候 new RecyclerViewAdapter(getActivity(),helper.retrieve()); 您的数组是空的,因为只有在其他事件引发ChildEventListener事件的事件时才会添加数据。

因此,您应该在将数据添加到阵列后调用notifyDataSetChanged()来通知适配器。