Reputation: 9
im trying to addView's and Remove View's Depending on my Need From my ListView Adapter, by Calling :
holder.wrapper.removeView(holder.PimageView);
holder.wrapper.removeView(holder.theMessage);
and
holder.wrapper.addView(holder.PimageView);
depending on my need, when convertView is null (First time they load) it works find, but when i refresh that (ConvertView not null) im getting this error:
07-05 14:00:36.077: E/AndroidRuntime(2019): FATAL EXCEPTION: main
07-05 14:00:36.077: E/AndroidRuntime(2019): Process: com.lifemate.lmmessenger, PID:
2019
07-05 14:00:36.077: E/AndroidRuntime(2019): java.lang.IllegalStateException: The
specified
child already has a parent. You must call removeView() on the child's parent first.
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.view.ViewGroup.addView(ViewGroup.java:3415)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.view.ViewGroup.addView(ViewGroup.java:3360)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.view.ViewGroup.addView(ViewGroup.java:3336)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
com.lifemate.lmmessenger.listviewengine.ChatAdapter.getView(ChatAdapter.java:249)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.widget.AbsListView.obtainView(AbsListView.java:2240)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.widget.ListView.makeAndAddView(ListView.java:1790)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.widget.ListView.fillUp(ListView.java:725)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.widget.ListView.layoutChildren(ListView.java:1611)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.widget.AbsListView.onLayout(AbsListView.java:2091)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.view.View.layout(View.java:14817)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.view.ViewGroup.layout(ViewGroup.java:4631)
07-05 14:00:36.077: E/AndroidRuntime(2019): at
android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
and by the way the last row says RelativeLayout.onLayout, but i my xml is FrameLayout with a LinearLayout child, is this normal?
this is my code:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, null);
this.convertview=convertView;
System.out.println("ConverView null");
holder.PimageView = (ImageView)convertView.findViewById(R.id.PimageView);
holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper);
holder.theMessage = (TextView) convertView.findViewById(R.id.comment);
holder.theName = (TextView) convertView.findViewById(R.id.MSGname);
holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage);
holder.lp = (FrameLayout.LayoutParams) holder.theName.getLayoutParams();
holder.paramsleft = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT);
holder.paramsright = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
String theType="Known";
if(holder.badge!=null){
holder.badge.setVisibility(View.GONE);
holder.badge.invalidate();
holder.badge=null;
}
mCursor.moveToPosition(position);
String imagenamer=
(mCursor.getString(mCursor.getColumnIndex("username")).split("\\@"))[0];
int isright= Integer.valueOf(mCursor.getString(mCursor.getColumnIndex("isright")));
holder.wrapper.removeView(holder.PimageView);
holder.wrapper.removeView(holder.theMessage);
// here both DependantViews are removed
//Condition 1 :
holder.wrapper.addView(holder.PimageView);
//Condition 2 :
holder.wrapper.addView(holder.theMessage);
these methods are where Error happenes, i have tried View.GONE, and View.VISIBLE, but after scrolling listview gets mixed up and leaves the layout Space Reserved to the PImageView empty when its supposed to be GONE, like when you call View,INVISIBLE.
here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/MSGname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
<ImageView
android:id="@+id/MSGimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
/>
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/PimageView"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_margin="5dip"
/>
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:background="@drawable/bubble_yellow"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
the holder:
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, null);
this.convertview=convertView;
System.out.println("ConverView null");
holder.PimageView = (ImageView)convertView.findViewById(R.id.PimageView);
holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper);
holder.theMessage = (TextView) convertView.findViewById(R.id.comment);
holder.theName = (TextView) convertView.findViewById(R.id.MSGname);
holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage);
holder.lp = (FrameLayout.LayoutParams) holder.theName.getLayoutParams();
holder.paramsleft = new FrameLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT);
holder.paramsright = new FrameLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
and the Viewholder:
public static class ViewHolder {
private TextView theMessage;
private TextView theName;
private LinearLayout wrapper;
private ImageView PimageView;
ImageView theImage;
ImageView theImage2;
FrameLayout.LayoutParams lp;
FrameLayout.LayoutParams paramsleft;
FrameLayout.LayoutParams paramsright;
BadgeView badge ;
VideoView PvideoView;
}
sorry if its repeated, but some body asked precisely
any help guys what is wrong? thanks alot
Upvotes: 0
Views: 794
Reputation: 83517
You are incorrectly implementing the ViewHolder pattern. Since the views you get with findViewById()
are already children of a LinearLayout
, you don't need to call addView()
at all.
Upvotes: 1