Android: How to save listview in root directory

141 Views Asked by At

I am making a Bluetooth chat app, which displays the chat in ListView named "list". I am new to android and have no idea how to save listview to root directory in .txt format. I tried searching but it went over my head.

Here is the part where I want to implement the save option:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.save:
            //add the function to perform here
            return(true);
    }
    return(super.onOptionsItemSelected(item));
}

Also, following is the way I declare list in one of my .java file:

listView = (ListView) findViewById(R.id.list);

I am using the following permission in the manifest file:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I don't even know how to begin.

2

There are 2 best solutions below

0
On

Do you want to save the data of list view into the internal storage? Because we can not store directly view object into the memory.

1
On
 try {

    File file = new File("D://filename.txt");
    // if file doesnt exists, then create it
    if (!file.exists()) {
        file.createNewFile();
    }
    for(String str: movementArray){    
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(str);  
        bw.close();
    }
}

    catch (IOException e) {
    e.printStackTrace();
    }
    finally {
    writer.close();
    }