我在显示从API到RecyclerView获取到Tabbed Activity Fragment的一些数据时遇到问题。首先是错误:
E/RecyclerView﹕ No adapter attached; skipping layout
以下是我的代码,类和布局:
这是片段RegPicsFragment
:
public class RegPicsFragment extends Fragment {
ProgressBar progressBar;
View view;
RecyclerView myRecyclerView;
RecyclerView.LayoutManager myLayoutManager;
RecyclerView.Adapter myAdapter;
List<RegEndaneredItem> myItems;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_regpics, container ,false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FontChangeCrawler fontChanger = new FontChangeCrawler(getActivity().getAssets(), "iransans.ttf");
fontChanger.replaceFonts((ViewGroup) this.getView());
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
myRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view2);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myLayoutManager = new GridLayoutManager(view.getContext(), 1);
myRecyclerView.setLayoutManager(myLayoutManager);
}
class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
protected String doInBackground(Void... urls) {
try {
URL url = new URL("http://jsonplaceholder.typicode.com/users");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
try {
JSONArray data = new JSONArray(response);
for(int i=0; i< data.length(); i++) {
String name = data.getJSONObject(i).getString("name");
Log.i("milad", name);
myItems.add((RegEndaneredItem) myItems);
}
} catch (JSONException e) {
e.printStackTrace();
}
myAdapter = new RegGridAdapter(myItems);
myRecyclerView.setAdapter(myAdapter);
}
}
以下是适配器RegGridAdapter
:
public class RegGridAdapter extends RecyclerView.Adapter<RegGridAdapter.ViewHolder> {
List<RegEndaneredItem> myItems;
public RegGridAdapter(List<RegEndaneredItem> myItems) {
this.myItems = myItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.reg_grid_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
RegEndaneredItem nature = myItems.get(i);
viewHolder.tvspecies.setText(nature.getName());
}
@Override
public int getItemCount() {
return myItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvspecies;
public ViewHolder(View itemView) {
super(itemView);
tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
FontChangeCrawler fontChanger = new FontChangeCrawler(itemView.getContext().getAssets(), "iransans.ttf");
fontChanger.replaceFonts((ViewGroup) itemView);
}
}
Heres是Getter and Setter RegEndaneredItem
:
public class RegEndaneredItem {
private String mName;
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
最后这里是布局frag_regpics
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerHorizontal="true"
android:visibility="gone" />
</LinearLayout>
答案 0 :(得分:0)
在onActivityCreated
中设置一个带有空列表的适配器。
List<RegEndaneredItem> myItems = new ArrayList<RegEndaneredItem>();
myAdapter = new RegGridAdapter(myItems);
myRecyclerView.setAdapter(myAdapter);
同时检查myItems
中的RegPicsFragment
列表是否为空。
我没有看到myItems
的初始化。
myItems.add((RegEndaneredItem) myItems);
这条线没有任何意义。您必须将值设置为RegEndaneredItem
的对象并将其添加到列表中。
RegEndaneredItem reg = new RegEndaneredItem();
reg.setName(name);//calling Your SetterMethod
myItems.add(reg);
答案 1 :(得分:0)
也许我的答案稍微偏离主题,但如果您想降低代码复杂性,可以使用此库而不是Android RESTAdapter
实施起来非常简单,每次都像魅力一样。