【问题标题】:Android: How to save listview in root directoryAndroid:如何将列表视图保存在根目录中
【发布时间】:2018-08-01 04:49:23
【问题描述】:

我正在制作一个蓝牙聊天应用程序,它在名为“list”的 ListView 中显示聊天。 我是 android 新手,不知道如何以 .txt 格式将 listview 保存到根目录。我尝试搜索,但结果超出了我的想象。

这是我要实现保存选项的部分:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.save:
            //add the function to perform here
            return(true);
    }
    return(super.onOptionsItemSelected(item));
}

另外,以下是我在我的 .java 文件之一中声明列表的方式:

listView = (ListView) findViewById(R.id.list);

我在清单文件中使用了以下权限:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我什至不知道如何开始。

【问题讨论】:

  • 你想在内部存储中保存聊天记录吗?
  • 您不会保存列表视图。列表视图是一个 UI 组件。您可以将支持列表视图的数据(应该在其自己的模型类中)保存到文件或数据库中。

标签: java android listview save menu-items


【解决方案1】:

是否要将列表视图的数据保存到内部存储中? 因为我们不能直接将视图对象存储到内存中。

【讨论】:

    【解决方案2】:
     try {
    
        File file = new File("D://filename.txt");
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        for(String str: movementArray){    
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.append(str);  
            bw.close();
        }
    }
    
        catch (IOException e) {
        e.printStackTrace();
        }
        finally {
        writer.close();
        }
    

    【讨论】:

    • movementArray 给出了无法解析符号的错误。另外如何将变量“listView”内容复制到“str”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2021-10-27
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多