是否可以通过内部存储将String arraylist保存在类中?

时间:2019-07-18 15:12:03

标签: java android class arraylist android-internal-storage

我正在编写一个笔记应用程序,并试图将String arraylist保存在内部存储中,但它似乎不起作用 用户正在创建和编辑表,而我试图将内容保存在arraylist中,这是行不通的。 有人知道

我创建了一个类,并另存为“ .bin”文件

  

假定存储数组列表的函数:

      ArrayList<String> tableLayoutBcontent=new ArrayList<>();
      ArrayList<String> tableLayoutCcontent=new ArrayList<>();
      ArrayList<String> tableLayoutDcontent=new ArrayList<>();


      TableRow tableRow1=(TableRow)tableLayoutB.getChildAt(0);
      EditText editText, editText1;
      int tableCellsCountD=0;

     for(int i=0;i<tableColumnCountB;i++){
         editText1= (EditText) tableRow1.getChildAt(i).getTag();
         tableLayoutBcontent.add(editText1.getText().toString());

         for(int j=0;j<tableRowCountC;j++) {

             tableRow= (TableRow)tableLayoutD.getChildAt(j);
             Log.d("In create table", "number of rows and columns"+tableColumnCountB + i + j);
             Log.d("In create table", "number of child counts"+tableRow.getChildCount()+tableRow1.getChildCount());
             editText=(EditText)tableRow.getChildAt(i).getTag();
             Log.d("In create table", "Content"+editText.getText().toString());
             tableLayoutDcontent.add(editText.getText().toString());
             tableCellsCountD++;
         }

     }


      for(int i=0;i<tableRowCountC;i++){
         tableRow=(TableRow)tableLayoutC.getChildAt(i);
         editText=(EditText)tableRow.getChildAt(0).getTag();
         tableLayoutCcontent.add(editText.getText().toString());
      }

      Table table= new Table(TableTitle, tableLayoutAcontent, tableLayoutBcontent, tableLayoutCcontent, tableLayoutDcontent, tableRowCountC, tableColumnCountB);
     Log.d("In get table layout", "Check");
     return table;

  }  
  

应该恢复阵列列表的功能:

public void setTableLayoutData(Table table){
     Title.setText(TableTitle);
     TableLayoutBcontent= table.getTableLayoutBcontent();
     TableLayoutCcontent= table.getTableLayoutCcontent();
     TableLayoutDcontent= table.getTableLayoutDcontent();

    InitialColumnsNumber = table.getColumnsNumber();
    InitialRowsNumber = table.getRowsNumber();

    for(int i = 0; i< InitialColumnsNumber; i++){
        addColumnsToTableB(TableLayoutBcontent.get(i),i);
        Log.d("Check content", "TablelayoutB content:" + TableLayoutBcontent.get(i));
    }
    for(int i = 0; i< InitialRowsNumber; i++){
        initializeRowForTableD(i);
        addRowToTableC(TableLayoutCcontent.get(i));
        Log.d("Check content", "TablelayoutC content:" + TableLayoutCcontent.get(i));

        for (int j = 0; j< InitialColumnsNumber; j++){
            addColumnToTableD(i, TableLayoutDcontent.get(i));
        }
    }

}```
  

和保存表的功能:

 public void SaveTable(){
        Table table=getTableLayoutData();
        Idea idea=Utilities.getIdea(IdeaTitle);

        if(table!=null) {
            Log.d("Idea'S title", IdeaTitle);
            idea.AddTable(table);
        }

        if(Utilities.SaveIdea(this, idea)){
            Toast.makeText(this, "Your table is saved in your idea", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(this, "your table couldn't be saved for some reason", Toast.LENGTH_SHORT).show();
        }
    }

我希望它可以存储内容,但无法正常工作, 有人可以尝试帮助我吗?

编辑:

  

想法类代码:

public class Idea implements Serializable{
    private long Date;
    private String Title;
    private String Content;
    private ArrayList<Table> Tables;


    public Idea(long date, String title, String content) {
        Date = date;
        Title = title;
        Content = content;
        Tables=new ArrayList<>();

    }

    public Idea(long date, String title, String content, ArrayList<Table> tables) {
        Date = date;
        Title = title;
        Content = content;
        Tables = tables;
    }

    public long getDate() {
        return Date;
    }

    public String getTitle() {
        return Title;
    }

    public String getContent() {
        return Content;
    }

    public void setDate(long date) {
        Date = date;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public void setContent(String content) {
        Content = content;
    }

    public ArrayList<Table> getTables() {
        return Tables;
    }

    public void setTables(ArrayList<Table> tables) {
        Tables = tables;
    }

    public void AddTable(Table table){
        Tables.add(table);
        Log.d("TablesCount", "tables count is "+Tables.size());
    }

    public Table getTable(String title){
        for (int i=0;i<Tables.size();i++){
            Log.d("Table name", Tables.get(i).getTitle());
            if(title.equals(Tables.get(i).getTitle())){
                return Tables.get(i);
            }
        }
        return null;
    }

    public String getTableTitle(int i){
        if(Tables.size()>i){
            return Tables.get(i).getTitle();
        }
        return null;
    }

    public boolean hasTable(){
        Log.d("hasTables", "TablesCount is "+Tables.size());
        return Tables.size() != 0;
    }

    public int getTablesCount(){
        return Tables.size();
    }




}
  

Utillities.saveIdea代码:

public static boolean SaveIdea(final Context context, final Idea idea){
        final String FileName=String.valueOf(idea.getTitle()+".bin");

            FileOutputStream fos;
            ObjectOutputStream oos;
            try {
                fos = context.openFileOutput(FileName, Context.MODE_PRIVATE);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(idea);
                Log.d("in save idea", "in try");
                oos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("in save idea", "in catch");
                Toast.makeText(context, "file wasn't saved, please check if you have enough storage space left on your device", Toast.LENGTH_SHORT).show();
                return false;
            }

        return true;
    }

0 个答案:

没有答案