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