Adding a way to see what's been logged, back button on FirstActivity still isn't right...
This commit is contained in:
parent
f27355fa2d
commit
bf033932a1
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
package com.coldlightalchemist.interactivitystatesharingexample;
|
||||
|
||||
public class Constants {
|
||||
public static final String kInterActivityStateKey = "Inter Activity State";
|
||||
public static final String kFileName = "MyFile.csv";
|
||||
|
||||
public static final String[] kCSVHeaders = {"First Activity String", "Second Activity Int"};
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.coldlightalchemist.interactivitystatesharingexample;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
@ -8,6 +10,8 @@ import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.activity.OnBackPressedCallback;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
@ -32,7 +36,29 @@ public class FirstActivity extends AppCompatActivity {
|
||||
return insets;
|
||||
});
|
||||
|
||||
InterActivityState state = (InterActivityState) Objects.requireNonNull(getIntent().getExtras()).get(MainActivity.kInterActivityStateKey);
|
||||
Context context = getApplicationContext();
|
||||
InterActivityState state = (InterActivityState) Objects.requireNonNull(getIntent().getExtras()).get(Constants.kInterActivityStateKey);
|
||||
|
||||
this.getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
|
||||
@Override
|
||||
public void handleOnBackPressed() {
|
||||
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
|
||||
DialogInterface.OnClickListener listener = (d, w) -> {
|
||||
switch(w) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
finish();
|
||||
break;
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
alertBuilder.setMessage("Are you sure you want to back out, the data you've entered so far will be lost!")
|
||||
.setPositiveButton("Yes", listener)
|
||||
.setNegativeButton("No", listener)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
|
||||
enterStringField = (EditText) findViewById(R.id.enterStringField);
|
||||
enterStringField.addTextChangedListener(new TextWatcher() {
|
||||
@ -56,7 +82,7 @@ public class FirstActivity extends AppCompatActivity {
|
||||
moveToSecondActivity = (Button) findViewById(R.id.moveToSecondActivity);
|
||||
moveToSecondActivity.setOnClickListener((v) -> {
|
||||
Intent secondActivityIntent = new Intent(v.getContext(), SecondActivity.class);
|
||||
secondActivityIntent.putExtra(MainActivity.kInterActivityStateKey, state);
|
||||
secondActivityIntent.putExtra(Constants.kInterActivityStateKey, state);
|
||||
startActivity(secondActivityIntent);
|
||||
});
|
||||
|
||||
|
@ -9,9 +9,6 @@ import java.io.Serializable;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class InterActivityState implements Serializable {
|
||||
|
||||
private static final String[] kCSVHeaders = {"First Activity String", "Second Activity Int"};
|
||||
|
||||
private File writeFile;
|
||||
private String firstActivityString;
|
||||
private int secondActivityInt;
|
||||
@ -44,7 +41,7 @@ public class InterActivityState implements Serializable {
|
||||
PrintWriter writer = new PrintWriter(new FileWriter(writeFile, true));
|
||||
|
||||
if (!fileExists) {
|
||||
writer.println(Utilities.makeCSVString(kCSVHeaders));
|
||||
writer.println(Utilities.makeCSVString(Constants.kCSVHeaders));
|
||||
}
|
||||
|
||||
writer.println(Utilities.makeCSVString(firstActivityString, Integer.toString(secondActivityInt)));
|
||||
|
@ -1,8 +1,13 @@
|
||||
package com.coldlightalchemist.interactivitystatesharingexample;
|
||||
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
@ -11,17 +16,27 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private InterActivityState interActivityState;
|
||||
|
||||
private Button startDataEntry;
|
||||
|
||||
public static final String kInterActivityStateKey = "Inter Activity State";
|
||||
public static final String kFileName = "MyFile.csv";
|
||||
private TextView nothingInFile;
|
||||
|
||||
private RecyclerView dataInFile;
|
||||
|
||||
private File dataFile;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -34,12 +49,41 @@ public class MainActivity extends AppCompatActivity {
|
||||
return insets;
|
||||
});
|
||||
|
||||
dataFile = new File(getFilesDir(), Constants.kFileName);
|
||||
|
||||
startDataEntry = (Button) findViewById(R.id.startDataEntry);
|
||||
startDataEntry.setOnClickListener((v) -> {
|
||||
interActivityState = new InterActivityState(new File(v.getContext().getFilesDir(), kFileName));
|
||||
interActivityState = new InterActivityState(dataFile);
|
||||
Intent firstActivityIntent = new Intent(v.getContext(), FirstActivity.class);
|
||||
firstActivityIntent.putExtra(kInterActivityStateKey, interActivityState);
|
||||
firstActivityIntent.putExtra(Constants.kInterActivityStateKey, interActivityState);
|
||||
startActivity(firstActivityIntent);
|
||||
});
|
||||
|
||||
nothingInFile = (TextView) findViewById(R.id.nothingInFile);
|
||||
|
||||
dataInFile = (RecyclerView) findViewById(R.id.dataInFile);
|
||||
dataInFile.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
try {
|
||||
List<Map<String, String>> currentFileData = Utilities.readFormattedCSV(dataFile);
|
||||
|
||||
if (currentFileData == null || currentFileData.isEmpty()) {
|
||||
nothingInFile.setVisibility(VISIBLE);
|
||||
dataInFile.setVisibility(INVISIBLE);
|
||||
} else {
|
||||
nothingInFile.setVisibility(INVISIBLE);
|
||||
dataInFile.setVisibility(VISIBLE);
|
||||
|
||||
Log.d("RECYCLER DATA", "Recycler Data Size: " + currentFileData.size());
|
||||
|
||||
StoredDataRecyclerViewAdapter adapter = new StoredDataRecyclerViewAdapter(currentFileData);
|
||||
dataInFile.setAdapter(adapter);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
nothingInFile.setVisibility(VISIBLE);
|
||||
dataInFile.setVisibility(INVISIBLE);
|
||||
Log.d("FILE READ", "Failed to read the data file for some reason", e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -35,7 +35,7 @@ public class SecondActivity extends AppCompatActivity {
|
||||
return insets;
|
||||
});
|
||||
|
||||
InterActivityState state = (InterActivityState) Objects.requireNonNull(getIntent().getExtras()).get(MainActivity.kInterActivityStateKey);
|
||||
InterActivityState state = (InterActivityState) Objects.requireNonNull(getIntent().getExtras()).get(Constants.kInterActivityStateKey);
|
||||
|
||||
editNumber = (EditText) findViewById(R.id.editNumber);
|
||||
editNumber.addTextChangedListener(new TextWatcher() {
|
||||
|
@ -0,0 +1,65 @@
|
||||
package com.coldlightalchemist.interactivitystatesharingexample;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StoredDataRecyclerViewAdapter extends RecyclerView.Adapter<StoredDataRecyclerViewAdapter.StoredDataRecyclerViewHolder> {
|
||||
private final List<Map<String, String>> data;
|
||||
|
||||
public static class StoredDataRecyclerViewHolder extends RecyclerView.ViewHolder {
|
||||
private final TextView rvFirstActivityString;
|
||||
private final TextView rvSecondActivityInt;
|
||||
|
||||
public StoredDataRecyclerViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
rvFirstActivityString = itemView.findViewById(R.id.rvFirstActivityString);
|
||||
rvSecondActivityInt = itemView.findViewById(R.id.rvSecondActivityInt);
|
||||
}
|
||||
|
||||
public TextView getFirstActivityStringTextView() {
|
||||
return rvFirstActivityString;
|
||||
}
|
||||
|
||||
public TextView getSecondActivityIntTextView() {
|
||||
return rvSecondActivityInt;
|
||||
}
|
||||
}
|
||||
|
||||
public StoredDataRecyclerViewAdapter(List<Map<String, String>> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public StoredDataRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.stored_data_recycler_view_item, parent, false);
|
||||
|
||||
return new StoredDataRecyclerViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull StoredDataRecyclerViewHolder holder, int position) {
|
||||
holder.getFirstActivityStringTextView().setText(
|
||||
data.get(position).get(Constants.kCSVHeaders[0])
|
||||
);
|
||||
holder.getSecondActivityIntTextView().setText(
|
||||
data.get(position).get(Constants.kCSVHeaders[1])
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,12 @@
|
||||
package com.coldlightalchemist.interactivitystatesharingexample;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Utilities {
|
||||
@ -9,4 +15,25 @@ public class Utilities {
|
||||
.map((s) -> "\"" + s + "\"")
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
public static List<Map<String, String>> readFormattedCSV(File toRead) throws IOException {
|
||||
if (!toRead.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new FileReader(toRead));
|
||||
|
||||
return reader.lines()
|
||||
.filter((s) -> !s.equals(makeCSVString(Constants.kCSVHeaders)))
|
||||
.map((s) -> {
|
||||
String[] spliteration = s.split("\",\"");
|
||||
|
||||
// TODO: If you were to add columns, this would need to be more dynamic then it is
|
||||
return Map.of(
|
||||
Constants.kCSVHeaders[0], spliteration[0].replace("\"", ""),
|
||||
Constants.kCSVHeaders[1], spliteration[1].replace("\"", "")
|
||||
);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,17 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Start Data Entry" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/nothingInFile"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="THERE IS NOTHING IN THE DATA FILE!" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/dataInFile"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
50
app/src/main/res/layout/stored_data_recycler_view_item.xml
Normal file
50
app/src/main/res/layout/stored_data_recycler_view_item.xml
Normal file
@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView7"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="First Activity String" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/rvFirstActivityString"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView9"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Second Activity Int" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/rvSecondActivityInt"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user