你可以向我解释为什么我得到了#34;致命的异常:java.lang.UnsupportedOperationException"刷新我的内容? 在正常情况下,该功能应该下载数据并为每一行创建一个回收器/卡......
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 2500);
new Thread(new Runnable(){
public void run(){
if(!isNetworkAvailable()){
Toast.makeText(getApplicationContext(), getResources().getString(R.string.nointernet), Toast.LENGTH_LONG).show();
return;
}
String str=getOnline("http://ni141767_1.vweb12.nitrado.net/vp.php");
String lines[] = str.split("\\<.*?\\>");
for (int i = 0; i < lines.length; i++) {
String content[] = lines[i].split("_");
String fach = content[5];
String lehrer = content[6];
Data dataToAdd = new Data(fach,lehrer);
mData.add(dataToAdd);
mAdapter.addItem(i, dataToAdd);
content = null;
}
}
}).start();
}
Data.java:
public class Data {
public String text;
public String lehrer;
public Data(String text, String lehrer) {
this.text = text;
this.lehrer = lehrer;
}
}
CustomRecyclerAdapter.java:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Collections;
import java.util.List;
public class CustomRecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private List<Data> mData = Collections.emptyList();
public CustomRecyclerAdapter() {
// Pass context or other static stuff that will be needed.
}
public void updateList(List<Data> data) {
mData = data;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.card, viewGroup, false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) {
viewHolder.fach.setText(mData.get(position).text);
viewHolder.lehrer.setText(mData.get(position).lehrer);
}
public void addItem(int position, Data data) {
mData.add(position, data);
notifyItemInserted(position);
}
public void removeItem(int position) {
mData.remove(position);
notifyItemRemoved(position);
}
}
RecyclerViewHolder.java:
import android.support.v7.widget.RecyclerView;
import android.view.View; import android.widget.TextView;
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView fach;
public TextView lehrer;
public RecyclerViewHolder(View itemView) {
super(itemView);
fach = (TextView) itemView.findViewById(R.id.fach);
lehrer = (TextView) itemView.findViewById(R.id.lehrer);
}
}
答案 0 :(得分:5)
您无法添加List
返回的Collections.emptyList()
。此方法不会返回ArrayList
或LinkedList
,而是永久保持为空的不可变List
。
改为使用new ArrayList<>()
。
答案 1 :(得分:0)
Collections.emptyList()返回一个不可变列表(您将无法在列表中添加元素!!)
您应该使用新关键字
将其初始化为新列表