我查看了关于此的其他问题,并逐字地匹配了代码,但是我一定错过了一些东西,因为没有任何效果. getItemCount()返回零,但是我调试了数据列表,似乎可以正确添加所有数据,因此与适配器的通信出现了问题.
请告诉我这里出了什么问题!
下面是我的MainActivity.java代码:
import androID.content.Context;import androID.content.Intent;import androID.support.v7.app.ActionBaractivity;import androID.os.Bundle;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import com.parse.FindCallback;import com.parse.ParseException;import com.parse.ParSEObject;import com.parse.Parsequery;import com.parse.ParseUser;import java.util.ArrayList;import java.util.List;public class Profile_Page extends ActionBaractivity { UserLocalStore userLocalStore; private RecyclerVIEw recyclerVIEw; private DataAdapter adapter; private Context context; final linearlayoutmanager layoutManager = new linearlayoutmanager(this); public List<information> data = new ArrayList<>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_profile__page); ParseUser currentUser = ParseUser.getCurrentUser(); String struser = currentUser.getUsername(); recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclerVIEw); button logoutBtn = (button) findVIEwByID(R.ID.logoutBtn); adapter = new DataAdapter(getApplicationContext(), getData()); recyclerVIEw.setAdapter(adapter); recyclerVIEw.setLayoutManager(new linearlayoutmanager(getApplicationContext())); public List<information> getData(){ Parsequery <ParSEObject> query = Parsequery.getquery("ParseClassname"); query.whereEqualTo("author", ParseUser.getCurrentUser()); query.findInBackground(new FindCallback<ParSEObject>() { @OverrIDe public voID done(List<ParSEObject> List, ParseException e) { if (e == null) { for (ParSEObject getData : List) { information current = new information(); current.thing= getData.getString("Thing"); data.add(current); adapter.notifyDataSetChanged(); //for some reason, data isn't getting transferred to adapter } } else { //something went wrong } } });return data; } public voID logout(VIEw vIEw) { // logout current user ParseUser.logout(); finish(); }}
DataAdapter的代码:
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.button;import androID.Widget.Chronometer;import androID.Widget.EditText;import androID.Widget.TextVIEw;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class DataAdapter extends RecyclerVIEw.Adapter<DataAdapter.MyVIEwHolder>{ private LayoutInflater inflater; List<information> data = Collections.emptyList(); public DataAdapter(Context context, List<information> data) { inflater = LayoutInflater.from(context); this.data= data; } @OverrIDe public MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { VIEw vIEw = inflater.inflate(R.layout.recycler_vIEw_layout, parent, false); MyVIEwHolder holder = new MyVIEwHolder(vIEw); return holder; } @OverrIDe public voID onBindVIEwHolder(MyVIEwHolder holder, int position) { information current = data.get(position); holder.tv.setText(current.thing); } @OverrIDe public int getItemCount() { Log.d("datasize", String.valueOf(data.size())); return data.size(); } class MyVIEwHolder extends RecyclerVIEw.VIEwHolder { TextVIEw tv; public MyVIEwHolder(VIEw itemVIEw) { super(itemVIEw); tv= (TextVIEw) itemVIEw.findVIEwByID(R.ID.thingID); } }}
information.java
public class information{ public String thing;}
Recycler_vIEw_layout.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Dummy Text" androID:ID="@+ID/thingID"/></linearLayout>
添加了RecyclerVIEw的活动:
<relativeLayout 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" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.androID.blah.Profile_Page" > <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="button" androID:ID="@+ID/button3" androID:layout_alignParenttop="true" androID:layout_alignRight="@+ID/recyclerVIEw" androID:layout_alignEnd="@+ID/recyclerVIEw" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Log out" androID:ID="@+ID/logoutBtn" androID:layout_below="@+ID/button3" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:onClick="logout" androID:layout_margintop="85dp" /> <androID.support.v7.Widget.RecyclerVIEw androID:layout_wIDth="match_parent" androID:layout_height = "wrap_content" androID:ID="@+ID/recyclerVIEw" androID:layout_aligntop="@ID/logoutBtn" /></relativeLayout>
解决方法:
也许如果您在完成的方法中设置适配器,它将起作用.将底部插入循环.
adapter = new DataAdapter(getApplicationContext(), data); recyclerVIEw.setAdapter(adapter); recyclerVIEw.setLayoutManager(new linearlayoutmanager(getApplicationContext()));
总结 以上是内存溢出为你收集整理的java-未填充RecyclerView(getItemCount()返回零)全部内容,希望文章能够帮你解决java-未填充RecyclerView(getItemCount()返回零)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)