简体   繁体   English

RecyclerView未显示在我的应用中

[英]RecyclerView Not showing up in my app

i couldn't find whats wrong with my code.. RecyclerView not showing up when i run my app.. 我找不到我的代码有什么问题。.当我运行我的应用程序时,RecyclerView没有显示。

Thanks in advance. 提前致谢。

Main Activity onCreate Method: 主要活动onCreate方法:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_appbar);

    toolbar=(Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);

    NavFragment drawerFragment = (NavFragment)
            getSupportFragmentManager().findFragmentById(R.id.nav_frag);
    drawerFragment.setUp(R.id.nav_frag, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);
    listEvents= new ArrayList<>();



    Parse.enableLocalDatastore(this);
    Parse.initialize(this, "xxxxxxx", "xxxxxxxx");

    //TEST PARSE
    final TextView t =(TextView) findViewById(R.id.textView);
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Test");
    //query.whereEqualTo("playerName", "Dan Stemkoski");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> eventList, ParseException e) {
            if (e == null) {
                Log.d("Events", "Retrieved " + eventList.size() + " Events");

                for (int i = 0; i < eventList.size(); i++) {
                    Events events = new Events();
                    events.setTitle((String) eventList.get(i).get("teststr"));
                    events.setId(String.valueOf( eventList.get(i).get("id")));
                    listEvents.add(events);
                }
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });


    eventAdapter= new EventAdapter(this);
    eventAdapter.setEventList(listEvents);
    eventsList=(RecyclerView)findViewById(R.id.list_events);
    eventsList.setLayoutManager(new LinearLayoutManager(this));
    eventsList.setAdapter(eventAdapter);


}

Event Adapter for the RecylerView : RecylerView的事件适配器:

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolderEvents> {
private LayoutInflater layoutInflater;
private ArrayList<Events> listEvents= new ArrayList<>();
public EventAdapter(Context context){
    layoutInflater=LayoutInflater.from(context);
}

public void setEventList(ArrayList<Events> listEvents){
    this.listEvents=listEvents;
    notifyItemRangeChanged(0,listEvents.size());
}

@Override
public ViewHolderEvents onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view=layoutInflater.inflate(R.layout.custom_event_row,viewGroup,false);
    ViewHolderEvents viewHolder =new ViewHolderEvents(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolderEvents viewHolderEvents, int i) {
    Events currentEvent= listEvents.get(i);
    viewHolderEvents.title.setText(currentEvent.getTitle());
    viewHolderEvents.id.setText(currentEvent.getId());

}

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

static class ViewHolderEvents extends RecyclerView.ViewHolder{
    private TextView title;
    private TextView id;

    public ViewHolderEvents(View itemView) {
        super(itemView);
        title=(TextView)itemView.findViewById(R.id.title);
        id=(TextView)itemView.findViewById(R.id.description);
    }
}

} }

XML Layout which is been set as Layout for the RecylerView: activity_main_appbar : 设置为RecylerView布局的XML布局:activity_main_appbar:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_events"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView"
            android:layout_gravity="center" />
    </FrameLayout>
    <fragment
        android:id="@+id/nav_frag"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_nav"
        android:name="com.squaredbytes.eventlane.NavFragment"
        tools:layout="@layout/fragment_nav" />

</android.support.v4.widget.DrawerLayout>
</LinearLayout>

Try to create and set your EventAdapter into your FindCallback. 尝试创建并将EventAdapter设置为FindCallback。

query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> eventList, ParseException e) {
            if (e == null) {
                Log.d("Events", "Retrieved " + eventList.size() + " Events");

            for (int i = 0; i < eventList.size(); i++) {
                Events events = new Events();
                events.setTitle((String) eventList.get(i).get("teststr"));
                events.setId(String.valueOf( eventList.get(i).get("id")));
                listEvents.add(events);
            }

            eventAdapter = new EventAdapter(this);
            eventAdapter.setEventList(listEvents);
            eventsList.setAdapter(eventAdapter);
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM