-1

can you explain to me why I get "FATAL EXCEPTION: java.lang.UnsupportedOperationException" on refreshing my content? In normal case, the function should download the data and creates a recycler / card for every line...

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);
        }
    }
Fabian
  • 87
  • 1
  • 10

2 Answers2

5

You can't add to the List returned by Collections.emptyList(). This method does not return an ArrayList or a LinkedList, but an immutable List that always remains empty.

Use new ArrayList<>() instead.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • nice spotted. Can you add the reason to your answer? – Blackbelt Mar 22 '15 at 15:33
  • 1
    @Blackbelt I've added a bit more. – Paul Boddington Mar 22 '15 at 15:36
  • Thank you, no more errors. Now it´s the question why it doesn´t creates cards... Do you have an idea? – Fabian Mar 22 '15 at 15:59
  • Thank you. Do anyone of you also know why I get "android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views." on uncommenting "notifyItemInserted(position);" in CustomRecyclerView.java on line 42? – Fabian Mar 22 '15 at 16:06
  • @Fabian Yes. It's because you're using a second thread. See this question: http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi – Paul Boddington Mar 22 '15 at 16:14
  • @pbabcdefp So I have to create another method outside the thread which adds the item? and i should call this method by the thread? – Fabian Mar 22 '15 at 16:20
  • @Fabian To be honest, you're asking the wrong person on this one. I seldom use multiple threads. All I know is that in Android you can only modify views on the main thread. I'd ask it as a separate question. – Paul Boddington Mar 22 '15 at 16:22
  • ok, thank you all for helping me :D – Fabian Mar 22 '15 at 16:24
0

Collections.emptyList() returns an immutable list (you will NOT be able to add elements to your list!!)

You should initialize it rather to a new list using new keyword

Karan
  • 2,120
  • 15
  • 27