0

I'm Adding Some Elements Dynamically, and i'm succeed in that. Now i want to store them in SQLite or any other database option. Also i want to retrieve them too.

Here's the code of how i'm adding elements dynamically in my fragment.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

final TableLayout tableLayout = (TableLayout)                                     view.findViewById(R.id.tableLayout);
 final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollview);

 FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

           number = number + 1;

           EditText text = new EditText(getActivity());
           TextView id = new TextView(getActivity());

           id.setHint(number + ". ");
           text.setHint("Enter Device Name");

           tableLayout.addView(id);
           tableLayout.addView(text);

     }
    }
});


FloatingActionButton fab_save =     (FloatingActionButton)view.findViewById(R.id.fab_save);
fab_save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        // save added elements



        // save added elements

    }
});
 }

I want to save my data in fab_save.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Pranav Fulkari
  • 110
  • 2
  • 9
  • Noone can give you a tailormade solution on that, because storing data in sqlite is very project case dependant. Look at my answer to get some ideas on how to do what you want – Thomas Richter Jul 20 '16 at 12:19

1 Answers1

1

You can not save your objects directly within a database. You should save the data of the object, like text or states into corresponding sqlite table. When you need the element again, read the data from the database and generate a new view out of that data.

See the official android developer page on how to save/store data into database (in my oppinion it's quite easy to understand that even as a newbie): https://developer.android.com/training/basics/data-storage/databases.html

To generate views out of data look at this related post: Android - Dynamically Add Views into View

Community
  • 1
  • 1
Thomas Richter
  • 833
  • 8
  • 20
  • can you provide some stuff that i can use to store my view's data into db as per my above code. – Pranav Fulkari Jul 20 '16 at 13:09
  • 1
    Spend one hour to read about the sqlite topic (see link above). You can easily modify the android developer example, i needed one hour to get familiar to that topic. It sounds cruel but without understandig it, you won't have much effort in developping apps. I know it's hard to invest time, but it's worth it and you won't run into the next trouble and bugs because you didn't understand the implementation – Thomas Richter Jul 20 '16 at 13:17