添加的视图未被删除甚至removeAllViews()被调用

时间:2012-10-16 03:10:00

标签: android textview android-linearlayout relativelayout

即使在调用removeAllviews()之后,我仍然遇到删除视图的问题。 onView我正在将textView和imageView添加到布局中。下次当我点击textView之前显示的textview和imageview没有被删除,并且在调用removeAllViews()之后附加了新闻。

感谢任何帮助。

代码是

活动代码

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.check);

            content=(LinearLayout)findViewById(R.id.content);
            today=(TextView)findViewById(R.id.today);
            tomorrow=(TextView)findViewById(R.id.tomorrow);

            today.setOnClickListener(new TextView.OnClickListener() {                  

                @Override                 
                public void onClick(View v) {                     
                    // TODO Auto-generated method stub  
                    //Toast.makeText(ToDo.this, "Before"+content.getChildCount(), Toast.LENGTH_LONG).show();      
                    content.removeAllViews();                      
                    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
                    int id=0;                                              
                    for (int idx = 0; idx < 4; idx++) {                             
                        id = idx + 1;                             
                        rel = (RelativeLayout) inflater.inflate(R.layout.data, null); 
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);                             
                        params.setMargins(0, 50, 0, 0);                             
                        txt = (TextView) rel.findViewById(R.id.txt);                             
                        txt.setText("AAA"+idx);                              
                        img = (ImageView) rel.findViewById(R.id.img);                             
                        img.setImageResource(R.drawable.stub);                             
                        img.setId(id);                              
                        img.setOnClickListener(new ImageView.OnClickListener() {                                  
                            @Override                                 
                            public void onClick(View v) {                                     
                                // TODO Auto-generated method stub                                     
                                Toast.makeText(ToDo.this,"Selected" + v.getId(),Toast.LENGTH_LONG).show();       
                                }                             
                            });                              
                        content.addView(rel, params);                          
                        }                                           
                    content.setVisibility(View.VISIBLE);                     
                    Toast.makeText(ToDo.this, ""+content.getChildCount(), Toast.LENGTH_LONG).show();                                      
                    }             
                });         

            tomorrow.setOnClickListener(new TextView.OnClickListener() {                  

                @Override                 
                public void onClick(View v) {                     
                    // TODO Auto-generated method stub  
                    //Toast.makeText(ToDo.this, "Before"+content.getChildCount(), Toast.LENGTH_LONG).show();      
                    content.removeAllViews();                      
                    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
                    int id=0;                                              
                    for (int idx = 0; idx < 4; idx++) {                             
                        id = idx + 1;                             
                        rel = (RelativeLayout) inflater.inflate(R.layout.data, null); 
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);                             
                        params.setMargins(0, 50, 0, 0);                             
                        txt = (TextView) rel.findViewById(R.id.txt);                             
                        txt.setText("Tomo"+idx);                              
                        img = (ImageView) rel.findViewById(R.id.img);                             
                        img.setImageResource(R.drawable.stub);                             
                        img.setId(id);                              
                        img.setOnClickListener(new ImageView.OnClickListener() {                                  
                            @Override                                 
                            public void onClick(View v) {                                     
                                // TODO Auto-generated method stub                                     
                                Toast.makeText(ToDo.this,"Selected" + v.getId(),Toast.LENGTH_LONG).show();       
                                }                             
                            });                              
                        content.addView(rel, params);                          
                        }                                           
                    content.setVisibility(View.VISIBLE);                     
                    Toast.makeText(ToDo.this, ""+content.getChildCount(), Toast.LENGTH_LONG).show();                                      
                    }             
                });         
            }

check.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="250dp"
        android:id="@+id/whiteboard"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:background="#FFFFFF" >

        <Button
            android:id="@+id/add"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="17dp"
            android:layout_marginLeft="20dp"
            android:text="Add" />

        <View
            android:id="@+id/bottomseperator"
            android:layout_width="200dp"
            android:layout_height="1dp"
            android:layout_above="@+id/add"
            android:layout_alignLeft="@+id/add"
            android:layout_marginBottom="10dp"
            android:background="#000000" />

        <View
            android:id="@+id/topseperator"
            android:layout_width="220dp"
            android:layout_height="1dp"
            android:layout_marginTop="35dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="#000000" />

        <TextView android:id="@+id/day"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="8dp"
            android:textSize="15sp"
            android:text="Day :"/>

        <TextView
            android:id="@+id/today"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/day"
            android:layout_alignBottom="@+id/day"
            android:layout_toRightOf="@+id/day"
            android:textSize="15sp"
            android:text="Today /" />

        <TextView
            android:id="@+id/tomorrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/today"
            android:layout_alignBottom="@+id/today"
            android:layout_toRightOf="@+id/today"
            android:textSize="15sp"
            android:text="Tomorrow /" />

         <TextView
            android:id="@+id/custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/tomorrow"
            android:layout_alignBottom="@+id/tomorrow"
            android:layout_toRightOf="@+id/tomorrow"
            android:textSize="15sp"
            android:text="Custom" />

         <ScrollView android:id="@+id/contenntscroll"
             android:layout_width="200dip"
             android:layout_height="200dip"
             android:layout_alignBottom="@+id/whiteboard"
             android:layout_alignTop="@+id/whiteboard"
             android:layout_marginLeft="20dp"
             android:layout_marginTop="10dp"
             android:layout_below="@+id/topseperator"
             android:isScrollContainer="false"
             android:scrollbars="none">

              <LinearLayout
                android:id="@+id/content"
                android:layout_width="200dp"
                android:layout_height="300dp"
                android:layout_marginTop="30dp"
                android:orientation="vertical" >
            </LinearLayout>
         </ScrollView>
    </RelativeLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

这可能是问题但我不确定,

在'今日'视图的 onClickListener 中,设置ID的逻辑错误

尝试使用以下内容,

id = idx + 4; // idx can be 0, 1, 2, 3... and id can be 4, 5, 6, 7...<br/>

您使用的逻辑会覆盖可能会混淆操作系统的ImageViews和TextViews的ID。


如果这不能解决问题,请尝试在日志中打印

Log.d("ChildCount", "content child count:" + content.getChildCount());

之前的

 content.removeAllViews();

并用它更新您的问题......