【问题标题】:List View to display contacts from txt file-Android Studio列表视图显示来自 txt 文件的联系人-Android Studio
【发布时间】:2016-05-05 09:32:43
【问题描述】:

您好,我需要创建一个列表视图来显示存储在文本文件中的所有联系人。要添加联系人,用户必须自己输入详细信息,因此这些信息将保存在文本文件中。现在我需要创建一个列表视图来在列表中显示这些联系人,并且每个联系人都会显示一个按钮。至于现在我的代码所做的只是在 Toast 中显示联系人。

public void ViewContacts(View v)
{
    //reading contacts from textfile
    try{
        FileInputStream fileIn=openFileInput("mytextfile.txt");
        InputStreamReader InputRead=new InputStreamReader(fileIn);

        char[] inputBuffer=new char[READ_BLOCK_SIZE];
        String s="";
        int charRead;

        while((charRead=InputRead.read(inputBuffer))>0)
        {
            //char to string conversion
            String readstring=String.copyValueOf(inputBuffer,0,charRead);
            s+=readstring;
        }
        InputRead.close();
        Toast.makeText(getBaseContext(),s,Toast.LENGTH_SHORT).show();
    } catch(Exception e)
    {
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java android listview text-files


    【解决方案1】:

    您可以将所有联系人详细信息存储在 json 字符串类型的单个文本文件中。然后解析该 json 字符串并在需要时创建一个数据列表。在列表视图中填充该列表。

    You can follow this link to convert string to json

    Parse your json data - example

    【讨论】:

    • 这件事是我需要它来接受用户输入。我能在你给我的例子中做到这一点吗?抱歉,我对此有点陌生:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2011-05-24
    • 2013-08-14
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多