Android: Trouble with array that's imported from a file and converted from String
I have a text file that I am pulling from my SD card which contains an array in plain text format. Here are the contents of that file ...
http://www.oddree.com/rayhaque/android1.jpg,http://www.oddree.com/rayhaque/android2.jpg,http://www.oddree.com/rayhaque/android3.jpg,http://www.oddree.com/rayhaque/android4.jpg,http://www.oddree.com/rayhaque/android5.jpg
I am trying to import that text file into a String, convert that String to an array, and then load that array into a list adapter. If I try to split up readString or a trimmed result of readString, I get a forced close every time. If I copy the contents of the file into a String and then use that ... everything works as expected.
So what is the difference between loading this stuff from a text file, and loading it from an included string? Is it a byte conversion issue?
Here is my code. I have noted what works and what fails.
public class MainActivity extends Activity {
ListView list;
LazyAdapter adapter;
List<String> strings = new ArrayList<String>();
String readString = new String();
String arrayNBOW;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
File f = new File(Environment.getExternalStorageDirectory()+"/LazyList/gkindex.txt");
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
while((readString = buf.readLine())!= null){
String arrayNBOW = readString.trim();
Toast.makeText(MainActivity.this, "STARTUPPULL: "+arrayNBOW, Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e){
Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
String testArray = "http://www.oddree.com/rayhaque/android1.jpg,http://www.oddree.com/rayhaque/android2.jpg,http://www.oddree.com/rayhaque/android3.jpg";
// THIS FAILS
// String[] testArraySplit = TextUtils.split(arrayNBOW, ",");
// THIS WORKS
String[] testArraySplit = TextUtils.split(testArray, ",");
adapter=new LazyAdapter(this, testArraySplit);
list.setAdapter(adapter);
Button b=(Button)findViewById(R.id.button1);
b.s开发者_运维百科etOnClickListener(listener);
Button c=(Button)findViewById(R.id.button2);
c.setOnClickListener(loadtext);
}
Thank you in advance for any advice or assistance you can offer me! :-)
SOLUTION CREATED FROM EMANNUEL'S SUGGESTION:
public class MainActivity extends Activity {
ListView list;
LazyAdapter adapter;
List<String> strings = new ArrayList<String>();
String readString = new String();
String arrayNBOW;
String[] nardsArray;
String nards;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// New Solution
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/LazyList/gkindex.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
nards = text.toString();
nardsArray = TextUtils.split(nards, ",");
}
}
catch (IOException e) {
Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
}
// End New Solution
Toast.makeText(MainActivity.this, "FILE-READ: "+nardsArray, Toast.LENGTH_LONG).show();
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, nardsArray);
list.setAdapter(adapter);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(listener);
Button c=(Button)findViewById(R.id.button2);
c.setOnClickListener(loadtext);
}
I suspect your reading didn't work. First thing I noticed is that there's no finally clause for closing streams. Second is you're looping until the end of the file. What if you had an empty line there? Check for empty "" lines. Also you should break on the first line that has contents.
Also, I prefer to use a FileReader like this:
Does this help?
精彩评论