gpt4 book ai didi

android - 指定的 child 已经有一个 parent ,RecyclerView

转载 作者:行者123 更新时间:2023-11-29 15:03:00 25 4
gpt4 key购买 nike

那是我的适配器:

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();
}
}

这是 fragment :

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: The specified child already has a parent. You must call removeView() on the child's parent first.

最佳答案

只是替换

ViewHolder vh = new ViewHolder(itemList);

ViewHolder vh = new ViewHolder(v);

onCreateViewHolder(ViewGroup parent, int viewType) 中。

因为 itemListv 的子项。

关于android - 指定的 child 已经有一个 parent ,RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41505378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com