Saving a layout for later view - java

I've created an app in which a user can can add or delete rows in a tableLayout. Inside of these rows, a user inputs an Academic class name, the letter grade received in the class, and the number of units the class is worth. Then, it calculates the user's gpa.
I'm not sure how I would go about doing this, but I need to save an exact copy of the layout when the user presses the 'save' button that will save every current specific thing. (Eg. number of editTexts, text in the editTexts, number of rows, etc.)
I've researched using SharedPreferences and using SQLite. I'm not sure which one is better for saving multiple specific layouts. Also, I know nothing of using SQLite because I've never used it before, so if that is the best choice, could you post a link or something to a good tutorial? Thanks!

Avoid database access you can just use simple 2d array to Store all your data
Ex:
//Saving info of 1st Layout
String[][] data= new String[4][4];
data[0][0] =editTextno;
data[0][1] =editText;
data[0][2] =noofrows;
data[0][3] =field4;
For saving info of second,third you can similarly change the index no. like data[1] for layout 1,data[2] for layout 2
later you can set this array inside SharedPreference or can create setter getter to store this array and can retrieve the Array for info inputted by user wherever you want

Related

How to write value to different textfield every time i click a button?

I am having trouble overcoming this and need your assistance
picture
Every time i click a stop button there is a random number generated in the field above. What I want to do is save each number in the field on the left. Names of the textfields are textField_0, textField_1, textField_2 and so on.
izvucen = rnd.nextInt(31) + 1;
randomField.setText(Integer.toString(izvucen));
textField_i.setText(Integer.toString(izvucen)); // critical line, need to replace "i" with something
i++;
You could put all text fields in a list when they are created and access them via indexing
txtFields.get(i).setText(...)
Or put all fields in a map (Map<Integer, TextField>) and access specific ones via get()
mapOfTextFields.get(i).setText()

How can I accept a value to be passed, using a method

I am trying to select an item from the jList in one form (Home), and extract the data from the ArrayList and output the data to separate jTextFields in a different form (Details). Below is the method I'm trying to use to do this (not a lot there I know!).
public void passObjectData()
{
int i = proObjList.getSelectedIndex();
}
I know once the method is complete, I can just call it on the form load method in the next form, but I'm stuck on how to get the method correct.
I don't know what other code, if any, will be needed for your help.
I have hardcoded data into an ArrayList and have output a name to a jList. Now I want to get all of the data of one person that is stored
in the ArrayList (name, address, tel num etc) and put this information
into jTextFields.
As I understand your question this ArrayList is the undelying data structure used to fill the ListModel and you want to get the selected index from the JList to retrieve the correct object stored in that array list. In this case you can:
Have a domain class called Person to hold the person's data (name, address, etc)
Add Person objects to the ListModel.
Provide an appropriate ListCellRenderer to display the person's name.
Use JList#getSelectedValue() to get the selected Person.
Pass this selected Person object to the text field's form and update those accordingly.
Optional: attach a ListSelectionListener to the JList in order to listen for user's selection changes and do the previous step automatically.
See the first 3 points of this approach exemplified here (note: the example is using JComboBox but the same applies to JList as well).
Suggested readings
Creating a Model
Selecting Items in a List
Writing a Custom Renderer
Side note
Not sure if by forms you mean JFrames but just in case: please note that we should avoid using multiple JFrames. See this topic: The Use of Multiple JFrames, Good/Bad Practice?

How to dynamically add and use edit texts in android?

What I'm wanting to do is use the user's input to determine the size of a matrix, then create the appropriate amount of edit texts for them to fill out the elements. I think I know how to add in new edit texts dynamically, but I don't know how to address them, because they have no id since they are being made dynamically.
you can set the index of the EditText as id.
for(int i=0;i<max;i++){
EditText et=new ExitText(this);
//.........
et.setId(i);
}
And call findViewById(0); and so on

Loading specific data onto a jtable on a frame

The screenshot above shows the current design of a frame, and this concept still is difficult for me to grasp: Adding data onto a table.
I've searched about the topic before, but I always saw that the code included the use of Vectors. I however, wish to use no vectors, and for this table, to be able to insert only a specific set of data.
For example, if the selected Activity is Bowling
the method will run through a file called students.dat, which is in this format:
StudentName
Grade
Email
ParentName
ParentPhone
ParentEmail
Sunday
SundayActivity
Monday
MondayActivity
Tuesday
TuesdayActivity
Wednesday
WednesdayActivity
Thursday
ThursdayActivity
The method will return only the StudentNames Grades and Parent Contact& ParentEmail of the students who are involved in Bowling, as seen in the screenshot, into an ArrayList. From the moment the items are entered into the Arraylist, I do not know what to do onwards.
-Also, when loading into the ArrayList, I plan to do something like this:
StudentNames into NamesList, Grades into GradeList, etc to seperate each array
If you don't want to use Vectors then you need to create a custom TableModel because the DefaultTableModel was written to support Vectors or 2Dimensional arrays.
One way to do this is to copy the DefaultTableModel and change all the occurrences of Vector to ArrayList.
Or if you want to get fancy then you can use my version of a List Table Model.

Binding database column to java jComboBox and JList

Ok, say I have a database table with two columns - one "Name", the other "Age", and there are over 40 names and their respective ages in the table. I want these names to be listed out in a jList/jComboBox, and also I want to be able to click on a name in the jList/jComboBox and have its respective age appear in - say - a text box. Do I have to go about this by simply writing a code that selects all the names from the table and populates the jList/jComboBox and then another code that takes the selected name, puts it in an sql statement, finds the matching age and sends it to a text box, OR is there some kind of a VB-esque column-to-comboBox/List-binding that I can utilise to go about this?
For only 40 name-age combinations, I would just query the database once, and store this information in a Map. Then you can just query the map when a name is selected, and update the age textfield. This will go a lot faster then running SQL queries each time the selection has been changed.
You have to set Model for your swing elements and for updating data based on changes at one place to other implement Listeners.
Have a look at this
Binding comboboxes in swing
Create a custom object that stores both the name and age values and add this object to the combobox. Then when you select an item you have access to both values.
For example: How to use Map element as text of a JComboBox

Categories