I have two activities (A and B), activity A asks for a text input (string) and activity B stores that string in a table. What I wish to achieve: every time I add a new string into the table, the previous string moves down one row and the new string takes the old string's place. I also wish to save the table layout in activity B every time I return to activity A. How would I go about achieving this ?
-
1How about a RecyclerView that inserts at the beginning of the adapter? You have dynamic data, so best to use an adapter – OneCricketeer Oct 16 '16 at 10:27
-
Thanks for your reply @cricket_007 . I'm not too sure what you mean by that ? – Erz Oct 16 '16 at 10:36
-
Unfortunately I cannot find the RecyclerView you speak of in Android Studio. Would you happen to have an example code of such a process ? @cricket_007 – Erz Oct 16 '16 at 11:29
-
@cricket_007 I was searching for the view in the layout XML file. Thanks for your help. Is there a simpler way to go about achieving this ? – Erz Oct 16 '16 at 12:28
-
@cricket_007 I have three rows and three strings. When the first string is added, it goes to row 1. When the second string is added, it goes to row 1 and the first string goes to row 2. When the third string is added, it goes to row 1, the second string goes to row 2 and the first string goes to row 3. – Erz Oct 16 '16 at 12:35
-
Ah. You only have three rows... That changes things. I would like to know what you've tried, though. Are you at least able to just add rows without reordering them? http://stackoverflow.com/a/7280804/2308683 – OneCricketeer Oct 16 '16 at 17:41
-
What I have coded so far: Enter text in Activity A, get text and place it in specific row in Activity B. (I have created 3 rows in the design XML file using TableLayout / TableRow) @cricket_007 – Erz Oct 17 '16 at 05:02
-
@cricket_007 I now wish to: (1) reorder the rows in Activity B every time a new text is inputted in Activity A (2) save the state of the UI and row layout every time I go back to Activity A from Activity B (in order to input a new text) – Erz Oct 17 '16 at 05:16
-
You really should [edit] your question to include some layouts and code, then. It's hard to grasp what you are describing. Plus, like I said, going back and forth between activities doesn't play out so well with saving data – OneCricketeer Oct 17 '16 at 06:45
-
I used a shared preferences method, committing data to an editor and that seems to work in terms of saving data @cricket_007 – Erz Oct 17 '16 at 12:05
-
Do you know how to reorder data in a table ? @cricket_007 – Erz Oct 17 '16 at 12:07
-
I might. Still not seeing any code in your question, though. Please provide a [mcve] of the problem – OneCricketeer Oct 17 '16 at 16:04
-
I just posted my code @cricket_007 . Disregard the recognizer part, I am using the pocketsphinx framework to add voice recognition to my app. – Erz Oct 18 '16 at 02:34
-
1Are you only going to add a maximum of 3 rows? – Code-Apprentice Oct 18 '16 at 02:40
-
For this project, yes I only require a maximum of 3 rows. Ideally in the future I would like more rows but that will come later. Will the number of rows make a considerable difference ? @Code-Apprentice – Erz Oct 18 '16 at 03:34
-
@Erz For a small, fixed number of rows TableLayout is probably sufficient. For many rows, you should use ListView or the newer RecyclerView instead. – Code-Apprentice Oct 18 '16 at 03:41
-
@Code-Apprentice Thank you for your advice. I will be fine using TableLayout as I do not have many rows. Would you happen to know how to rearrange those rows and save the state of the UI when switching between activities ? – Erz Oct 18 '16 at 04:14
-
@Erz just change the text in each TextView. Not sure exactly what you need help with here. – Code-Apprentice Oct 18 '16 at 04:21
-
@Code-Apprentice I'm not sure if you read my original question, it is much more complicated than that. So far I have managed to code: inputting a string via voice command in Activity A and displaying it in an EditText field contained in a TableRow in Activity B. What I need help with: I wish to programmatically rearrange the TableRows so that every new string displayed in Activity B is displayed in row 1, pushing the other already displayed strings down one row. Then I wish to save the state of the TableRow arrangement every time I return to Activity A to input a new string by voice. – Erz Oct 18 '16 at 05:18
-
What views are in each row? – Code-Apprentice Oct 18 '16 at 09:36
-
I have an EditText field in each row @Code-Apprentice – Erz Oct 18 '16 at 11:57
1 Answers
Updated - Input activity
public class MainActivity extends AppCompatActivity {
public static String PREFS_NAME = "prefs";
public static String VALUES_KEY = "vals";
EditText editText;
SharedPreferences prefs;
@Override
protected void onStart() {
super.onStart();
prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
private void addValue(Object s) {
prefs.edit()
.putString(VALUES_KEY, String.valueOf(s))
.apply();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_edit_btn);
editText = (EditText) findViewById(R.id.editText);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString();
JSONArray arr = new JSONArray();
// Just for testing
if (s.equals("clear")) {
addValue(arr);
Toast.makeText(MainActivity.this, "prefs cleared", Toast.LENGTH_SHORT).show();
return;
}
String jsonString = prefs.getString(VALUES_KEY, arr.toString());
// Store a JSON array
try {
arr = new JSONArray(jsonString);
arr.put(s);
addValue(arr);
} catch (JSONException e) {
e.printStackTrace();
}
// Go to next activity
startActivity(new Intent(MainActivity.this, TableLayoutActivity.class));
}
});
}
}
Second activity - Has the table
public class TableLayoutActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tablelayout);
SharedPreferences prefs = getSharedPreferences(MainActivity.PREFS_NAME, MODE_PRIVATE);
TableLayout tl = (TableLayout) findViewById(R.id.table);
// Get the array out of preferences
JSONArray arr = new JSONArray();
String jsonString = prefs.getString(MainActivity.VALUES_KEY, arr.toString());
LayoutInflater inflater = LayoutInflater.from(this);
try {
arr = new JSONArray(jsonString);
// Loop backwards over list, adding rows
for (int i = arr.length() - 1; i >= 0; --i) {
View rowView = inflater.inflate(R.layout.table_row, tl, false);
TextView rowText = (TextView) rowView.findViewById(android.R.id.text1);
rowText.setText( i + ": " + arr.getString(i));
tl.addView(rowView,
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Original Answer
every time I add a new string into the table, the previous string moves down one row and the new string takes the old string's place
I'm not seeing a TableLayout or TableRow in your question. There are three constant strings, but the order you want to display those in seems unclear.
But here's an approach... You store a List of Strings in SharedPreferences or database, whatever. You pass that list of strings to the Activity with the TableLayout tl = (TableLayout) findViewById(R.id.tableLayout)
.
From there, you can simply loop backwards for (int i = list.size(); i >=0; i--)
, get String s = list.get(i);
, then generate a TableRow tr = new TableRow()
which you'll add some TextView
that you've setText(s)
on it. (Or inflate
some XML file containing both the TextView
and TableRow
).
When you are done building TableRow tr
, then you can
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
Thereby, doing what I've quoted above.

- 179,855
- 19
- 132
- 245
-
Thank you for your response and advice, I shall try it out and see how I go. The TableLayout and EditText inside each TableRow were created in the XML file. They have the ID: diagnosis1, diagnosis2, and diagnosis3, but I realise now that it is the wrong way to go about it. – Erz Oct 18 '16 at 06:30
-
Would you also care to show me how to properly store a list of strings in SharedPreferences ? The method I used in my code unfortunately does not seem to work. – Erz Oct 18 '16 at 06:31
-
I'd probably use JSON representation. http://stackoverflow.com/a/27872280/2308683 Other solutions are in that post, though – OneCricketeer Oct 18 '16 at 06:33
-
Unfortunately I cannot use JSON representation as my app has to function completely offline and cannot make use of external libraries. @cricket_007 – Erz Oct 18 '16 at 06:40
-
JSON **is** offline. I think you are confusing yourself with REST APIs that use JSON to transfer data. You don't need to use external libraries either. `JsonArray` is a built-in Android class. Gson just makes it really easy to convert `List` objects to `JsonArray`, though, that's all – OneCricketeer Oct 18 '16 at 06:59
-
Ah I see, I apologise, as you can see I am not very experienced in programming. Is there a way to use JsonArray without using Gson ? Or an easier SharedPreferences method for storing a list containing just 3 strings ? – Erz Oct 18 '16 at 07:22
-
Yeah, you make a `new JsonArray()`, then you call the `put()` method with a String value. Or... just use commas separated values `string1,string2,string3`, then you can `split(",")` to get a `String[]`. Either way, I think that post I linked discusses more than just the JSON option – OneCricketeer Oct 18 '16 at 07:24
-
Thank you very much for your help, I will try it out and let you know if I run into any issues. @cricket_007 – Erz Oct 18 '16 at 07:51
-
Sorry to bother you again. I have tried implementing your advice and unfortunately am unable to make it work as I am not very experienced. Do you mind typing up a more complete example of the code you mentioned above ? Thanks. – Erz Oct 19 '16 at 08:40
-