Android I';我不理解下面的代码

Android I';我不理解下面的代码,android,Android,我必须理解此代码才能创建自己的应用程序(几乎基于此功能): 我不明白为什么要使用2D数组?并有两行?(字符串[][]s=新字符串[2][i/2];) 以下是将存储在文件中的数据: data = date + " : " + y + "L/100KM"+ " " + value1 + "L "+ value2 + "KM\n"; 必要功能: public void updatelv(Activity activity) { SharedPreferences preferenc

我必须理解此代码才能创建自己的应用程序(几乎基于此功能):

我不明白为什么要使用2D数组?并有两行?(字符串[][]s=新字符串[2][i/2];) 以下是将存储在文件中的数据:

data = date + " : " + y + "L/100KM"+ " " + value1 + "L "+ value2 + "KM\n";
必要功能:

 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<String>(this, R.layout.list_item, s[0]));
        for (int i = 0; i< 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<?> 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();
            }
        });

感谢您的帮助。

它似乎是从一个文件中读取的,将其拆分为二维数组(基于行数)


为什么会这样?我不知道你为什么要那样查看它返回到的函数,然后找到

代码显然是从一个由成对行组成的文件中读取的;它将每对的第一行放在
s[0][…]
中,每对的第二行放在
s[1][…]
中。如果您的格式没有这种特殊性——听起来好像没有这种特殊性——那么您就不需要这样做。只需创建一个普通的一维字符串数组。

他们所做的似乎是将文件分解为两个列表(在本例中为字符串数组),一个包含所有偶数行,另一个包含所有奇数行。我将为您注释代码:

public static String[][] ReadFilePerLine(Context context, String nom) {
    int i = 0;
    try {
        //open the specified input file and create a reader
        FileInputStream fIn = context.openFileInput(nom);
        InputStreamReader ipsr = new InputStreamReader(fIn);
        BufferedReader b = new BufferedReader(ipsr);

        //get the total number of lines in the file, and allocate 
        //a buffer large enough to hold them all
        i = getLineNumber(context, nom);
        String[][] s = new String[2][i/2];

        i = 0;      //set the current line to 0
        String ligne;
        int j = 0;  //set the section index to 0

        //now read through the lines in the file, and place every
        //even-numbered line in the first section ('s[0]'), and every 
        //odd-numbered line in the second section ('s[1]')
        while ((ligne = b.readLine()) != null) {
            if (i % 2 == 0)
                //even-numbered line, it goes into the first section
                s[0][j] = ligne;
            else {
                //odd-numbered line, it goes into the second section
                s[1][j] = ligne;
                j++;  //increment the section index
            }
            i++;  //increment the line count
        }

        //done, cleanup and return
        fIn.close();
        ipsr.close();
        return s;
    } 
    catch (Exception e) {
        //should at least log an error here...
    }
}
至于他们为什么选择使用字符串[],我不能说。可能是为了方便起见,因为他们需要一个可以从包含两个列表的函数返回的对象。就我个人而言,我会使用一个包含两个列表实例的映射,但是字符串[][]同样有效,并且可能稍微更有效

从您的示例数据判断,您似乎不需要使用此格式。但如果要使用它,则需要对数据进行结构化,使键位于一行上,其关联值位于下一行上,如:

date
2011-03-19
userName
someGuy

我认为如果您显示文件的内容会有所帮助。请注意,如果行号为奇数-
String[]]s=新字符串[2][I/2],此函数可能会导致ArrayIndexOutOfBounds异常
如果您查看代码,它似乎根本没有引用s[1],则@andronienn可能重复。。。也许他们这样做是为了扔掉一半的木头。。或者,它也在其他地方使用:/例如,在onCreate方法中,它执行s=myIO.ReadFilePerLine(..),并且您已经删除了该方法的其余部分。@user551841:我编辑了第一篇文章并添加了其余的代码。我不知道他为什么关心偶数和奇数\谁知道为什么我没有得到同样的选票explanation@user551841:对不起,我不明白你的意思:\@aroth:因此,要在ListView中使用这种格式显示正确的diplay,我必须在每个变量后面添加“\n”?:\!另外,我不知道他为什么使用字符串[]变量!他只需要使用一个字符串变量!不另外,对于偶数和奇数(文件中的行数!),显示非常困难,没有用,不是吗?是的,在每个键和每个值之后都需要有一个“\n”。你是对的,一个奇数行的文件会破坏这个代码,但是如果文件格式正确,它就不应该包含奇数行。存储的每个变量应该正好有两行与其关联。当然,其中包含文字换行符的值将完全破坏此格式,除非换行符被手动转义或以其他方式清理。@aroth:但我想在一行中看到所有变量:\!在这种情况下,字符串变量的简单存储是100%有用和正确的!不没有奇偶数的检查!是和否。如果原始开发人员出于某种原因需要解析一个输入文件,该文件在一行上有键,在下一行上有值,那么这种方法并不是不合理的(尽管确实存在更好的方法来完成同样的事情)。但是,如果正在解析的文件不遵循此格式,则是的,二维数组和行号检查没有任何用处。
public static String[][] ReadFilePerLine(Context context, String nom) {
    int i = 0;
    try {
        //open the specified input file and create a reader
        FileInputStream fIn = context.openFileInput(nom);
        InputStreamReader ipsr = new InputStreamReader(fIn);
        BufferedReader b = new BufferedReader(ipsr);

        //get the total number of lines in the file, and allocate 
        //a buffer large enough to hold them all
        i = getLineNumber(context, nom);
        String[][] s = new String[2][i/2];

        i = 0;      //set the current line to 0
        String ligne;
        int j = 0;  //set the section index to 0

        //now read through the lines in the file, and place every
        //even-numbered line in the first section ('s[0]'), and every 
        //odd-numbered line in the second section ('s[1]')
        while ((ligne = b.readLine()) != null) {
            if (i % 2 == 0)
                //even-numbered line, it goes into the first section
                s[0][j] = ligne;
            else {
                //odd-numbered line, it goes into the second section
                s[1][j] = ligne;
                j++;  //increment the section index
            }
            i++;  //increment the line count
        }

        //done, cleanup and return
        fIn.close();
        ipsr.close();
        return s;
    } 
    catch (Exception e) {
        //should at least log an error here...
    }
}
date
2011-03-19
userName
someGuy