-2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Achal Sharma
  • 152
  • 1
  • 11

2 Answers2

0

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.

Sunny
  • 3,134
  • 1
  • 17
  • 31
0
 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();
    }
Shubham Vala
  • 1,024
  • 7
  • 18
Neha Chauhan
  • 622
  • 4
  • 12
  • movementArray is giving the error of cannot resolve symbol. Also how do I copy the variable "listView" content to "str"? – Achal Sharma Aug 01 '18 at 14:27