Mr.abing_da_student
Mr.abing_da_student

Reputation: 51

android listview to editTxt inside a dialog

I have a custom listview , what im trying to do is when a user select a specific value in lisview it will goto editText inside a dialog in the same activity.. but it wont get the value.. here's my code

public void savedNotes(){

          list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View arg1, final int position,
                          long arg3) {


                            LayoutInflater li = LayoutInflater.from(context);
                            View promt = li.inflate(R.layout.prompt_saved_notes,null);
                            AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
                            alerDialogBuilder.setView(promt);


                           final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);

                            alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog,int id){
                                      String itemString = list.getItemAtPosition(position).toString();
                                      textfield1.setText(itemString);


                                 }
                          }).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
                              public void onClick(DialogInterface dialog,int id){
                                        dialog.cancel();
                                        }
                          });
                          AlertDialog alertDialog = alerDialogBuilder.create();
                          alertDialog.show();

                }
            });
} 

Upvotes: 1

Views: 99

Answers (3)

VikasGoyal
VikasGoyal

Reputation: 3376

You are setting value on EditText after clicking the positive("saved") button of dialog, but when your dialog is already visible EditText is not having any value.

First you need to remove the code from here:-

    alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog,int id){
//Remove these two line code from here                                       
String itemString = list.getItemAtPosition(position).toString();

textfield1.setText(itemString);
                                 }
                          }

Now add these two line just after finding the reference of EditText in alert dialog like :-

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View arg1, final int position,
                      long arg3) {


                        LayoutInflater li = LayoutInflater.from(context);
                        View promt = li.inflate(R.layout.prompt_saved_notes,null);
                        AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
                        alerDialogBuilder.setView(promt);


                       final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
                       String itemString = list.getItemAtPosition(position).toString();
                                  textfield1.setText(itemString);

                        alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog,int id){
                             }
                      }).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
                          public void onClick(DialogInterface dialog,int id){
                                    dialog.cancel();
                                    }
                      });
                      AlertDialog alertDialog = alerDialogBuilder.create();
                      alertDialog.show();

            }
        });
}

Upvotes: 0

Vishal Chauhan
Vishal Chauhan

Reputation: 917

Try this..

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, final int position,
                  long arg3) {


                    LayoutInflater li = LayoutInflater.from(context);
                    View promt = li.inflate(R.layout.prompt_saved_notes,null);
                    AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
                    alerDialogBuilder.setView(promt);


                   final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
                   String itemString = list.getItemAtPosition(position).toString();
                   textfield1.setText(itemString);
                    alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog,int id){


                         }
                  }).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
                      public void onClick(DialogInterface dialog,int id){
                                dialog.cancel();
                                }
                  });
                  AlertDialog alertDialog = alerDialogBuilder.create();
                  alertDialog.show();

        }
    });

Hope this will help you.

Upvotes: 1

Akhil Soman
Akhil Soman

Reputation: 2217

You can't get the value from a custom listview with

String itemString = list.getItemAtPosition(position).toString();

Instead you might have used an array or an arraylist to store the data that you are setting inside the listview. Inside the onItemCLick() type

//For arraylist
String itemString = your_arrayList.get(position);
//For array
String itemString = your_array[position];

By your code, this "itemString" will appear in edittext only when you click/tap the "Saved" button in the dialog.

Upvotes: 0

Related Questions