我有一個listview,我用一個適配器來添加數據。我想添加一些textviews動態somerows,這是我的代碼:android指定的孩子已經有父母。您必須先調用子對象的父對象的removeView()。 on listView adaper
public class TimeListAdapter extends CursorAdapter {
public TimeListAdapter(Context context, Cursor c) {
super(context, c);
}
public class ViewHolder {
LinearLayout extra;
public ViewHolder(View row) {
extra = (LinearLayout) row.findViewById(R.id.extras);
}
}
@Override
public View newView(Context context, Cursor arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.sabadrow, arg2, false);
ViewHolder holder = new ViewHolder(row);
row.setTag(holder);
return row;
}
@Override
public void bindView(View v, Context context, Cursor c) {
ViewHolder holder = (ViewHolder) v.getTag();
holder.extra.setTag(c.getInt(0));
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v2 = vi.inflate(R.layout.curstomrow, null,false);
TextView tvcheck = (TextView) v2.findViewById(R.id.tvcheck);
tvcheck.setTypeface(typeface);
Cursor c2=db.getServices(c.getInt(0));
int i=0;
for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) {
tvcheck.setText(c2.getString(3));
holder.extra.addView(v2);
holder.extra.refreshDrawableState();
}
}
}
當我運行我的應用程序,我得到這些錯誤:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我在做什麼是錯的?我怎麼解決這個問題 ? 感謝