2015-06-21 58 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(); 
} 
相關問題