2
votes

I've created a tablelayout which dynamically adds a table row by a click of a button. Each table row consists of three edittexts. Each edittext displays the contents time, activity and budget which are retrieved from a sqlite table. A new row can also be added. My question is: How can I update the sqlite table whenever a change is made in an edittext that is currently holding data from the sqlite table?

EDIT: For further information: I've programmatically created the three edittexts and the table row. So, this is the way I've been currently trying to do it. I've placed each edittext in its own list array in order to get the data from edittext. I'll show an example code of one of the edittext in the table row that holds the data time (e.g. 12:00):

String[] timesDatabase = new String[allEdsTimeDatabase.size()];

for(int i=0; i < allEdsTimeDatabase.size(); i++){

timesDatabase[i] = allEdsTimeDatabase.get(i).getText().toString();

timeItemsDatabase.add(timesDatabase[i]);

}

for (int i = 0; i < timeItemsDatabase.size(); i++){

updateTime = timeItemsDatabase.get(i).toString();

}

for (int i = 0; i < db.getActivityIDs().size(); i++){

plannerDatabase = new Planner(db.getActivityIDs2().get(i),updateTime,updateName,updateEx2,dateId,tripId2);

           }

db.updateActivity(plannerDatabase);

If I have this: db.updateActivity(plannerDatabase); inside the loop then it overwrites/updates everything in the table to the same contents as the last row. If I have it outside it just updates the last row.

1
Here it's totally depends over the way you have implemented. One way is you provide the button to each row for edit, delete and bind this view tag with the row id which represents the database table row id, now if user presses the edit button, edit the row against the table row id.jitain sharma

1 Answers

2
votes

Use below code for updating text :

EditText editText= (EditText) findViewById(R.id.edittxt);

     editText.setOnFocusChangeListener(new OnFocusChangeListener() {          

            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus)
                   //update sqlite using asynstask
            }
        });

Note : Since you are adding dynamically u can use findViewByTag()

Example : DEMO