我使用objectinputstream或fobjectoutputstream收到错误。我的矩形(视图)也不会改变位置

时间:2019-05-18 16:35:38

标签: java android objectinputstream objectoutputstream

因此,我希望每次将“表”的位置以我的可重排布局移动时,它们的位置都将保存到文件中。然后,当再次打开该应用程序时,该应用程序将使用关闭该应用程序之前的位置,将当前“表”替换为文件中保存的那些“表”。

这是我的MainActivity

package res.takiisushi.tablereservationsystem;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;

import com.rajasharan.layout.RearrangeableLayout;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class RestaurantLayoutMainActivity extends AppCompatActivity{
    private static final String TAG = "REST-REARRANGEABLE-LOUT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().setTitle(getString(R.string.table_layout_title));

        setContentView(R.layout.restaurant_main);

        final RearrangeableLayout root = findViewById(R.id.restaurant_layout);
        root.setChildPositionListener(new RearrangeableLayout.ChildPositionListener() {
            @Override
            public void onChildMoved(View childView, Rect oldPosition, Rect newPosition) {
                Log.d(TAG, childView.toString());
                Log.d(TAG, oldPosition.toString() + " -> " + newPosition.toString());
                saveTable(root.getContext(), childView);
            }
        });

        getSavedTables(root);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.example_menu, menu);
        return true;
    }

    public void saveTable(Context context,View viewTable){
        try {
            // create a new file with an ObjectOutputStream
            FileOutputStream out = context.openFileOutput("tables", MODE_APPEND);
            ObjectOutputStream oout = new ObjectOutputStream(out);

            // write something in the file
            oout.writeObject(viewTable);
            oout.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getSavedTables(RearrangeableLayout parent){
        ArrayList<View> al = new ArrayList<>();
        boolean cont = true;
        try {
            // create an ObjectInputStream for the file we need to read from
            ObjectInputStream ois = new ObjectInputStream(parent.getContext().openFileInput("tables"));

            // read objects from file
            while(cont){
                View view=null;
                try {
                    view = (View) ois.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                if(view != null)
                    al.add(view);
                else
                    cont = false;
            }

            if (!al.isEmpty()){
                View childView;

                for (int count=0; count<= al.size()-1; count++){
                    for (int counter=0; counter <= parent.getChildCount()-1; counter++){
                        childView = parent.getChildAt(counter);

                        //check if view on file is same as child view
                        if (getResources().getResourceEntryName(parent.getChildAt(counter).getId()) == getResources().getResourceEntryName(al.get(count).getId())){
                            //check layout parameters are same or different
                            if (al.get(count).getLayoutParams() != parent.getChildAt(counter).getLayoutParams()){
                                parent.removeView(childView);
                                parent.addView(al.get(count));
                                /*parent.getChildAt(counter).setLeft(al.get(count).getLeft());
                                parent.getChildAt(counter).setTop(al.get(count).getTop());
                                parent.getChildAt(counter).setRight(al.get(count).getRight());
                                parent.getChildAt(counter).setBottom(al.get(count).getBottom());*/
                            }
                        }

                    }
                }
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

这是我可重新排列的布局

<?xml version="1.0" encoding="utf-8"?>
<com.rajasharan.layout.RearrangeableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/restaurant_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/light_woodfloor"
    android:clipToPadding="false"
    android:padding="12dp"
    app:outlineColor="@color/colorAccent"
    app:outlineWidth="2dp"
    app:selectionAlpha="0.5"
    app:selectionZoom="1.2">

    <!-- add child views with `android:id` attr to
        save position during orientation change -->
    <FrameLayout
        android:id="@+id/table1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table1"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table1TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table1" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table2"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table2TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table2" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table3"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table3TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table3" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_4"
        android:clickable="true"
        android:contentDescription="@string/table4"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table4TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table4" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table5"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table5TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table5" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table6"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table6TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table6" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_window"
        android:clickable="true"
        android:contentDescription="@string/table7"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table7TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table7" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_window"
        android:clickable="true"
        android:contentDescription="@string/table8"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table8TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table8" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table9"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table9TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table9" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table10"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table10TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table10" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table11"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table11TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table11" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table12"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table12TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table12" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table13"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table13TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table13" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table14"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table14TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table14" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table15"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table15TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table15" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table16"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table16TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table16" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table17"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table17TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table17" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table18"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table18TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table18" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table19"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table19TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table19" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table20"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table20TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table20" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table"
        android:clickable="true"
        android:contentDescription="@string/table21"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table21TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table21" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table22"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table22TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table22" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table23"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table23"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table23TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table23" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table24"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table24"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table24TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table24" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table25"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table25TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table25" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table26"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table26"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table26TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table26" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table27"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_27"
        android:clickable="true"
        android:contentDescription="@string/table27"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table27TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table27" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table28"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_28"
        android:clickable="true"
        android:contentDescription="@string/table28"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table28TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table28" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table29"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table29"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table29TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table29" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/table30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/table_small"
        android:clickable="true"
        android:contentDescription="@string/table30"
        android:focusable="true"
        android:foregroundGravity="center"
        android:padding="8dp">

        <TextView
            android:id="@+id/table30TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/table30" />
    </FrameLayout>

    <!-- more child views -->

</com.rajasharan.layout.RearrangeableLayout>

Here是我正在使用的可重排布局。

我尝试使用view.layout(l,t,r,b)。没有移动桌子。我也尝试过setLayoutParameters(params)。我还尝试使用可重新排列的布局LayoutParameters设置.top和.left。我什至尝试使用SharedPreferences。保存到共享首选项即可。应用值有效,但布局本身未更改。如果我移动表格,每次都会显示相同的oldPosition。

0 个答案:

没有答案