mardi 3 février 2015

Why doesn't the data from my database update in Android Studio?

I think my SQL queries is working but the data of my table is not updating. Why is that?


I have a table in my database named MasterStudents with column names StudentID INTEGER NOT NULL UNIQUE, FirstName VARCHAR, LastName VARCHAR, ContactNumber VARCHAR, EmailAddress VARCHAR


I have two classes, StudentsMasterList.java - makes a Table layout that display data in a database using a Table Row & StudentsFormUpdate.java that retrieves the data of the selected Table Row and displays it in EditText/s.


I have a button in the StudentsFormUpdate that calls the function UpdateButton() onClick.


This is the StudentsMasterList.java class:





public class StudentsMasterList extends Activity
{
SQLiteDatabase db;

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_masterlist);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);


final Cursor c=db.rawQuery("SELECT * FROM MasterStudents ORDER BY LastName", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView, textView1 = null;

RelativeLayout rl = (RelativeLayout)findViewById(R.id.layout);
ScrollView sv = new ScrollView (this);
sv.addView(tableLayout);
rl.addView(sv, new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));

for (Integer j = 0; j < count; j++)
{

tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);

tableRow.setClickable(true);

final String container = textView1.getText().toString();
tableRow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent StudentUpdateScreenIntent = new Intent (view.getContext(), StudentsFormUpdate.class);

StudentUpdateScreenIntent.putExtra("sender", container);

startActivity(StudentUpdateScreenIntent);

overridePendingTransition(R.anim.transition,R.anim.right2left);

}
});

textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.blueactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
db.close();
rl.requestLayout();


}

}



This is the StudentsFormUpdate.java class:





public class StudentsFormUpdate extends Activity
{
String FirstName, LastName, EmailAddress, ContactNumber, StudentID;
SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_updateform);
db = openOrCreateDatabase("ClassManager", MODE_WORLD_READABLE, null);

String reciever = getIntent().getStringExtra("sender");

Cursor c = db.rawQuery("SELECT * FROM MasterStudents WHERE StudentID = '" + reciever + "'", null);

TextView StudentIDViewUpdate2 = (TextView)findViewById(R.id.StudentIDViewPermanentUpdate);
EditText FirstNameUpdate2 = (EditText)findViewById(R.id.FirstNameTextUpdate);
EditText LastNameUpdate2 = (EditText)findViewById(R.id.LastNameTextUpdate);
EditText ContactNumberUpdate2 = (EditText)findViewById(R.id.ContactNumberTextUpdate);
EditText EmailUpdate2 = (EditText)findViewById(R.id.EmailAddressTextUpdate);


c.moveToFirst();
StudentIDViewUpdate2.setText(c.getString(c.getColumnIndex("StudentID")));
FirstNameUpdate2.setText(c.getString(c.getColumnIndex("FirstName")));
LastNameUpdate2.setText(c.getString(c.getColumnIndex("LastName")));
ContactNumberUpdate2.setText(c.getString(c.getColumnIndex("ContactNumber")));
EmailUpdate2.setText(c.getString(c.getColumnIndex("EmailAddress")));

StudentID = StudentIDViewUpdate2.getText().toString();
FirstName = FirstNameUpdate2.getText().toString();
LastName = LastNameUpdate2.getText().toString();
ContactNumber = ContactNumberUpdate2.getText().toString();
EmailAddress = EmailUpdate2.getText().toString();

}

public void UpdateButton(View view)
{
db.execSQL("UPDATE MasterStudents SET FirstName = '" + FirstName + "', LastName = '" + LastName + "', ContactNumber = '" + ContactNumber + "', " +
"EmailAddress = '" + EmailAddress + "' WHERE StudentID = '" + StudentID + "'");

Toast toast = Toast.makeText(getApplicationContext(), "Student Updated", Toast.LENGTH_SHORT);
toast.show();
finish();
}



Why isn't my code working? Any help is highly appreciated!


Aucun commentaire:

Enregistrer un commentaire