指定的孩子已经有一个父母,RecyclerView
Posted
技术标签:
【中文标题】指定的孩子已经有一个父母,RecyclerView【英文标题】:The specified child already has a parent, RecyclerView 【发布时间】:2017-05-21 04:43:56 【问题描述】:那是我的适配器:
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()。
【问题讨论】:
【参考方案1】:只需替换
ViewHolder vh = new ViewHolder(itemList);
与
ViewHolder vh = new ViewHolder(v);
在onCreateViewHolder(ViewGroup parent, int viewType)
.
因为itemList
是v
的孩子。
【讨论】:
【参考方案2】:您必须使用ViewHolder vh = new ViewHolder(v);
而不是ViewHolder vh = new ViewHolder(itemList);
,因为TextView itemList
已经有一个父级。
【讨论】:
感谢您的回答。现在我必须更改 viewHolder 并在其中设置 textview,对吗? 可以,你可以在ViewHolder里面初始化TextView。以上是关于指定的孩子已经有一个父母,RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章
冲突的 Android 错误消息:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。 (C#)
指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView() (Android)
Fragments - 指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
Android - ArrayAdapter<LinearLayout> :指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()