how to call setAction in dialog? - android

i want to create add data to SQL lite with pop up dialog, in dialog just have one textedit and buttom save. in Snackbar usually use setAction(). but in dialog I don't know how to call "AddData".
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_category_expanses, container, false);
mTextMessage = (TextView) view.findViewById(R.id.message);
textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);
Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog fbDialogue = new Dialog(getActivity());
fbDialogue.setContentView(R.layout.layout_add_category_expanses);
fbDialogue.setCancelable(true);
fbDialogue.show();
}
});
initObjects();
return view;
}
and i want to call this code AddData to onCreate, this code to save text in sqlLite.
public View.OnClickListener AddData = new View.OnClickListener() {
#Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
};
Please help me solve this problem. thank you.

You must do some like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_category_expanses, container, false);
mTextMessage = (TextView) view.findViewById(R.id.message);
textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);
Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
initObjects();
return view;
}

Related

clicking on a listview item

I have a ListView in my app and each item in the ListView contains a button and item count in it.While clicking on the button in each item, I want to show a dialogue with an EditText to enter new count of the corresponding item and update the item with the value which i get from the dialogue EditText field.
I created dialogue to enter new count on button click, but can't update the value.
My adapter
public class MyAdapter extends BaseAdapter {
#Override
public int getCount() {
return planList.size();
}
#Override
public Object getItem(int position) {
return planList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final SalesModel db_data = planList.get(position);
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.return_confirm, null);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
final TextView stock = (TextView) convertView.findViewById(R.id.stock);
TextView amount = (TextView) convertView.findViewById(R.id.amount);
ImageView minus = (ImageView) convertView.findViewById(R.id.minus);
Double count = Double.parseDouble(db_data.getStock());
Double price = Double.parseDouble(db_data.getSprice());
Double s_price = count*price;
String set_amount = s_price.toString();
name.setText(db_data.getName());
stock.setText(db_data.getStock());
amount.setText(set_amount);
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stock_return = ShowDialogue();
Double n_stock = Double.parseDouble(db_data.getStock())-Double.parseDouble(stock_return);
stock.setText(n_stock.toString());
}
});
return convertView;
}
}
Function to show dialogue
public String ShowDialogue(){
String stk_val;
stock_return = "0.0";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
dialog.setCancelable(false);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.popup_reminder, null);
final EditText stk = (EditText)dialogLayout.findViewById(R.id.stock);
Button ok = (Button)dialogLayout.findViewById(R.id.later );
Button close = (Button)dialogLayout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(stk.getText().toString().trim().isEmpty()){
stk.setError("Enter quantity");
}
else {
stock_return = stk.getText().toString().trim();
}
}
});
dialog.setView(dialogLayout,0,0,0,0);
dialog.show();
return stock_return;
}
you doing something wrong.
Dialog cannot return before you enter ok button.
Try like this :
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stock_return = ShowDialogue(this, position);
}
});
Change dialog to accordance with it like this..
public void ShowDialogue(MyAdapter myAdapter, int position){
String stk_val;
stock_return = "0.0";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
dialog.setCancelable(false);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.popup_reminder, null);
final EditText stk = (EditText)dialogLayout.findViewById(R.id.stock);
Button ok = (Button)dialogLayout.findViewById(R.id.later );
Button close = (Button)dialogLayout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(stk.getText().toString().trim().isEmpty()){
stk.setError("Enter quantity");
}
else {
planList.get(position).setStock(stk.getText().toString().trim());
dialog.dismiss();
myAdapter.notifyDataSetChanged();
}
}
});
dialog.setView(dialogLayout,0,0,0,0);
dialog.show();
}

how to call a fragment method in an activity

