Recyclerview 未连接适配器
Posted
技术标签:
【中文标题】Recyclerview 未连接适配器【英文标题】:Recyclerview No adapter attached 【发布时间】:2018-02-04 06:53:04 【问题描述】:一切正常。但是在 logcat 中显示了一些错误。
E/RecyclerView:没有附加适配器;跳过布局 E/RecyclerView:没有附加适配器;跳过布局
我的活动代码:
Recyclerview recyclerView = findViewById( R.id.Recyclerview );
LinearLayoutManager layoutManager = new LinearLayoutManager( this, LinearLayoutManager.VERTICAL, false );
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(toolbaradapter);
我已阅读与同一问题相关的其他问题,但没有任何帮助。 请帮帮我
【问题讨论】:
什么是工具栏适配器? 你在哪里定义回收站的适配器,从你的适配器名称看来你使用了错误的适配器 ToolAdapter toolbaradapter = new ToolAdapter(this, List);这是我的适配器名称。 添加您有问题的整个活动..... 谢谢你重播我。我的问题解决了。适配器首先调用 onCreate() 。 【参考方案1】:My Activity class:
public class HelpToolBar extends Activity
RecyclerView.Adapter toolbaradapter;
RecyclerView.LayoutManager layoutManager;
Header header;
Recyclerview recyclerView;
public List<String> List;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
List = new ArrayList<>();
Recyclerview recyclerView = findViewById( R.id.Recyclerview );
toolbaradapter= new ToolAdapter(HelpToolBar.this,List);
LinearLayoutManager layoutManager = new LinearLayoutManager( this, LinearLayoutManager.VERTICAL, false );
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(toolbaradapter);
userLogin();
private void userLogin()
final JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
null, new Response.Listener<JSONObject>()
@Override
public void onResponse(JSONObject response)
String code = response.optString("success");
if (code.equals("true"))
try
JSONObject obj = response.getJSONObject("data");
JSONArray abc = obj.getJSONArray("list");
for(int i = 0; i < abc.length();i++)
JSONObject item = abc.getJSONObject(i);
List.add(item.getString("date"));
toolbaradapter= new ToolAdapter(HelpToolBar.this,List);
recyclerView.setAdapter(toolbaradapter);
catch (JSONException e)
progressbar.hidePDialog();
e.printStackTrace();
progressbar.hidePDialog();
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
VolleyLog.d("", "Error: " + error.getMessage());
Log.e("", "Site Info Error: " + error.getMessage());
progressbar.hidePDialog();
)
@Override
public Map<String, String> getHeaders() throws AuthFailureError
return header.getHeaders();
;
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(req);
My Adapter class:
public class ToolAdapter extends RecyclerView.Adapter<ToolAdapter.ViewHolder>
Context context;
private List<String> List
public ToolAdapter(HelpToolBar helpToolBar, List<String> List)
this.context= helpToolBar;
this.List=List;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.helptooladapter,parent, false);
return new ViewHolder(itemLayoutView);
@Override
public void onBindViewHolder(final ViewHolder holder, int position)
holder.textview1.setText(List.get(position) );
@Override
public int getItemCount()
return List.size();
public class ViewHolder extends RecyclerView.ViewHolder
public TextView1
public ViewHolder(View itemView)
super(itemView);
textview1= itemView.findViewById(R.id.tooladtextview);
【讨论】:
以上是关于Recyclerview 未连接适配器的主要内容,如果未能解决你的问题,请参考以下文章