Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm not understanding the following code
    text
    copied!<p>I have to understand this code to create my own app(almost based on this function):</p> <pre><code>public static String[][] ReadFilePerLine(Context context, String nom) { int i = 0; try { FileInputStream fIn = context.openFileInput(nom); InputStreamReader ipsr = new InputStreamReader(fIn); BufferedReader b = new BufferedReader(ipsr); i = getLineNumber(context, nom); String[][] s = new String[2][i/2]; i = 0; String ligne; int j = 0; while ((ligne = b.readLine()) != null) { if (i % 2 == 0) s[0][j] = ligne; else { s[1][j] = ligne; j++; } i++; } fIn.close(); ipsr.close(); return s; } catch (Exception e) {} </code></pre> <p>I'm not understanding why the using of a 2D array? and with two rows ?(String[][] s = new String[2][i/2];) here is the data that it will be stored in the file:</p> <pre><code>data = date + " : " + y + "L/100KM"+ " " + value1 + "L "+ value2 + "KM\n"; </code></pre> <p>Necessary functions:</p> <pre><code> public void updatelv(Activity activity) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); String fileName = getResources().getString(R.string.fileName); fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + "."; s = myIO.ReadFilePerLine(getApplicationContext(), fileDir+fileName); ListView L = (ListView) findViewById(R.id.lv); L.setAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.list_item, s[0])); for (int i = 0; i&lt; s[0].length; i++) { Log.d("Saves",s[0][i]); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.histo); context = getApplicationContext(); activity = this; final SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences(context); String fileName = getResources().getString(R.string.fileName); fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + "."; s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName); updatelv(this); ListView L = (ListView) findViewById(R.id.lv); L.setTextFilterEnabled(true); L.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { // When clicked, show a toast with the TextView text String tmp = s[1][position]; if (tmp == null) tmp = "Aucun fichier trouvé!"; Toast.makeText(getApplicationContext(), tmp, Toast.LENGTH_SHORT) .show(); } }); </code></pre> <p>ReadFilePerLine function:</p> <pre><code>public static String[][] ReadFilePerLine(Context context, String nom) { int i = 0; try { FileInputStream fIn = context.openFileInput(nom); InputStreamReader ipsr = new InputStreamReader(fIn); BufferedReader b = new BufferedReader(ipsr); i = getLineNumber(context, nom); String[][] s = new String[2][i/2]; i = 0; String ligne; int j = 0; while ((ligne = b.readLine()) != null) { if (i % 2 == 0) s[0][j] = ligne; else { s[1][j] = ligne; j++; } i++; } fIn.close(); ipsr.close(); return s; } catch (Exception e) { } </code></pre> <p>Thank you for you help.</p>
 

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