Java RecyclerView:未连接适配器;跳过布局问题
我得到了一个错误: 回收视图﹕ 无适配器连接;跳过布局 我检查了一些其他人对此问题给出的答案,但没有运气 适配器:Java RecyclerView:未连接适配器;跳过布局问题,java,android,android-studio,android-recyclerview,Java,Android,Android Studio,Android Recyclerview,我得到了一个错误: 回收视图﹕ 无适配器连接;跳过布局 我检查了一些其他人对此问题给出的答案,但没有运气 适配器: public class MyAdapter extends RecyclerView.Adapter<com.example.myapplication.MyAdapter.MyViewHolder> { private Context context; private List<HeadlineModel> headlineModelL
public class MyAdapter extends RecyclerView.Adapter<com.example.myapplication.MyAdapter.MyViewHolder> {
private Context context;
private List<HeadlineModel> headlineModelList;
public MyAdapter(Context context, List<HeadlineModel> headlineModelList) {
this.context = context;
this.headlineModelList = headlineModelList;
}
@NonNull
@Override
public com.example.myapplication.MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.news_item_layout,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final com.example.myapplication.MyAdapter.MyViewHolder holder, int position) {
final HeadlineModel headlineModel=headlineModelList.get(position);
holder.newTitle.setText(headlineModel.getTitle());
holder.newsDescription.setText(headlineModel.getDescription());
holder.newsDescription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri= Uri.parse(headlineModel.getUrl());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
//Log.v("SSSSSS",headlineModel.getUrl());
}
});
holder.newsName.setText(headlineModel.getName());
holder.newsTime.setText(headlineModel.getPublishedAt());
Glide.with(context).load(headlineModel.getUrlToImage())
.thumbnail(0.5f)
.into(holder.newsImage);
}
@Override
public int getItemCount() {
return headlineModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView newTitle;
TextView newsDescription;
TextView newsName;
TextView newsTime;
ImageView newsImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
newTitle=itemView.findViewById(R.id.tv_news_title);
newsDescription=itemView.findViewById(R.id.tv_news_desc);
newsName=itemView.findViewById(R.id.tv_name);
newsTime=itemView.findViewById(R.id.tv_news_date);
newsImage=itemView.findViewById(R.id.im_news_image);
}
}
}
公共类MyAdapter扩展了RecyclerView.Adapter{
私人语境;
私有列表标题模型列表;
公共MyAdapter(上下文上下文,列表headlineModelList){
this.context=上下文;
this.headlineModelList=headlineModelList;
}
@非空
@凌驾
public com.example.myapplication.MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int-viewType){
视图=布局更平坦。从(上下文)。充气(R.layout.news\u item\u布局,父项,false);
返回新的MyViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull final com.example.myapplication.MyAdapter.MyViewHolder,int位置){
最终HeadlineModel HeadlineModel=headlineModelList.get(位置);
holder.newTitle.setText(headlineModel.getTitle());
holder.newsDescription.setText(headlineModel.getDescription());
holder.newsDescription.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Uri=Uri.parse(headlineModel.getUrl());
意图=新意图(Intent.ACTION\u视图,uri);
背景。开始触觉(意图);
//Log.v(“ssss”,headlineModel.getUrl());
}
});
holder.newsName.setText(headlineModel.getName());
holder.newtime.setText(headlineModel.getPublishedAt());
Glide.with(context.load)(headlineModel.getUrlToImage())
.缩略图(0.5f)
.进入(持有者.新闻图像);
}
@凌驾
public int getItemCount(){
返回headlineModelList.size();
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
文本视图新标题;
文本视图新闻描述;
文本视图新闻名称;
文本视图新闻时间;
图像查看新闻图像;
公共MyViewHolder(@NonNull View itemView){
超级(项目视图);
newTitle=itemView.findviewbyd(R.id.tv\u news\u title);
newsDescription=itemView.findViewById(R.id.tv\u news\u desc);
newsName=itemView.findviewbyd(R.id.tv_name);
newsTime=itemView.findViewById(R.id.tv\u news\u date);
newsImage=itemView.findViewById(R.id.im\u news\u image);
}
}
}
我的片段中onCreateView中的recycler视图初始化如下:
RecyclerView recyclerView;
com.example.myapplication.MyAdapter myAdapter;
private ProgressDialog progressDialog;
List<com.example.myapplication.HeadlineModel> headlineModelList;
private static final String URL="http://newsapi.org/v2/top-headlines?country=in&apiKey=cbd46bd6a4f54fe69d0cb261dbe1a878";
public HeadlinesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.fragment_headlines, container, false);
// Inflate the layout for this fragment
recyclerView=rootview.findViewById(R.id.headRecyclerView);
headlineModelList=new ArrayList<>();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
progressDialog=new ProgressDialog(getContext(),R.style.ProgressColor);
progressDialog.setMessage("loading...");
progressDialog.show();
loadData();
return rootview;
}
RecyclerView-RecyclerView;
com.example.myapplication.MyAdapter MyAdapter;
私有进程对话;
列表标题模型列表;
私有静态最终字符串URL=”http://newsapi.org/v2/top-headlines?country=in&apiKey=cbd46bd6a4f54fe69d0cb261dbe1a878";
公共标题片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u标题,容器,false);
//为该碎片膨胀布局
RecycleView=rootview.findViewById(R.id.HeadleCyclerView);
headlineModelList=新的ArrayList();
recyclerView.setHasFixedSize(true);
setLayoutManager(新的LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
progressDialog=新建progressDialog(getContext(),R.style.ProgressColor);
progressDialog.setMessage(“加载…”);
progressDialog.show();
loadData();
返回rootview;
}
调用的loadData()函数
private void loadData() {
progressDialog.setMessage("Loading data...");
//progressDialog.show();
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONArray jsonArray=response.getJSONArray("articles");
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String title=jsonObject.getString("title");
String desc=jsonObject.getString("description");
String url=jsonObject.getString("url");
String urlToImage=jsonObject.getString("urlToImage");
String publishedAt=jsonObject.getString("publishedAt");
JSONObject source=jsonObject.getJSONObject("source");
String name=source.getString("name");
// for formatting time and date //
String year=publishedAt.substring(0,4);
String month=publishedAt.substring(5,7);
String date=publishedAt.substring(8,10);
String hour=publishedAt.substring(11,13);
//String hour="11";
String min=publishedAt.substring(14,16);
//Log.v("XXXXXX",min);
String updatedDate=date.concat("-").concat(month).concat("-").concat(year).concat(" ");
String print="";
int convertHour= Integer.parseInt(hour);
int convertMin= Integer.parseInt(min);
if (convertHour==12) {
convertHour=12;
print="PM";
}
else if (convertHour>11&&convertMin>0){
convertHour=convertHour-12;
print="PM";
}else {
print="AM";
}
String newHour= String.valueOf(convertHour);
String updatedTime=updatedDate.concat(newHour).concat(":").concat(min).concat(" ").concat(print);
myAdapter=new com.example.myapplication.MyAdapter(getContext(),headlineModelList);
com.example.myapplication.HeadlineModel headlineModel=new com.example.myapplication.HeadlineModel(name,title,desc,url,urlToImage,updatedTime);
headlineModelList.add(headlineModel);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), cacheEntry);
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(JSONObject response) {
super.deliverResponse(response);
}
@Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
RequestQueue queue= Volley.newRequestQueue(getContext());
queue.add(jsonObjectRequest);
}
private void loadData(){
progressDialog.setMessage(“正在加载数据…”);
//progressDialog.show();
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,URL,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
progressDialog.disclose();
试一试{
JSONArray JSONArray=response.getJSONArray(“文章”);
对于(int i=0;i11&&convertMin>0){
convertHour=convertHour-12;
print=“PM”;
}否则{
print=“AM”;
}
String newHour=String.valueOf(convertHour);
字符串updatedTime=updatedDate.concat(newHour).concat(“:”).concat(min).concat(“”).concat(print);
myAdapter=new com.example.myapplication.myAdapter(getContext(),headlineModelList);
com.example.myapplication.HeadlineModel HeadlineModel=new com.example.myapplication.HeadlineModel(名称、标题、说明、url、urlToImage、updatedTime);
headlineModelList.add(headlineModel);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
})
{
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
Cache.Entry cacheEntry=HttpHeaderParser.parseCacheHeaders(响应);
if(cacheEntry==null){
cacheEntry=new Cache.Entry();
}
final long cachehitbutrefresh=3*60*1000;//3分钟后将命中缓存,但也会在后台刷新
最终长缓存过期=24