起初我試圖重新調整我的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
}
'wrapper.addView(thumbPhoto,0);'thumbPhoto已經具有父,因爲它是在一行。 – njzk2
你想在這裏做的是有2個不同的佈局,並使用getViewTypeCount和getItemViewType來聲明2種不同的類型。 – njzk2
@ njzk2你在暗示什麼?你能提供一個代碼示例嗎? – Jeremy