Question

What I am trying to accomplish here is to make a Cursor that could be used in an android ListView. I'm reading values directly from multiple files and have to feed them to the cursor. I tried to use MatrixCursor but I can't get it to work with arrays. I have posted my attempt at it so far below and I'm open to all new suggestions. Is there a simpler way to do this?

static MatrixCursor getnameList() {
        ArrayList<String> fsitem = getfsiList();
        MatrixCursor cursor;
        cursor = null;
        for (int i = 0; i < fsitem.size(); i++) {
            try {
                File root = new File(Environment.getExternalStorageDirectory()
                        .getName() + "/" + fsitem.get(i));
                if (root.canRead()) {
                    File namefile = new File(root, ".name");
                    FileReader namereader = new FileReader(namefile);
                    BufferedReader in = new BufferedReader(namereader);
                    String name = in.readLine();
                    String id = in.readLine();
                    String info = in.readLine();
                    String[] fsii = new String[3];
                    fsii[0]= name;
                    fsii[1]= id;
                    fsii[2]= info;
                    cursor.addRow(fsii); //crashes here on running.
                }

            } catch (IOException e) {
                Log.e("NameManager.java : ", ("Error!! Not Writable!!"
                        + Environment.getExternalStorageDirectory().getName()
                        + "/" + fsitem.get(i)));
            }
        }

This code compiles but crashes at cursor.addRow(fsii);:

with 02-24 21:16:49.589: E/AndroidRuntime(3895): at com.manager.abcd.r1223.NameManager.getnameList(NameManager.java:81).

I'm thinking this is a problem with MartixCursor not supporting arrays, but I might be wrong. Any ideas?

Was it helpful?

Solution

If this is all the code then it is normal because you try to add a row on a null cursor(you never initialize cursor) and probably get a NullPointerException. Initialize the MatrixCursor before you enter in the for loop:

String[] columnNames = {"col1", "col2", "col3"};
MatrixCursor cursor = new MatrixCursor(columnNames);

Check the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top