简体   繁体   中英

Android - addView() doesn't work after removeAllViews()

I have a LinearLayout ll inside the other LinearLayout inside the ScrollView


<ScrollView
    android:layout_width="270px"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/gifcreator_thumbs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical" />
        <Button
            android:id="@+id/gifcreator_add"
            android:layout_width="250px"
            android:layout_height="125px"
            android:layout_margin="10px"
            android:textColor="#000000"
            android:text="Добавить кадр" />
    </LinearLayout>
</ScrollView>

In onCreate I'm running the update_frames() method:


public void update_frames(String a)
{
    Log.e("EmSy", "Updating Frame @ " + a);
    try
    {
        File f = new File(a);
        tag = ll.getChildCount();
        View v = getLayoutInflater().inflate(R.layout.gifcreator_item, null);
        v.setTag(tag);
        v.setOnLongClickListener(new OnLongClickListener() { 
                public boolean onLongClick(View v)
                {
                    delete_frame(Integer.parseInt(v.getTag().toString()));
                    return true;
                }
            });

        ImageView thumb = (ImageView)v.findViewById(R.id.gircreator_item_thumb);
        TextView desc = (TextView)v.findViewById(R.id.gifcreator_item_desc);

        Bitmap t = BitmapFactory.decodeFile(a);
        Bitmap t2 = Bitmap.createScaledBitmap(t, 110, 110, false);
        thumb.setImageBitmap(t2);

        desc.setText("Кадр №" + tag);

        data.add(new GIFFrame(t, 42, a));

        ll.addView(v);
    }
    catch (Exception e)
    {
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

Here's the delete_frame method:


public void delete_frame(int tag_for_deleting)
{
    try
    {
        ll.removeAllViews();
        data.remove(tag_for_deleting);
        temp_data = data;
        data.clear();

        for (GIFFrame gf : temp_data)
        {
            update_frames(gf.p);
            frames.notify();
        }

        temp_data.clear();
    }
    catch (Exception e)
    {
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

So, before executing delete_view method all views has been added, but after executing that method the views aren't adding to the ll . My code stops on the ll.addView(v) line, so I have the rigth path to the file in my LogCat:


`ru.mso.gb - EmSy - Updating Frame @ /sdcard/pic.png`

Why it isn't working?

in your code, when you longpress a view, the main idea is to delete THE view, though you seem to remove all views, delete_frame(int tag_for_deleting) should remove one view, not all the views, as made by this line ll.removeAllViews();

get the index of your view, and execute this instead

ll.removeViewAt(index)

EDIT: After reading your code again, it looks like when you want to delete 1 single line, you delete everything and build your layout from scratch, this is BAD , very BAD, instead, remove the one view you want to remove by the line I posted above, and remove the one info from the arraylist, that's it

Problem here

temp_data = data;
data.clear();

You just add new reference to data, then clear it. This means code in the loop never run.

You can do the following replace

temp_data = data;

with

temp_data = new ArrayList<your type here>(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM