动态编辑CSV文件

时间:2018-04-10 14:56:43

标签: android

我从CSV文件构建回收站视图。单击按钮后,我需要编辑CSV文件中的字段并重新构建回收器视图。 Android文件系统是只读的,因此我必须创建一个位于外部存储目录中的新文件。但是当我从这个新文件开始构建Recycler视图时,它是空的。为什么呢?

(在此之前,我验证了存储权限)

从原始csv(" catalog.csv")创建rw文件(" rw_file.csv"):

public void createRWFile(Context context)
    {
        String source = "catalog.csv";
        File root = Environment.getExternalStorageDirectory();
        File rw_file = new File(root, "rw_file.csv");

        try
        {
            CSVReader reader = new CSVReader(new InputStreamReader(context.getAssets().open(source)),';');
            List<String[]> csvBody = reader.readAll();

            CSVWriter writer = new CSVWriter(new FileWriter(rw_file),';',' ');
            writer.writeAll(csvBody);
            writer.flush();
        }
        catch (FileNotFoundException e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
        }
        catch (IOException e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

读取新的csv文件并将其存储在LinkedList中:(此处必须是错误)

public void fillCatalog(String filename)
    {
        catalog.clear();    

        try {
            CSVReader reader = new CSVReader(new InputStreamReader(getContext().getAssets().open(filename)),';');
            List<String[]> csvBody = reader.readAll();

            for(String[] i : csvBody)
            {
                Book curr = new Book();

                curr.author = i[0];
                curr.title = i[1];
                curr.genre = i[4];
                curr.cover = getResources().getIdentifier(i[5], "drawable", "com.example.rese.biblioteca_3");
                curr.availability = getResources().getIdentifier(i[6], "drawable", "com.example.rese.biblioteca_3");

                if(curr.genre.equals("Classico"))
                    catalog.add(curr);
            }

        } catch (IOException e) {
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

创建Recycler View(在OnCreate方法中):

fillCatalog("catalog.csv");     //TODO se metto rw_file non funziona
mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.classici_rv);
mAdapter = new BookListAdapter(getContext(), catalog);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

1 个答案:

答案 0 :(得分:0)

我解决了我的问题!我无法修改文件(或者更好,很难),但我可以将所有数据存储在结构中,例如LinkedList),然后我可以编辑数据并从该结构构建回收器视图。 / p>

在MainActivity中填写结构

public void fillCatalog(String filename)
    {
        catalog.clear();   

        try {
            CSVReader reader = new CSVReader(new InputStreamReader(getApplicationContext().getAssets().open("catalog.csv")),';');
            List<String[]> csvBody = reader.readAll();

            for(String[] i : csvBody)
            {
                ...
                catalog.add(...);
            }

        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

在我的活动中,我填写了回收者视图:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.classici_tab, container, false);

        mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.classici_rv);
        mAdapter = new BookListAdapter(getContext(), MainActivity.catalog);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        return fragmentView;
    }

在某处我编辑数据,然后我回想起onCreateView来显示修改。