简体   繁体   English

List View 显示txt文件中的联系人-Android Studio

[英]List View to display contacts from txt file-Android Studio

Hi i need to create a list view to display all the contacts which are stored in a text file.嗨,我需要创建一个列表视图来显示存储在文本文件中的所有联系人。 To add a contact the user must enter the details himself and so these will be saved in a text file.要添加联系人,用户必须自己输入详细信息,因此这些信息将保存在文本文件中。 Now I need to create a list view to display these contacts in a list and also for every a contact a button would be shown.现在我需要创建一个列表视图来在列表中显示这些联系人,并且对于每个联系人都会显示一个按钮。 As for now what my code does is simply show the contact in a Toast.至于现在我的代码所做的只是在 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();
    }
}

You can store all contact details in a single text file in a json string type.您可以将所有联系人详细信息以 json 字符串类型存储在单个文本文件中。 Then parse that json string and create a list of data whenever you need them back.然后解析该 json 字符串并在需要时创建数据列表。 Populate that list in a listview.在列表视图中填充该列表。

You can follow this link to convert string to json 您可以按照此链接将字符串转换为 json

Parse your json data - example 解析您的 json 数据 - 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文本文件的编辑文本视图中显示项目-Android Studio - Display items in edit text views from text file-Android Studio 如何从Android Studio中检索到的联系人列表中呼叫联系人? - How to call a contact from a retrieved list of contacts in Android Studio? 片段中的Android Studio列表视图不显示 - Android Studio List View in Fragment Not Display 抓取联系人并将其显示在列表视图中 - Grab contacts and display them into a list view 如何从Firebase正确连接并显示数据到自定义列表视图Android Studio - How to properly connect and display data from Firebase to custom list view Android Studio 从Google Firebase检索数据并在android studio应用程序的列表视图中显示 - Retrieve data from google firebase and display in list view in android studio app 需要从android studio中assets文件夹中的txt文件中读取 - Need to read from a txt file in the assets folder in android studio Android Studio 在小部件中从网络获取 txt 文件 - Android Studio get a txt file from web in widget 有没有办法在 Android Studio 的列表视图中显示连续数据? - Is there a way to display continuous data on a List View in Android Studio? 如何在列表视图中显示项目的文本Android Studio - How to display text for an item in a list view Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM