我有个人资料视图,我正在尝试在对话框中编辑个人资料信息和个人资料图片。代码正在研究genymotion,但不适用于真实设备(Discovery 2)。请帮帮我。
这是代码:
public class Profile extends Activity implements View.OnClickListener{
DataCollector dataCollector = new DataCollector();
private ProgressDialog pDialog;
// normal profile page
TextView fullName, tvUniversity, tvDepartment, tvClass;
ImageView profileImage;
String username;
ImageButton buttonEdit;
ArrayList<GridElement> currentGridCourses, completedGridCourses;
GridView gvCurrentCourses, gvCompletedCourses;
LinearLayout llHeader;
Student newStudent;
// edit dialog
private EditText etUsername, etPassword, etName, etSurname;
ImageView ivProfile;
Bitmap image;
Spinner spinnerUniversities, spinnerDepartments, spinnerClass;
String password, name, surname, university, department, classNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.profile);
setVariables();
setTabs();
new LoadProfile().execute("message");
new LoadCourses().execute("message");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
ivProfile.setImageURI(selectedImage);
}
}
private void setVariables(){
// deneme
buttonEdit = (ImageButton) findViewById(R.id.buttonEditProfile);
if (getIntent().getStringExtra("username") != null){
username = getIntent().getStringExtra("username");
if (username.equals(getSharedPreferences("SharedPreff", 0).getString("username", "bulunamadi"))){
buttonEdit.setOnClickListener(this);
}else {
buttonEdit.setVisibility(View.GONE);
}
} else {
username = getSharedPreferences("SharedPreff", 0).getString("username", "bulunamadi");
buttonEdit.setOnClickListener(this);
}
fullName = (TextView) findViewById(R.id.tvProfileName);
tvUniversity = (TextView) findViewById(R.id.tvUniversity);
tvDepartment = (TextView) findViewById(R.id.tvDepartment);
tvClass = (TextView) findViewById(R.id.tvClass);
profileImage = (ImageView) findViewById(R.id.ivProfile);
gvCurrentCourses = (GridView) findViewById(R.id.gvProfileCourses);
gvCompletedCourses = (GridView) findViewById(R.id.gvCompletedCourses);
llHeader = (LinearLayout) findViewById(R.id.llHeader);
currentGridCourses = new ArrayList<GridElement>();
completedGridCourses = new ArrayList<GridElement>();
gvCompletedCourses.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
final Dialog dialog = new Dialog(Profile.this);
dialog.setTitle("Options");
dialog.setContentView(R.layout.course_dialog);
dialog.show();
Button removeButton = (Button) dialog.findViewById(R.id.buttonCourseAdd);
Button setCurrentButton = (Button) dialog.findViewById(R.id.buttonCourseReview);
removeButton.setText("Remove");
setCurrentButton.setText("Set Current");
final GridElement gridCourse = completedGridCourses.get(i);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new RemoveCourse(username, gridCourse.getName()).execute();
completedGridCourses.remove(i);
gvCompletedCourses.setAdapter(new MyGridAdapter(Profile.this, completedGridCourses));
dialog.cancel();
}
});
setCurrentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new SetCourseIsCompleted(username, gridCourse.getName(), "No", "00").execute();
completedGridCourses.remove(i);
gvCompletedCourses.setAdapter(new MyGridAdapter(Profile.this, completedGridCourses));
dialog.cancel();
}
});
}
});
}
public class RemoveCourse extends AsyncTask<String, Integer, String>{
String username, course;
public RemoveCourse(String username, String course){
this.username = username;
this.course = course;
}
@Override
protected String doInBackground(String... strings) {
return dataCollector.removeSelectedCourse(username, course);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(Profile.this, s, Toast.LENGTH_LONG).show();
}
}
public class SetCourseIsCompleted extends AsyncTask<String, Integer, String>{
String username, course, isCompleted, letter_grade;
public SetCourseIsCompleted(String username, String course, String isCompleted, String letter_grade){
this.username = username;
this.course = course;
this.isCompleted = isCompleted;
this.letter_grade = letter_grade;
}
@Override
protected String doInBackground(String... strings) {
return dataCollector.setCompleted(username, course, isCompleted, letter_grade);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(Profile.this, s, Toast.LENGTH_LONG).show();
}
}
private void setTabs(){
TabHost th = (TabHost) findViewById(R.id.tabHostProfile);
th.setup();
TabHost.TabSpec spec = th.newTabSpec("tag1");
spec.setContent(R.id.tabCurrentCourses);
spec.setIndicator("Current Courses");
th.addTab(spec);
TabHost.TabSpec spec2 = th.newTabSpec("tag1");
spec2.setContent(R.id.tabCompletedCourses);
spec2.setIndicator("Completed Courses");
th.addTab(spec2);
TabHost.TabSpec spec3 = th.newTabSpec("tag2");
spec3.setContent(R.id.tabSkills);
spec3.setIndicator("Skills");
th.addTab(spec3);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.buttonEditProfile:
final Dialog dialog = new Dialog(Profile.this);
dialog.setTitle("Edit Profile");
dialog.setContentView(R.layout.register);
dialog.show();
etUsername = (EditText) dialog.findViewById(R.id.username);
etPassword = (EditText) dialog.findViewById(R.id.password);
etName = (EditText) dialog.findViewById(R.id.etName);
etSurname = (EditText) dialog.findViewById(R.id.etSurname);
ivProfile = (ImageView) dialog.findViewById(R.id.ivRegisterProfileImage);
username = getSharedPreferences("SharedPreff", 0).getString("username", "bulunamadi");
spinnerUniversities = (Spinner) dialog.findViewById(R.id.spinnerRegisterUniversities);
spinnerDepartments = (Spinner) dialog.findViewById(R.id.spinnerRegisterDepartments);
spinnerClass = (Spinner) dialog.findViewById(R.id.spinnerRegisterClass);
ArrayAdapter arrayAdapterUniversities = ArrayAdapter.createFromResource(this, R.array.universities, android.R.layout.simple_spinner_item);
arrayAdapterUniversities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerUniversities.setAdapter(arrayAdapterUniversities);
ArrayAdapter arrayAdapterDepartments = ArrayAdapter.createFromResource(this, R.array.departments, android.R.layout.simple_spinner_item);
arrayAdapterDepartments.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDepartments.setAdapter(arrayAdapterDepartments);
ArrayAdapter arrayAdapterClass = ArrayAdapter.createFromResource(this, R.array.classNo, android.R.layout.simple_spinner_item);
arrayAdapterClass.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerClass.setAdapter(arrayAdapterClass);
spinnerUniversities.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String[] universities = getResources().getStringArray(R.array.universities);
university = universities[i];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinnerDepartments.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String[] departments = getResources().getStringArray(R.array.departments);
department = departments[i];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinnerClass.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String[] classNumbers = getResources().getStringArray(R.array.classNo);
classNo = classNumbers[i];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
ivProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
}
});
SetUserInfo(newStudent);
Button buttonSave = (Button) dialog.findViewById(R.id.register);
buttonSave.setText("Kaydet");
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = etName.getText().toString();
surname = etSurname.getText().toString();
password = etPassword.getText().toString();
image = ((BitmapDrawable) ivProfile.getDrawable()).getBitmap();
Student student = new Student(username, "#314159", name, surname, university, department, classNo, image);
SharedPreferences prefs = getSharedPreferences("SharedPreff", MODE_PRIVATE);
prefs.edit().putString("username", username).commit();
prefs.edit().putString("name", name).commit();
prefs.edit().putString("surname", surname).commit();
prefs.edit().putString("university", university).commit();
prefs.edit().putString("department", department).commit();
prefs.edit().putString("classNo", classNo).commit();
new Update(student, password).execute();
new LoadProfile().execute();
dialog.cancel();
}
});
}
}
public class LoadProfile extends AsyncTask<String, Integer, Student> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Profile.this);
pDialog.setMessage( "Profile uploading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Student doInBackground(final String... params) {
return dataCollector.getStudent(username, 4, true);
}
protected void onPostExecute(Student student) {
pDialog.dismiss();
profileImage.setImageBitmap(student.getImage());
fullName.setText(student.getName() + " " + student.getSurname());
tvUniversity.setText(student.getUniversity());
tvDepartment.setText(student.getDepartment());
tvClass.setText(student.getClassNo());
llHeader.setBackgroundColor(Color.parseColor(student.getColor()));
newStudent = student;
}
}
public class LoadCourses extends AsyncTask<String, Integer, ArrayList<GridElement>>{
@Override
protected ArrayList<GridElement> doInBackground(String... strings) {
completedGridCourses = dataCollector.getUserCourses(username, "Yes");
return dataCollector.getUserCourses(username, "No");
}
@Override
protected void onPostExecute(ArrayList<GridElement> courses) {
super.onPostExecute(courses);
gvCurrentCourses.setAdapter(new MyGridAdapter(Profile.this, courses));
gvCompletedCourses.setAdapter(new MyGridAdapter(Profile.this, completedGridCourses));
}
}
private void SetUserInfo (Student student){
etUsername.setText(student.getUsername());
etUsername.setKeyListener(null);
etName.setText(student.getName());
etSurname.setText(student.getSurname());
image = student.getImage();
ivProfile.setImageBitmap(image);
}
class Update extends AsyncTask<String, String, String>{
Student student;
String password;
public Update(Student student, String password) {
this.password = password;
this.student = student;
}
@Override
protected String doInBackground(String... strings) {
dataCollector.UploadImage(student.getUsername(), student.getImage());
return dataCollector.updateProfile(student, password);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(Profile.this, s, Toast.LENGTH_LONG).show();
}
}
}