Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中添加弹出/对话框并将信息传递回类_Android_Database_Dialog - Fatal编程技术网

如何在Android中添加弹出/对话框并将信息传递回类

如何在Android中添加弹出/对话框并将信息传递回类,android,database,dialog,Android,Database,Dialog,我正在尝试创建一个弹出对话框,允许我的android应用程序的用户在主页上的列表中添加一个新字段。 我做了一些研究,找到了Dialog/alertDialog选项,但是我还没能让它正常工作 这是我的主要活动课: public class MainActivity extends Activity { private ListView listView; public static ArrayList<String> ArrayofName = new ArrayList<Str

我正在尝试创建一个弹出对话框,允许我的android应用程序的用户在主页上的列表中添加一个新字段。 我做了一些研究,找到了Dialog/alertDialog选项,但是我还没能让它正常工作

这是我的主要活动课:

public class MainActivity extends Activity {
private ListView listView;
public static ArrayList<String> ArrayofName = new ArrayList<String>();
//final Dialog dialog = new Dialog(this); //!!! ERROR HERE !!! //

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DatabaseHandler db = new DatabaseHandler(this);
    Button AddNewStudent = (Button) findViewById(R.id.AddNew);

    /**
     * CRUD Operations
     * */
    // Inserting Contacts
    Log.d("Insert: ", "Inserting ..");
    //db.addStudentProfile(new StudentProfile("Shannon", "White"));
    //db.addStudentProfile(new StudentProfile(1,"Shannon", "White", "WhitehousePS", "30", "21" ));       

    /*AddNewStudent.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        // Using an alertDialog to get the user to enter in a new Student

        dialog.setContentView(R.layout.add_new_student);
        dialog.setTitle("Add a new student");
        final EditText firstName=(EditText)dialog.findViewById(R.id.firstName);
        final EditText surname=(EditText)dialog.findViewById(R.id.surname);
        final EditText school=(EditText)dialog.findViewById(R.id.school);
        final EditText age=(EditText)dialog.findViewById(R.id.age);
        Button save=(Button)dialog.findViewById(R.id.save);
        Button btnCancel=(Button)dialog.findViewById(R.id.cancel);
        dialog.show();
    }
});*/

    // Reading all contacts
    Log.d("Reading: ", "Reading all contacts..");
    List<StudentProfile> contacts = db.getAllStudents();       

    for (StudentProfile studProf : contacts) {
        String log = "Id: "+studProf.getID()+" , First name: " + studProf.getFirstName() + ", Surname: " + studProf.getSurname() + ", School: " + studProf.getSchool() + 
                ", Reading Level: "+ studProf.getReadingLevel() + ", Age: " + studProf.getAge();
            // Writing Contacts to log
    Log.d("Name: ", log);

    }

    db.getAllStudents();

    listView = (ListView) findViewById(R.id.listView1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, ArrayofName);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
           Toast.makeText(getApplicationContext(),
            ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();
}
在我的节目的顶端

这是制作弹出对话框的最佳方式吗? 另外,将信息传递回要存储在数据库中的类的最简单方法是什么


任何帮助都将不胜感激,谢谢!:)

您可以在类的顶部创建一些私有变量,从editText中获取文本,并将其放入对话框的按钮单击处理程序方法(其中有“登录用户”注释)中的这些变量中


有时,以对话为主题的活动也很容易包装起来。

大家好,现在有很多事情可以简化,更改最终对话=新建对话(此);到最终对话框=新建对话框(MainActivity.this);看看是不是works@KevinCrain-我刚试过,恐怕没用,运行时应用程序仍然会停止:(还有其他建议吗?对不起,忘了提到你应该在OnCreate方法中调用它,如果你现在没有:)@KevinCrain-啊!你太棒了!非常感谢,这就成功了!:)
final Dialog dialog = new Dialog(this);