这是我的主要活动:我正在尝试从外部存储中读取文件,之前我从原始文件夹中提取它。一切都很好,直到从原始文件夹中选择它,现在我的自定义列表视图为空。
public class MainActivity extends AppCompatActivity {
InputStream inputStream;
//String uri;
String[] data;
Vector<String> contacts = new Vector<String>();
String uri = Environment.getExternalStorageDirectory().toString();
String location = uri+"/contacts.csv";
//String[] contacts={"hello","world","ge","rg","e","er"};
int j = 0;
TextView tv1;
/* String fileName = "contacts.csv";
String path = Environment.getExternalStorageDirectory()+"/"+fileName;
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file); */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1= (TextView) findViewById(R.id.tv1);
tv1.setText(location);
// inputStream = getResources().openRawResource(R.raw.contacts);
File file = new File(location);
try {
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
data = csvLine.split(",");
for (int i = 0; i < 2; i++) {
contacts.add(new String(data[i]));
j++;
}
}
try {
ListAdapter customAdapter = new CustomAdapter(this, contacts);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(customAdapter);
} catch (Exception e) {
Log.e("Problem", e.toString());
}
} catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: " + ex);
}
} catch (FileNotFoundException fe) {
}
}
}