指定的子级已有父级RecyclerView

时间:2017-01-06 12:07:25

标签: android android-recyclerview fragment

那是我的适配器:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter .ViewHolder> {
    private List<String> mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public ListAdapter (List<String> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ListAdapter .ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_list_out_in, parent, false);

        TextView itemList = (TextView) v.findViewById(R.id.item_list);
        ViewHolder vh = new ViewHolder(itemList);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset.get(position));
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

那是片段:

public class OutInListFragment extends Fragment {
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private ListAdapter mAdapter;
    private List<String> list = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_in_out_list, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.list_in_out);
        if(mRecyclerView!=null){
            Bundle bundle=getArguments();
            Cursor listCursor = getList(bundle.getInt("day"),bundle.getInt("month"),bundle.getInt("year"));
            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            mRecyclerView.setHasFixedSize(true);

            // use a linear layout manager
            mLayoutManager = new LinearLayoutManager(getContext());
            mRecyclerView.setLayoutManager(mLayoutManager);

            if(listCursor.getCount()>0) {
                listCursor.moveToFirst();
                int i = 0, index;
                while (!listCursor.isAfterLast()) {
                    index = listCursor.getColumnIndex("amount");
                    list.add(listCursor.getString(index));
                    listCursor.moveToNext();
                    i++;
                }
                listCursor.close();
            }else{
               list.add("0");
            }
            // specify an adapter (see also next example)
            mAdapter = new ListAdapter(list);
            mRecyclerView.setAdapter(mAdapter);
        }else{
            Toast.makeText(getContext(),"Error", Toast.LENGTH_SHORT).show();
        }
        return view;
    }
}

我收到此错误:

  

java.lang.IllegalStateException:指定的子节点已经有了   家长。您必须首先在孩子的父母身上调用removeView()。

2 个答案:

答案 0 :(得分:3)

只需替换

ViewHolder vh = new ViewHolder(itemList);

ViewHolder vh = new ViewHolder(v);
onCreateViewHolder(ViewGroup parent, int viewType)中的

因为itemListv的孩子。

答案 1 :(得分:2)

由于TextView ViewHolder vh = new ViewHolder(itemList);已有父级,因此您必须使用ViewHolder vh = new ViewHolder(v);而不是itemList