从对话框中选择项目

时间:2017-02-02 13:21:42

标签: java android android-alertdialog

我必须创建一个向特定用户发送任务的应用程序,所以我从数据库中选择数据的对话框中选择特定用户,我需要在任务提交时将相应的用户ID插入数据库,但我可以& #39; t将相应的用户ID发送到数据库这里是我做的代码

 getdata();
    String[] Names = new String[100];
     select_user = (EditText)findViewById(R.id.user_select_txt);
    select_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                AlertDialog.Builder builder = new AlertDialog.Builder(Add_task.this);
                builder.setTitle("Select Sender");
                builder.setItems(Names, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        select_user.setText(Names[i]);
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }catch (Exception ex){
                Toast.makeText(Add_task.this,"Oops...!",Toast.LENGTH_LONG).show();
            }
        }
    });


 private void getdata() {
    StringRequest stringRequest = new StringRequest(Config.USER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);
                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(Config.JSON_ARRAY);
                        //Calling method getStudents to get the students from the JSON Array
                        getUsers(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(Add_task.this);
    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getUsers(JSONArray j) {

    for(int i=0;i<j.length();i++){
        try {
            //Getting json object
            JSONObject json = j.getJSONObject(i);
            Users.add(json.getString(Config.TAG_NAME_USER));
            Users.add(json.getString(Config.TAG_ID_USER));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    //Setting adapter to show the items in the dialoguebox
    Names = Users.toArray(new String[Users.size()]);
}

请提前帮助我解决此问题

0 个答案:

没有答案