I am trying to serialize an object onClick and then in a ListFragment, grab all the files in that directory and put them into the array. I have the code to serialize and put the filenames into an ArrayList, but it does not work.
What am I doing wrong? How can I make it so they both point to the same directory?
Here is my code for serialization:
WIFIQRCODE = "WIFI:T:"+PASSTYPE2+";S:"+SSID2+";P:"+PASS2;
OutputStream file = null;
try {
file = new FileOutputStream(SSID2+".ser");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = null;
try {
output = new ObjectOutputStream(buffer);
} catch (IOException e) {
e.printStackTrace();
}
assert output != null;
try {
output.writeObject(WIFIQRCODE);
} catch (IOException e) {
e.printStackTrace();
}
And my code for getting the filenames in the ListFragment:
String[] filenames = getApplicationContext().fileList();
List<String> list = new ArrayList<String>();
for(int i = 0; i<filenames.length; i++){
//Log.d("Filename", filenames[i]);
list.add(filenames[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, filenames);
setListAdapter(adapter);
}
Any help would be appretitiated!
Related
public class PaymentActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
String FILENAME = "paid";
String data = "yes";
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
try {
File myFile = new File(folder, FILENAME);
FileOutputStream fstream = new FileOutputStream(myFile);
fstream.write(data.getBytes());
fstream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
File myFile = new File(folder, FILENAME);
FileInputStream fstream = new FileInputStream(myFile);
StringBuilder sbuffer = new StringBuilder();
int i;
while ((i = fstream.read())!= -1){
sbuffer.append((char)i);
}
String haspaid = sbuffer.toString();
System.out.println("Help!"+haspaid.equals("yes"));
if (haspaid.equals("yes")) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
fstream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have a file write that inputs "yes" and a file read the reads that "yes" on external storage. I have System.out.println printed it out, and the file read/write seems to work. And yet somehow, when I compare the string resulted, it cannot be checked if it is a value.
What am I doing wrong?
use equals to check the equality of string instead of "!=" or "==".
equals checks the value, and "!=" or "==" checks the reference.
in my android app im trying to set 2 text views names from 2 different files but for some reason the first text view is being set as the 2nd files information "ingredient 2 " and the 2nd text view isnt being displayed at all? am i doing something wrong with the way im setting and opening my files?
public class GroceriesActivity extends AppCompatActivity {
public TextView groceryname1, groceryname2, groceryname3, groceryname4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groceries);
groceryname1 = (TextView) findViewById(R.id.grocery1);
groceryname2 = (TextView) findViewById(R.id.grocery2);
groceryname3 = (TextView) findViewById(R.id.grocery3);
groceryname4 = (TextView) findViewById(R.id.grocery4);
String message;
String message2;
FileInputStream fis1 = null;
FileInputStream fis2 = null;
FileInputStream fis3 = null;
FileInputStream fis4 = null;
try {
fis1 = openFileInput("Ingredient1");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(fis1);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
try {
while ((message = br.readLine()) != null) {
sb.append(message);
}
} catch (IOException e1) {
e1.printStackTrace();
}
groceryname1.setText(sb.toString());
try {
fis1.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis2 = openFileInput("Ingredient2");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
InputStreamReader isr2 = new InputStreamReader(fis2);
BufferedReader br2 = new BufferedReader(isr2);
StringBuffer sb2 = new StringBuffer();
try {
while ((message2 = br2.readLine()) != null) {
sb2.append(message2);
}
} catch (IOException e1) {
e1.printStackTrace();
}
groceryname2.setText(sb2.toString());
try {
fis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
so as tldr im trying to set textview1 as ingredient1 and textview2 as ingredient2 but right now textview1 is being set as ingredient2 and textview2 is not being changed
EDIT: after fixing that error im now having the textview1 and textview2 being set as the first ingredient
try changing
sb.append(message2);
to
sb2.append(message2);
I have an activity which contains a listview, the activity (MainActivity) launches another activity (HomeworkAddActivity), this retrieves a string from the user and adds it to the listview (all of the above works).
However in order that the listview 'remembers' its contents whilest the other activity is launched I try to save the list to a file, code:
public void saveHomework() {
try {
#SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(homeworkFileName);
fos = openFileOutput(homeworkFileName,
Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(homeworkItems);
os.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
public void populateHomework() {
try {
#SuppressWarnings("resource")
FileInputStream fis = new FileInputStream(homeworkFileName);
fis = getParent().openFileInput(homeworkFileName);
ObjectInputStream is = new ObjectInputStream(fis);
homeworkItems = (ArrayList<String>) is.readObject();
is.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
I then read it from the file.
saveHomework is called onPause
and populateHomework:
#Override
public void onResume() {
super.onResume();
homeworkItems = new ArrayList<String>();
activity = this;
homeworkListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, homeworkItems);
populateHomework();
homeworkListAdapter.notifyDataSetChanged();
}
However it only ever shows the items added by addHomeworkActivity, not those 'saved' and 'restored' (they don't get saved/restored successfully), why is this?
It has 2 problems
your listview still connect with your old adapter
notifyDataSetChanged might not work in your case, it relate to this answer
notifyDataSetChanged example
you need to reorder your command in onResume()
#Override
public void onResume() {
super.onResume();
homeworkItems = new ArrayList<String>();
activity = this;
populateHomework(); // I move this line up
homeworkListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, homeworkItems);
//homeworkListAdapter.notifyDataSetChanged(); // this line might not work
yourListView.setAdapter(homeworkListAdapter); // because your listview still connect with your old ArrayAdapter
}
I am working on an app where I need to save/read my files from Internal storage.
But it read all my data in the same TextView.
Can someone show me show,how to show the data in 2 textviews, or to show me how put the one data under the other data.
Here is my code for saving data:
private void SaveMode() {
String FILENAME ;
String Strin1= textview1.getText().toString();
String String2= textview2.getText().toString();
EditText filename1 = (EditText) findViewById(R.id.filename);
FILENAME = filename1.getText().toString();
if (FILENAME.contentEquals("")){
FILENAME = "UNTITLED";
}
String1 = textview1.getText().toString();
String2= textview2.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write("Strin1.getBytes());
fos.write(String2.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
And here is my code for read my data:
private void getFilenames() {
String[] filenames = getApplicationContext().fileList();
List<String> list = new ArrayList<String>();
for(int i = 0; i<filenames.length; i++){
//Log.d("Filename", filenames[i]);
list.add(filenames[i]);
}
ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
spinner.setAdapter(filenameAdapter);
}
public void SpinnerClick(View v) {
String selectFile = String.valueOf(spinner.getSelectedItem());
openFile(selectFile);
}
private void openFile(String selectFile) {
showData = (TextView) findViewById(R.id.show_data);
TextView showData1 = (TextView) findViewById(R.id.show_data1);
String value = "";
FileInputStream fis;
try {
fis = openFileInput(selectFile);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
showData.setText(value);
}
EDIT
I tried to edit my read code like this, but with no luck
private void openFile(String selectFile) {
TextView showData = (TextView) findViewById(R.id.show_data);
TextView showData2 = (TextView) findViewById(R.id.show_data2);
String value = "";
String[] strArray = value.split(";");
try {
FileInputStream fis = openFileInput(selectFile);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
showData.setText(value);
showData.setText(strArray[0]);
showData2.setText(strArray[1]);
}
Edit 2
Got it to work with Shobhit Puri codes
First while saving your data you might insert a delimiter in between those two string. Make sure that delimiter is not the one expected in your textViews.
While saving:
String string3 = ";";
try {
fos.write("Strin1.getBytes());
fos.write("String3.getBytes());
fos.write(String2.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
Then when you are trying to read it into value string, then split is using .split function. Eg:
String[] strArray = value.split(";");
strArray[0] will give first textview's sting and strArray[1] will give the second.
Update
private void openFile(String selectFile) {
TextView showData = (TextView) findViewById(R.id.show_data);
TextView showData2 = (TextView) findViewById(R.id.show_data2);
String value = "";
try {
FileInputStream fis = openFileInput(selectFile);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String[] strArray = value.split(";");
showData.setText(strArray[0]);
showData2.setText(strArray[1]);
}
try
{
FileInputStream fis = new FileInputStream(myInternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText(myData);
I'm trying to learn how to make RSS Reader for my android app by following this tutorial.
The feed is generated from a wordpress blog and I want to figure out a way to read by categories. It's currently reading the entire feed items but I'm trying to sort out specific categories from the list.
This is the Activity class to read the feed.
// Connected - Start parsing
new AsyncLoadXMLFeed().execute();
}
}
private void startLisActivity(RSSFeed feed) {
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
// launch List activity
Intent intent = new Intent(SplashActivity.this, GridActivity.class);
intent.putExtras(bundle);
startActivity(intent);
// kill this activity
finish();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(http://mywordpressblog.com/feed/);
if (feed != null && feed.getItemCount() > 0)
WriteFeed(feed);
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
startLisActivity(feed);
}
}
// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {
FileOutputStream fOut = null;
ObjectOutputStream osw = null;
try {
fOut = openFileOutput(fileName, MODE_PRIVATE);
osw = new ObjectOutputStream(fOut);
osw.writeObject(data);
osw.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Method to read the feed from the File
private RSSFeed ReadFeed(String fName) {
FileInputStream fIn = null;
ObjectInputStream isr = null;
RSSFeed _feed = null;
File feedFile = getBaseContext().getFileStreamPath(fileName);
if (!feedFile.exists())
return null;
try {
fIn = openFileInput(fName);
isr = new ObjectInputStream(fIn);
_feed = (RSSFeed) isr.readObject();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return _feed;
}
before adding the items to your list you can check like this:
if(item.getCategory().contains("categorytoshow")){
add it to the list because it contains the category you want
}
then add it to the list