I have a dialog method in my fragment, which I want to call from my main activity FAB.
I tried to call it, but got this error;
Attempt to invoke virtual method 'android.view.LayoutInflater android.support.v4.app.FragmentActivity.getLayoutInflater()' on a null object reference
editProfileDialog() is the dialog method in my fragment that I tried to call in my main activity.
public class Edit_fragment extends Fragment {
private EditPresenter mEditPresenter;
public Edit_fragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.edit_profile_fragment, container, false);
ButterKnife.bind(this, view);
mEditPresenter = new EditPresenter(this);
mEditPresenter.onCreate();
return view;
}
public void editProfileDialog(){
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.edit_profile_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
builder.setCancelable(true);
TextView edit_profile = (TextView)dialoglayout.findViewById(R.id.edit_me);
Typeface Sms = Typeface.createFromAsset(getActivity().getAssets(), "fonts/FTLTLT.TTF");
edit_profile.setTypeface(Sms);
TextView new_profile_photo = (TextView)dialoglayout.findViewById(R.id.addAvatar);
new_profile_photo.setTypeface(Sms);
TextView edit_username = (TextView)dialoglayout.findViewById(R.id.editUsernameBtn);
edit_username.setTypeface(Sms);
TextView Change_bio = (TextView)dialoglayout.findViewById(R.id.statusLayout);
Change_bio.setTypeface(Sms);
final AlertDialog dialog = builder.create();
new_profile_photo.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
editPhotoDialog();
}
});
edit_username.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Change_bio.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
private void editPhotoDialog(){
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.edit_photo_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
builder.setCancelable(true);
FrameLayout cameraBtn = (FrameLayout)dialoglayout.findViewById(R.id.cameraBtn);
FrameLayout galleryBtn = (FrameLayout)dialoglayout.findViewById(R.id.galleryBtn);
AppCompatTextView cameraBtnTxt = (AppCompatTextView)dialoglayout.findViewById(R.id.cameraBtnTxt);
Typeface Sms = Typeface.createFromAsset(getActivity().getAssets(), "fonts/FTLTLT.TTF");
cameraBtnTxt.setTypeface(Sms);
AppCompatTextView galleryBtnText = (AppCompatTextView)dialoglayout.findViewById(R.id.galleryBtnText);
galleryBtnText.setTypeface(Sms);
final AlertDialog dialog = builder.create();
cameraBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
setCameraBtn();
}
});
galleryBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
setGalleryBtn();
}
});
dialog.show();
}
private void setGalleryBtn() {
if (PermissionHandler.checkPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
AppHelper.LogCat("Read data permission already granted.");
new PickerBuilder(getActivity(), PickerBuilder.SELECT_FROM_GALLERY)
.setOnImageReceivedListener(imageUri -> {
Intent data = new Intent();
data.setData(imageUri);
AppHelper.LogCat("new image SELECT_FROM_GALLERY" + imageUri);
mEditPresenter.onActivityResult(this, AppConst.SELECT_PROFILE_PICTURE, RESULT_OK, data);
})
.setImageName(getActivity().getString(R.string.app_name))
.setImageFolderName(getActivity().getString(R.string.app_name))
.setCropScreenColor(R.color.colorPrimary)
.withTimeStamp(false)
.setOnPermissionRefusedListener(() -> {
PermissionHandler.requestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
})
.start();
} else {
AppHelper.LogCat("Please request Read data permission.");
PermissionHandler.requestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
}
}
private void setCameraBtn() {
if (PermissionHandler.checkPermission(getActivity(), Manifest.permission.CAMERA)) {
AppHelper.LogCat("camera permission already granted.");
new PickerBuilder(getActivity(), PickerBuilder.SELECT_FROM_CAMERA)
.setOnImageReceivedListener(imageUri -> {
AppHelper.LogCat("new image SELECT_FROM_CAMERA " + imageUri);
Intent data = new Intent();
data.setData(imageUri);
mEditPresenter.onActivityResult(this, AppConst.SELECT_PROFILE_CAMERA, RESULT_OK, data);
})
.setImageName(getActivity().getString(R.string.app_name))
.setImageFolderName(getActivity().getString(R.string.app_name))
.setCropScreenColor(R.color.colorPrimary)
.withTimeStamp(false)
.setOnPermissionRefusedListener(() -> {
PermissionHandler.requestPermission(getActivity(), Manifest.permission.CAMERA);
})
.start();
} else {
AppHelper.LogCat("Please request camera permission.");
PermissionHandler.requestPermission(getActivity(), Manifest.permission.CAMERA);
}
}
}
On my fab.setOnClickListener() that is in my main_activity. I called editProfileDialog() like this;
mEdit_profile_fragment.editProfileDialog();
AND THAT ERROR SHOWED UP IN THE LOGCAT.
LayoutInflater inflater=(LayoutInflater)getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialoglayout = inflater.inflate(R.layout.edit_profile_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialoglayout);
builder.setCancelable(true);
Try this way..
Edit_fragment homeFragment = (Edit_fragment) getSupportFragmentManager().findFragmentByTag(Edit_fragment.class.getSimpleName());
if(fragment != null){
fragment.editProfileDialog();
}
Use this,
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
Change your method to this:
public void editProfileDialog(Context context){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialoglayout = inflater.inflate(R.layout.edit_profile_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialoglayout);
builder.setCancelable(true);
This is because getActivity() is not available in activity class.

After using file.delete() file is still in phone storage with length zero

When I reopen my app file is present and have length zero.
How to delete it permanently?
I am using the list view to display all song in external storage using Media Store and added delete option to it.
After deleting the file when I reopen my app song file is again in list view but with file length zero.
Please see onClickListener for delete:
public class TracksFragment extends Fragment {
songDetailloader loader = new songDetailloader();
ArrayList<Songs> give = new ArrayList<>();
public int pos = -1;
MediaPlayer mp ;
MusicService musicService;
boolean mBound;
Context context;
public TracksFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState)
{
View v =inflater.inflate(R.layout.listviewofsongs,container,false);
final ListView listView = (ListView) v.findViewById(R.id.listView);
loader.set(getContext());
give = loader.allsongs();
final ListViewAdapter listViewAdapter = new ListViewAdapter(getContext(),give);
listView.setAdapter(listViewAdapter);
listView.setSelector(R.drawable.selector);
new Thread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(getActivity(),MusicService.class);
getActivity().bindService(i, serviceConnection, Context.BIND_AUTO_CREATE);
}
}).start();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d("Uri of ",""+give.get(position).getSonguri());
musicService.setplaylist(give,position);
musicService.setMediaPlayer();
view.setSelected(true);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View v =LayoutInflater.from(getContext()).inflate(R.layout.select_dialog_layout,null);
builder.setView(v);
builder.setTitle(give.get(position).gettitle()+"\n "+give.get(position).getalbum());
builder.create();
final AlertDialog d=builder.show();
//seting click listner.....
TextView play = (TextView) v.findViewById(R.id.dialogplay);
TextView playnext = (TextView) v.findViewById(R.id.dialogplaynext);
TextView queue = (TextView) v.findViewById(R.id.dialogqueue);
TextView fav = (TextView) v.findViewById(R.id.dialogaddtofav);
TextView album = (TextView) v.findViewById(R.id.dialogalbum);
TextView artist = (TextView) v.findViewById(R.id.dialogartist);
TextView playlist = (TextView) v.findViewById(R.id.dialogaddtoplaylsit);
TextView share = (TextView) v.findViewById(R.id.dialogshare);
TextView delete = (TextView) v.findViewById(R.id.dialogdelete);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File f= new File(give.get(position).getSonguri().getLastPathSegment());
Log.d("LENGTH IS",""+f.length());
musicService.setplaylist(give,position);
musicService.setMediaPlayer();
d.dismiss();
}
});
playnext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
queue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
fav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
DataBaseClass db = new DataBaseClass(getContext());
int i=db.insetintoliked(give.get(position));
if(i==1)
{
Toast.makeText(getContext(),"Added to Favorites",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getContext(),"Already in Favorites",Toast.LENGTH_SHORT).show();
}
});
album.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
Intent i = new Intent( getActivity() , AlbumDetail.class);
Bundle b= new Bundle();
b.putCharSequence("album",give.get(position).getalbum());
i.putExtras(b);
startActivity(i);
}
});
artist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(getContext());
b.setMessage("Audio '"+give.get(position).gettitle()+"' will be deleted permanently !");
b.setTitle("Delete ?");
b.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
d.dismiss();
}
});
b.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
loader.deleteSong(getContext(),give.get(position).getPosition());
File f= new File(give.get(position).getSonguri().getPath());
boolean b = f.delete();
Log.d("Is file exist",f.exists()+"");
Log.d("File Length",""+f.length());
Log.d("Return value",""+b);
give.remove(position); // give is Arraylist of Songs(datatype);
listViewAdapter.notifyDataSetChanged();
if(b)
{
Toast.makeText(getContext(),"Deleted",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getContext(),"Fail to Delete",Toast.LENGTH_SHORT).show();
}
}
});
b.create().show();
d.dismiss();
}
});
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");`enter code here`
share.putExtra(Intent.EXTRA_STREAM, give.get(position).getSonguri());
startActivity(Intent.createChooser(share, "Share Sound File"));
}
});
return true;
}
});
container.addView(v);
return v;
}
}
LogCat:
01-14 05:19:54.189 18010-18010/com.developmentforfun.mdnafiskhan.mp3player D/Is file exist: false
01-14 05:19:54.190 18010-18010/com.developmentforfun.mdnafiskhan.mp3player D/File Lenth:0
01-14 05:19:54.190 18010-18010/com.developmentforfun.mdnafiskhan.mp3player D/Return value: false

EditText value in dialog not being stored

I have an application with a listview and a button on screen that opens a dialog box. Dialog box has one EditText field where user can input text and click Add. onClickListener for Add button makes a async call and passes text to api. Problem is EditText value is not being saved. I cant even get it to show in the Log. Im sure Im missing something simple. Your help is imprecated. Here is the code.
SignInActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
Log.v("SignInActivity","Activity has began");
final DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,1);
Log.v("SignInActivity","DancerAdapter");
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
getGirlList();
nameInput = (EditText) findViewById(R.id.cr_room_name);
loginDancer = (Button) findViewById(R.id.addDancer);
loginDancer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment newFragment = new AddDancerDialog();
newFragment.show(getFragmentManager(), "newDancer");
}
});
static public class AddDancerDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.signin_dialog);
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
//final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
builder.setMessage("Login New Dancer")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final ProgressBar newProgress = (ProgressBar)dialogview.findViewById(R.id.progressBar);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
String nameSubmit = dancerName.getText().toString();
Log.v("SignInActivity-Dialog",nameSubmit);
newProgress.setVisibility(View.VISIBLE);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("action", "addDancer");
params.put("name",nameSubmit);
Log.v("SignInActivity-Dialog", "Add Dancer Function");
Log.v("SignInActivity-Dialog",dancerName.getText().toString());
client.post("http://peekatu.com/apiweb/girlList.php", params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
Log.v("response", response);
responseString2 = response;
//parseDancerList(response);
Log.v("SignInActivity",response);
}
#Override
public void onFailure(Throwable error, String content) {
Log.v("response", "response failed network error");
//waitncall(true);
}
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
}
Problem is here:
Here you set a view to dialog
builder.setView(R.layout.signin_dialog);
Then, you search a EditText in this view:
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
That dialogView was inflated but it is not the view that was added to
dialog. They are different..
I think you can fix as follows:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
builder.setView(dialogview);
...
}

