Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Trouble with array that's imported from a file and converted from String
    primarykey
    data
    text
    <p>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 ...</p> <pre><code>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 </code></pre> <p>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. </p> <p>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?</p> <p>Here is my code. I have noted what works and what fails.</p> <pre><code>public class MainActivity extends Activity { ListView list; LazyAdapter adapter; List&lt;String&gt; strings = new ArrayList&lt;String&gt;(); 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.setOnClickListener(listener); Button c=(Button)findViewById(R.id.button2); c.setOnClickListener(loadtext); } </code></pre> <p>Thank you in advance for any advice or assistance you can offer me! :-)</p> <p><strong>SOLUTION CREATED FROM EMANNUEL'S SUGGESTION:</strong></p> <pre><code>public class MainActivity extends Activity { ListView list; LazyAdapter adapter; List&lt;String&gt; strings = new ArrayList&lt;String&gt;(); String readString = new String(); String arrayNBOW; String[] nardsArray; String nards; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); </code></pre> <p>// New Solution</p> <pre><code> 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(); } </code></pre> <p>// End New Solution</p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload