A night before, I implemented a code snippet in Android studio to access a file and write a string in a file.But some how I am not able to build it sucessfully. I don't know what is wrong with "openFileOutput()" method. I have already tried adding context before openFileOutput() method, but that doesn't work and if I am not wrong it must not.
Please help me. Here is the code part implementing that snippet.
package com.medaino.www.twitterapp;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.content.Intent;
public class tweetlistactivity extends ListActivity {
// private ListView tweetListView;
private String[] stringArray ;
private tweetaapter tweetItemArrayAdapter;
public List<tweet> getDataForListView()
{
List<tweet> tweets = new ArrayList<tweet>();
for ( int i = 0; i < 10; i++ )
{
tweet twt = new tweet();
twt.setTitle("A nice header for Tweet # " +i);
twt.setBody("Some random body text for the tweet # " +i);
tweets.add(twt);
}
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = Context.openFileOutput(FILENAME,MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
return tweets;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweetlistactivity);
stringArray = new String[10];
List<tweet> tweetlist = getDataForListView() ;
tweetItemArrayAdapter = new tweetaapter(this, stringArray, tweetlist);
setListAdapter(tweetItemArrayAdapter);
}
@Override
protected void onListItemClick(ListView list, View v, int position, long id)
{
Intent intent = new Intent(this, tweet_detail.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tweetlistactivity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
openFileOutput(); needs a Context. You are calling it in an ArrayAdapter. It should be: