ArrayList显示为红色,不会编译--- java android

时间:2015-11-22 18:44:00

标签: java android string arraylist

  

mytextview.setText(xyzList [0] [0]);

线路失败(RED) 我害怕,我混淆了一切 我知道这很简单。
我试图在相关的TextView上编写我填充了dan_akdag.pgn文本文件(带有“”拆分)的数组的第一个实例。

任何人都可以帮助我吗? 我还是个新手。

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AssetManager manager;
        String line = null;
        List<String[]> xyzList = new ArrayList<String[]>();
        String[][] xyz;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
            try {
            manager = getAssets();
            is = manager.open("dan_akdag.pgn");
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            while ((line = br.readLine()) != null) {
                xyzList.add(line.split(" "));
            }
            xyz = (String[][]) xyzList.toArray();
            TextView mytextview = (TextView) findViewById(R.id.showarray);
         **   mytextview.setText(xyzList[0][0]);   **

          } catch (IOException e1) {
            Toast.makeText(getBaseContext(), "Problem!", Toast.LENGTH_LONG).show();
        } finally {

            try {
                //if(reader != null)
              //  reader.close();
                if (br != null)
                br.close();
                if (isr != null)
                isr.close();
                if (is != null)
                is.close();
            } catch (IOException ex) {
            // dosomething
                ex.printStackTrace();
            }


         }


     }
 }

2 个答案:

答案 0 :(得分:2)

更改

mytextview.setText(xyzList[0][0]);

mytextview.setText(xyz[0][0]);

因为xyzList不是数组而xyz是。

同时更改

xyz = (String[][]) xyzList.toArray();

xyz = xyzList.toArray(new String[xyzList.size()][]);

Object[]投射到String[][]将无效。它将抛出ClassCastException

答案 1 :(得分:1)

更改

mytextview.setText(xyzList[0][0]);

mytextview.setText(xyz[0][0]);

xyzList 是指您的ArrayList,而不是array