Android getting the EditText value from an AlertDialog in a Fragment

I am getting a null pointer exception when I click the OK button. and I am trying to get the value form an EditText in an AlertDialog. I am calling the AlertDialog from a fragment.
I believe this line is the problem
View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
I think it is because I am passing null but I am not sure what eles to pass. Please note I am calling the AlertDialog from a fragment not an Activity.
public class fragment_account extends myFragment {
private final String firebase = "https://xxxxxxxx.firebaseio.com";
private String email = "";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account, container, false);
SharedPreferences sp = getActivity().getPreferences(0);
TextView address = (TextView) view.findViewById(R.id.textViewEmailAddress);
email = sp.getString("email", "");
address.setText(email);
ImageView imageChangePass = (ImageView) view.findViewById(R.id.imageChangePass);
imageChangePass.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
showInputDialog(v);
}
}
);
return view;
}
protected void showInputDialog(final View view) {
try
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext());
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText edittextOldPass = (EditText) view.findViewById(R.id.edittextOldPass);
EditText edittextNewPass = (EditText) view.findViewById(R.id.edittextNewPass);
EditText edittextConfirmPass = (EditText) view.findViewById(R.id.edittextConfirmPass);
final String oldPass = edittextOldPass.getText().toString().trim();
final String newPass = edittextNewPass.getText().toString().trim();
final String confirmPass = edittextConfirmPass.getText().toString().trim();
Firebase ref = new Firebase(firebase);
ref.changePassword(email, oldPass, newPass, new Firebase.ResultHandler() {
#Override
public void onSuccess() {
}
#Override
public void onError(FirebaseError firebaseError) {
}
});
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
catch (Exception e)
{
Log.i("myStuff", e.getMessage());
}
}
}
Any help or suggestions would be greatly appreciated. Thanks
I think you should call findViewById on promptView instead of on view variable
I guess you should initialize all those textview's before setting the layout of your dialog instead of inside your onClick method. As you have to access their values, you'll have to declare them final in order to get what the user typed in inside of your onClick function.

Categories

Resources