如何在不同的组名下保存多个arraylist然后一起检索它?

时间:2017-05-30 15:51:00

标签: java android arraylist

我是Android应用开发的新手。我试图保存一组包含'ArrayList'的名称。我知道如何检索它,但我不知道如何同时为多个组名称执行此操作,例如在ListView中打开。如果有办法可以帮助我。

public class CreateList extends ListActivity {

    /** Items entered by the user is stored in this ArrayList variable */
    public static ArrayList<String> list = new ArrayList<String>();

    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter<String> adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Setting a custom layout for the list activity */
        setContentView(R.layout.activity_create_list);

        /** Reference to the button of the layout main.xml */
        Button btn = (Button) findViewById(R.id.btnAdd);

        /** Defining the ArrayAdapter to set items to ListView */
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        /** Defining a click event listener for the button "Add" */
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.txtItem);
                String check = edit.getText().toString();
                if (!(check.isEmpty())) {
                    list.add(edit.getText().toString());
                    edit.setText("");
                    adapter.notifyDataSetChanged();
                }
            }
        };

        /** Setting the event listener for the add button */
        btn.setOnClickListener(listener);

        /** Setting the adapter to the ListView */
        setListAdapter(adapter);
    }

    public ArrayList<String> GroupList(){
        return list;
    }

    public void SaveFile(View view){
        EditText GroupSave = (EditText) findViewById(R.id.groupName);
        String GroupName = GroupSave.getText().toString();
        if (!(GroupName.isEmpty())){
            try{
                FileOutputStream fos = openFileOutput(GroupName, Context.MODE_PRIVATE);
                ObjectOutputStream os = new ObjectOutputStream(fos);
                os.writeObject(list);
                os.close();
                Log.v("MyApp","File has been written");
            }catch(Exception ex){
                ex.printStackTrace();
                Log.v("MyApp","File didn't write");
            }
        } else {
            Toast.makeText(this,"Please enter group name.",Toast.LENGTH_SHORT).show();
        }

    }


    public void GoToNextPage(View view){
        Intent nextClass = new Intent(CreateList.this, FoodCosting.class);
        if (nextClass.resolveActivity(getPackageManager())!=null){
            startActivity(nextClass);
        }
    }

    public void clearAll(View view){
        adapter.clear();
        //listview.getAdapter().notifyDataSetChanged();
    }
}

上面是我的CreateList Code,在这个SaveFile中是保存方法。

这是我的XML文件。每次我可以在不同的组名下保存,那么我想要的是在一个活动中检索所有组。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/groupName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/group"
        android:inputType="textCapSentences"
        android:layout_alignParentTop="true"/>

    <EditText
        android:id="@+id/txtItem"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="@string/hintTxtItem"
        android:inputType="textCapSentences"
        android:layout_below="@id/groupName"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/txtItem"
        android:background="@drawable/mybutton"
        android:onClick="clearAll"
        android:layout_below="@id/groupName"
        android:text="Clear All" />



    <View
        android:id="@+id/centerShim"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:visibility="invisible"
        android:layout_centerHorizontal="true"/>

    <Button
        android:id="@+id/btnAdd2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/mybutton"
        android:onClick="GoToNextPage"
        android:text="Next" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/centerShim"
        android:layout_above="@id/btnAdd2"
        android:background="@drawable/mybutton"
        android:text="@string/lblBtnAdd" />

    <Button
        android:id="@+id/bSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnAdd2"
        android:layout_toRightOf="@id/centerShim"
        android:background="@drawable/mybutton"
        android:onClick="SaveFile"
        android:text="Save Group" />

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/bSave"
        android:layout_below="@id/txtItem"
        android:fillViewport="true">

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </ScrollView>

</RelativeLayout>

0 个答案:

没有答案