从警报对话框检索项到列表视图中的活动



我正在开发一个 android 应用程序,我的要求是,我需要在 alertdialog 中显示光标值,并让用户从列表中选择一个项目,所选值应返回给调用者,Activity.In 我的应用程序,根据学生信息,光标保存他正在学习的课程的值。因此,用户应该能够选择其中一个课程,然后将该值返回到调用 alertdialog 的活动。你能告诉我如何继续这个吗?我看了多个例子,似乎没有一个完全有效。这是我的示例代码

   final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());  
   final Cursor courses=dbConnector.getCourses(student);
   builder.setTitle("Enter Course");
   builder.setCursor(courses, new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialogInterface, int itemSelected) {
       if (courses.moveToPosition(itemSelected)) {
           String text=courses.getString(0);
           Toast.makeText(getApplicationContext(),
                 "You selected: "+text,                                  
                   Toast.LENGTH_SHORT);
           builder.show();
      }
      courses.moveToFirst();
   }
 },"course");
builder.create();
createRow(sview, student, pass,text);

现在我想将文本变量返回到调用活动,但这里是 onClick() 方法的本地变量。我们如何在不必扩展任何 DialogFragment 类的情况下做到这一点。

我建议您在重写的onClick方法中调用该类的方法,并随心所欲地使用它,因为该方法将像这里一样在Activity

@Override
   public void onClick(DialogInterface dialogInterface, int itemSelected) {
    myMethod(itemSelected);
}
private void myMethod(int selectedItem){
  //Do whatever with that selected item
}

最新更新