mardi 3 février 2015

Send a string of a specific value of a field in a database to another class that shows that remaining fields of the table in Android Studio

I want to send a string value that finds a specific field in a table of a database to show its remaining fields via EditText. My Table name is MasterStudents with fields StudPic, StudentID, FirstName, LastName, ContactNumber, EmailAddress.


I have a tableRow that shows a list of the fields in the table. I want to get the StudentID value as a string so I can call it in another activity and reference the fields of the remaining table so I can update it.


This is my StudentsMasterList.java (Creates a list of the fields of the table showing StudentID and FirstName and starts StudentsUpdateScreen.java onClick)





package test.com.classmanagertest;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class StudentsMasterList extends Activity
{
SQLiteDatabase db;
static String sender;

@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;

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 TextView finalTextView = textView1;
tableRow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent StudentDeleteScreenIntent = new Intent (view.getContext(), StudentsFormUpdate.class);

sender = finalTextView.getText().toString();
startActivity(StudentDeleteScreenIntent);
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 my StudentsUpdateScreen.java(calls the string and concatinates it to my SQL query so I can call the field that has the StudentNumber in the item I clicked on the table row)





package test.com.classmanagertest;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.EditText;

public class StudentsFormUpdate extends Activity
{
String FirstName, LastName, EmailAddress, ContactNumber;
Integer 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);
Cursor c = db.rawQuery("SELECT * FROM MasterStudents WHERE StudentID = '" + StudentsMasterList.sender + "'", null);

EditText StudIDTextUpdate =(EditText)findViewById(R.id.StudentIDTextUpdate);
StudIDTextUpdate.setText(c.getString(c.getColumnIndex("LastName")));

db.close();
}


}



I want to get the value of Textview1 which is StudentID so I can call concatenate it to my SQL query in StudentsUpdateScreen and call the entire row of the selected StudentID. This is the code I have created and I don't know how to correct it. Fixing code and telling me where to add it is highly appreciated. Thank you in advance!


Aucun commentaire:

Enregistrer un commentaire