2015-06-21 85 views
0

我有一个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. 

我在做什么是错的?我怎么解决这个问题 ? 感谢

回答

0

我认为KTS更容易使用2个独立的XML文件,并将它们之间进行选择,而不是添加额外的意见

0

您创建(膨胀),只有一个新的观点,您需要创建在每次迭代中新。类似这样的:

for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) { 
    View v2 = vi.inflate(R.layout.curstomrow, null,false); 
    holder.extra.addView(v2); 
    holder.extra.refreshDrawableState(); 
} 
相关问题