2014-01-07 52 views
0

起初我試圖重新調整我的LinearLayout並以編程方式查看發佈在這個問題上。如何減輕指定的孩子已經有父母(IllegalStateException)錯誤?

Adjusting linear layout programatically

如何過這提出了另一個例外,特別是IllegalStateException異常(稱「指定的孩子已經有父」)在我的適配器發生特別是在這個部分(第二if語句

錯誤):

public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    if (row == null) { 
      LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.chat_item, parent, false); 
    } 

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

    textChat = (TextView) row.findViewById(R.id.textMessage); 
    thumbPhoto = (ImageView) row.findViewById(R.id.thumbPhoto); 

    ChatString chat = getItem(position); 
    if(chat.mLeft){ 
     textChat.setText(chat.mText); 
    }else{ 
     wrapper.removeViewAt(1); 
     wrapper.addView(thumbPhoto, 0); 
     textChat.setText(chat.mText); 
    } 
    return row; 
} 

這是適配器是如何其根視圖中使用(我懷疑這會是罪魁禍首,但我不肯定就做什麼吧):

這段代碼駐留在onPostExecute()內的的AsyncTask

Result is an ArrayList<Chat> 

for(Chat chat : result){ 
    if(chat.senderId != mAccount.accId){ 
     adapter.add(new ChatString(false, chat.chatMessage)); 
    }else{ 
     adapter.add(new ChatString(true, chat.chatMessage)); 
    } 
} 

這個適配器功能的簡要說明:

猜想新的「聊天泡泡」添加到該適配器在其根視圖中的列表視圖。

任何人都可以幫助我嗎?

編輯1 我嘗試了以下解決方案(仍然沒有運氣):

// Removing the parent. 
if(chat.mLeft){ 
    textChat.setText(chat.mText); 
}else{ 
    View view = (View) wrapper.getParent(); 
    wrapper.removeView(view); 
    wrapper.removeViewAt(1); 
    wrapper.addView(thumbPhoto, 0); 
    textChat.setText(chat.mText); 
} 

if (row == null) { 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.chat_item, null); <---- CHANGING THIS BIT 
} 
+0

'wrapper.addView(thumbPhoto,0);'thumbPhoto已經具有父,因爲它是在一行。 – njzk2

+0

你想在這裏做的是有2個不同的佈局,並使用getViewTypeCount和getItemViewType來聲明2種不同的類型。 – njzk2

+0

@ njzk2你在暗示什麼?你能提供一個代碼示例嗎? – Jeremy

回答

1

您可以編輯getView這樣:

public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    ChatString chat = getItem(position); 
    if (row == null) { 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     // Here, inflate 2 different layouts 
     if(chat.mLeft){ 
      row = inflater.inflate(R.layout.chat_item, parent, false); 
     } else { 
      row = inflater.inflate(R.layout.chat_item_right, parent, false); 
     } 
    } 

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

    textChat = (TextView) row.findViewById(R.id.textMessage); 
    thumbPhoto = (ImageView) row.findViewById(R.id.thumbPhoto); 

    // Here, just set the text 
    textChat.setText(chat.mText); 
    return row; 
} 

,並表明觀點回收爲您提供一致的視圖,添加到您的適配器:

@Override 
public int getItemViewType(int position) { 
    ChatString chat = getItem(position); 
    if (chat.mLeft) { 
     return 0; 
    } else { 
     return 1; 
    } 
} 

而且

@Override 
public int getViewTypeCount() { 
    return 2; 
} 
+0

謝謝...謝謝...謝謝.....它的作品...我不知道有這樣的事情getItemViewType&getViewTypeCount .... – Jeremy

相關問題