我正在开发一个应用程序,它从网站中提取新闻内容并显示在recyclerview(内部片段)中。
我收到logcat错误说明"没有附加适配器,跳过布局"。我可以在logcat中看到数据,但无法在recyclerview中显示。
代码到现在为止:private class NewsExtractor extends AsyncTask<Void, Void, Void>
{
String title;
@Override
protected Void doInBackground(Void... params) {
Document doc = null;
while (status == OK) {
currentURL = url + String.valueOf(page);
//add the page number to the url
try {
response = Jsoup.connect(currentURL)
.execute(); //you may add here userAgent/timeout etc.
status = response.statusCode();
if (status == OK) {
doc = response.parse();
Elements urlLists = doc.select("a[rel=bookmark]");
for (org.jsoup.nodes.Element urlList : urlLists) {
String src = urlList.text();
Log.d("NEWS",src);
myLinks.add(src);
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
page++;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
myRecyclerViewAdapter = new MyRecyclerViewAdapter(getActivity(),myLinks);
recyclerView.setAdapter(myRecyclerViewAdapter);
}
}
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_all, container, false);
int i = getArguments().getInt(NEWS);
String topics = getResources().getStringArray(R.array.topics)[i];
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyNews);
recyclerView.setHasFixedSize(true);
switch (i) {
case 0:
url = "http://myagdikali.com/category/sport-news/page/";
new NewsExtractor().execute();
}
return rootView;
}
RecyclerViewAdapter代码
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder>{
private ArrayList<String> myLinks;
private Context mContext;
public MyRecyclerViewAdapter(Context context, ArrayList<String> myLinks) {
this.myLinks = myLinks;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.textView.setText(myLinks.get(position));
}
@Override
public int getItemCount() {
return myLinks.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
protected TextView textView;
public CustomViewHolder(View itemView) {
super(itemView);
this.textView = (TextView) itemView.findViewById(R.id.title);
}
}
} 事实是,我只能从一个页面显示内容(不使用循环NewsExtractor)