我有一个学生名单。我想要的是,当您长按选择的列表时,将使用警报对话框显示一个选项列表。选项为显示,编辑和删除。
当用户单击显示时,将弹出一个警报对话框,显示学生的所有详细信息。
从下面的代码中,唯一显示的是姓氏,名字和课程,但是我的图像(imageUri)将不会显示。
请在下面查看我的代码。谢谢!
MainActivity.java
ArrayList<Student> studentArrayList = new ArrayList<>();
CustomAdapter adapter;
private Uri imageUri;
ListView lv;
AlertDialog.Builder options_builder, show_builder, edit_builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.student_listview);
adapter = new CustomAdapter(this, studentArrayList);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
//
options_builder = new AlertDialog.Builder(this);
show_builder = new AlertDialog.Builder(this);
edit_builder = new AlertDialog.Builder(this);
}
//for menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home){
onBackPressed();
return true;
}else if(id == R.id.action_add){
Intent add = new Intent(MainActivity.this, AddStudentActivity.class);
// startActivity(add);
startActivityForResult(add, 0);
}
return super.onOptionsItemSelected(item);
}
//inflate the menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.addmenu, menu);
return super.onCreateOptionsMenu(menu);
}
//handles the onclick listener for the listview
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
Bundle b = data.getExtras();
imageUri = b.getParcelable("image");
String lastname = b.getString("lastname");
String firstname = b.getString("firstname");
String course = b.getString("course");
Student student = new Student(imageUri, lastname, firstname, course);
studentArrayList.add(student);
adapter.notifyDataSetChanged();
}else{
}
}
//handles when the listview is long clicked
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
// final CharSequence[] options = {"Show", "Edit", "Delete"};
//
Student selectedStudents = studentArrayList.get(position);
final Uri image = selectedStudents.getUriImage();
final String lastname = selectedStudents.getStudlname();
final String firstname = selectedStudents.getStudfname();
final String course = selectedStudents.getStudcourse();
final ImageView imageView = new ImageView(this);
imageView.setImageURI(imageUri);
options_builder.setTitle("Choose an option");
String [] options = {"Show", "Edit", "Delete"};
options_builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
show_builder.setTitle(""+lastname+", "+firstname+"\n"+course);
show_builder.setView(imageView);
show_builder.setNeutralButton("Okay", null);
AlertDialog show_dialog = show_builder.show();
show_dialog.show();
break;
case 1:
Intent toedit = new Intent(MainActivity.this, AddStudentActivity.class);
startActivityForResult(toedit,0);
break;
case 2:
studentArrayList.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Item removed!", Toast.LENGTH_SHORT).show();
break;
}
}
});
AlertDialog options_dialog = options_builder.show();
options_dialog.show();
return true;
}