My previous question is solved, but now I have another one.
My If condition not working properly
Scenario:
1. If user has not filled up his Name, then Alert Dialog is displayed
2. If user has filled up his Name, then Alert Dialog should NOT displayed
In my case, I will get Alert Dialog in any case, it does not care if Name is filled or not.
ArtistProfileView.Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_artist_profile_view, container, false);
prefrence = SharedPrefrence.getInstance(getActivity());
userDTO = prefrence.getParentUser(Consts.USER_DTO);
paramsRate.put(Consts.ARTIST_ID, userDTO.getUser_id());
parms.put(Consts.ARTIST_ID, userDTO.getUser_id());
parms.put(Consts.USER_ID, userDTO.getUser_id());
baseActivity.headerNameTV.setText(getResources().getString(R.string.my_profile));
if(artistDetailsDTO.getName().isEmpty()) {
new AlertDialog.Builder(getActivity())
.setView(getLayoutInflater().inflate(R.layout.test5, null))
.setPositiveButton(R.string.okaypopup22,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
my_tag = 1;
dialogPersonalProfile();
}
})
.show();
}
setUiAction(view);
return view;
}
In the same Class, method which calls profile editing
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivEditPersonal:
my_tag = 1;
dialogPersonalProfile();
break;
case R.id.ivEditAbout:
my_tag = 2;
dialogAbout();
break;
case R.id.ivEditQualification:
dialogQualification();
break;
case R.id.ivEditSkils:
my_tag = 3;
dialogSkills();
break;
case R.id.ivEditProduct:
dialogProduct();
break;
case R.id.ivEditGallery:
dialogGallery();
break;
}
}
At first I misunderstood your question, so I have edited it.
I guess you should try adding equals() method in if condition
if(artistDetailsDTO.getName().toString.equals(""))
Related
For a bit of context, I'm doing a learning app, when the user reach the end of the lesson a dialog shows but when I click the "go back" option to return to the previous fragment (the app is designed with a Navigation object and different fragments) I get the error on the title.
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
switch (view.getId()) {
case R.id.btnL:
if (pagina == 1) {
Toast toast = Toast.makeText(getContext(), "Estás al principio", Toast.LENGTH_SHORT);
toast.show();
} else {
llenarDatos(--pagina, datos);
}
break;
case R.id.btnR:
if (pagina < limite)
llenarDatos(++pagina, datos);
else
showDialog(view);
break;
case R.id.btn_no:
bundle.putBoolean("modo", true);
Navigation.findNavController(view).navigate(R.id.fragment_menu, bundle);
break;
case R.id.btn_yes:
break;
}
}
The case R.id.btn_no is the option with the error.
Dialog code:
private void showDialog(View v) {
dialog = new Dialog(getContext());
dialog.setContentView(R.layout.custom_dialog);
Button btnVolver = dialog.findViewById(R.id.btn_no);
Button btnExamen = dialog.findViewById(R.id.btn_yes);
btnVolver.setOnClickListener(this);
dialog.show();
}
I've read in another post that this happens because the dialog dismiss when pressing the button, so it doesn't exist anymore. How can I solve it?
My question is quite similar to this one: fragment.getView() return null after backpressed
The problem is next: I have a special Fragment with two states: A and B. If Fragment is in the state B, Backpress should switch the state from B to A. (The difference between two states is in visibility of some elements on a layout) If Fragment is in state A, Backpress should close this Fragment. I overwrote the onBackPressed() method in my activity in next way:
#Override
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count > 1) {
if (isStateB) {
isStateB = false;
MessagesFragment messFragment = ((MessagesFragment) getSupportFragmentManager().findFragmentByTag("MessagesFragment"));
if (messFragment != null) {
Log.d(TAG, "Message Fragment Refresh");
messFragment.refreshFragWithoutMessChecked();
return;
} else {
Log.d(TAG, "Fail To Message Fragment Resfresh");
}
}
}
}
super.onBackPressed();
}
This code executes refreshFragWithoutMessChecked() but nothing happens. All elements from the layout which I am trying to close aren't null, but code doesn't affect them. Also I have a button in the MessageFragment which executes the similar method, and in case I press it, the code works well. In additional I found out that if I call getView() inside refreshFragWithoutMessChecked(). In case when it called from onBackPressed(), getView() returns NULL. In case when it called from onClick() it returns something isn't NULL.
So that is why I am asking, why Backpress makes my getView() returns NULL, and how can I solve my problem?
Fragment Code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_messages, container, false);
/*
Here is a big amount of elements initizalizations and onClick bindings like this:
check_message = view.findViewById(R.id.check_message);
check_message.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {MainActivity.isStateB = true;}});
*/
return view;
}
public void refreshFragWithoutMessChecked() {
MainActivity.isStateB = false;
MainActivity.posMessChecksEnabled.clear();
refreshMessageFragment();
Log.d(TAG,"Message Fragment Refreshed");
}
// Refresh Message Fragment
public void refreshMessageFragment() {
if(getView() != null){
Log.d(TAG,"A");
} else {
Log.d(TAG,"B");
}
// Hide Message Check Show
// THIS CODE DOESN'T WORK ON BACK PRESS BUT IT'S EXECUTED
if (MainActivity.isStateB == true) {
((MainActivity) getActivity()).getSupportActionBar().hide();
write_message_layout.setVisibility(View.GONE);
reply_forward_bottom.setVisibility(View.VISIBLE);
up_message_selected_panel.setVisibility(View.VISIBLE);
toolbar.setVisibility(View.GONE);
appbar_layout.setVisibility(View.GONE);
} else {
if (fromOpen.equalsIgnoreCase(MainActivity.MESSAGES_FILTER_CONTACTS) == false) {
write_message_layout.setVisibility(View.VISIBLE);
}
reply_forward_bottom.setVisibility(View.GONE);
up_message_selected_panel.setVisibility(View.GONE);
toolbar.setVisibility(View.VISIBLE);
appbar_layout.setVisibility(View.VISIBLE);
}
}
I have created a private member of the whole class called Member curMbr;
The activity (rather the fragment, since this is in a frament class) has a listview
with some contributions from members.
I also have a context menu on that list. When clicking on a contribution, I want a (customized) dialog box to show details about the member. (Member ID is part of the contribution objet. )
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
Log.d("FRGCOTIZ02", "create ctxt menu");
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String[] menuItems = getResources().getStringArray(R.array.ar_menu_ctxt_participant);
// Get selected member
Contribution curCotis = (Contribution) (((ListView)v).getItemAtPosition(info.position));
Participant p = new Participant(helper.getDBItem(DBHelper.TABLE_PARTICIPANT,
DBHelper.COL_ID, curCotis.getParticipant()));
curMbr = new Member(helper.getDBItem(DBHelper.TABLE_MEMBER, DBHelper.COL_ID, p.getMember()));
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
Log.d("FRGCOTIZ01", curMbr.getId_());
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return ( applyContextMenuSelection(item) || super.onContextItemSelected(item) );
}
private boolean applyContextMenuSelection(MenuItem item) {
switch (item.getItemId()) {
case 0: // Summary
final Dialog dlg = new Dialog(this.getContext());
final String sessID;
try {
sessID = KUtil.DATE_FORMAT.format(curSess.getDate());
dlg.setContentView(R.layout.alert_show_charges);
Button btnOK = dlg.findViewById(R.id.btn_alertOK);
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setupAlertDialogCharges(dlg, sessID, curMbr.getId_());
}
});
Button btnCancel = dlg.findViewById(R.id.btn_alertCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
dlg.show();
} catch (Exception e){
Log.d("FRAGMENT Contribution", e.getMessage());
}
break;
case 1: // Collect
break;
case 2: // Cancel
break;
default:
break;
}
return true;
}
In method onCreateContextMenu, I can get the member and display his ID.
But in method applyContextMenuSelection, there is an exception, saying the meber is null!
Funny enough there is another variable that I am using in that method, and it works fine. Difference is, that variable has been set at creation of the fragment.
How do I solve this?
I have been studying the code for a while and the only thing I could figure was that the issue is somehow linked to the use of contextmenu. It seems to me that variables are set back to their original when the menu action is supposed to be executed. Again, I am not too sure about that.
So, the only solution I found so far was to keep that vale in a "higher" context:
When I can still read the value,
getActivity().getIntent().putExtra(HomeActivity.ID_ENTRY, curMbr.getId_());
When I want to use it,
final String mbrID = getActivity().getIntent().getStringExtra(HomeActivity.ID_ENTRY);
Title basically says the issue. I referred to this: https://english.stackexchange.com/questions/11481 to solve the latter issue but then its giving me the former issue.
public class PbfSampleApplication extends Application {
public static final String TAG = "PbfSampleApplication";
#Override
public void onCreate() {
super.onCreate();
startService(new Intent(this.getApplicationContext(),
PbfSampleService.class));
FlicManager.init(this.getApplicationContext(), "2125f7c3-0d0e-42d5-88fe-
fda8765867d6", "94d6448c-22d3-4d2e-951f-f625f60f471a");
FlicManager manager = FlicManager.getManager();
for (FlicButton button : manager.getKnownButtons()) {
button.connect();
listenToButtonWithToast(button);
}
}
public void listenToButtonWithToast(FlicButton button) {
button.addEventListener(new FlicButtonAdapter() {
#Override
public void onButtonUpOrDown(FlicButton button, boolean wasQueued, int timeDiff, boolean isUp, boolean isDown) {
if (isDown) {
View v =LayoutInflater.inflate(R.layout.activity_main);
//Above line has the error
View innerView = v.findViewById(R.id.number);
Toast.makeText(getApplicationContext(), "Button " + button + " was pressed", Toast.LENGTH_SHORT).show();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:017702329065"));
startActivity(callIntent);
}
}
});
}
}
I know I'm supposed to pass in an int but when I check the method in the developer website it appears the argument is correctly formatted, so I'm not sure what else I need to get this to work
Create an instance of LayoutInflater before calling it:
LayoutInflater inflater = LayoutInflater.from(getContext());
View v = inflater.inflate(R.layout.activity_main, null);
View innerView = v.findViewById(R.id.number);
The second parameter is a ViewGroup that may be null. The LayoutInflater does not have an inflate method that takes only one argument
I have 20 XML layouts. What I want to happen is to show random xml layouts when the button is clicked. I tried and read same problem as mine but i didnt worked.
For example in Level1 class when the user clicked the PositiveButton in the AlertDialog, random XML Layout will be opened (Level 20 or Level 15 not Level 2).
This is code in Level1 class(the same pattern applies for the rest of the classes)
public class Luzon1 extends Activity {
private String [] answers;
private Button answerButton;
private TextView scoreTxt, showClue;
private EditText answerText;
int scoreNew=0;
public Button yes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_luzon1);
}
public void init()
{
//correct answer
answers=new String[]{"Tarlac"};
(R.id.AnswerButton);
answerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkAnswer();
}
});
}
public boolean isCorrect(String answer)
{ return(answer.equalsIgnoreCase(answers[currentQuestion])); }
public void checkAnswer()
{ String answer=answerText.getText().toString();
if(isCorrect(answer))
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Congratulations! You will receive P10!");
builder.setMessage("Did you know that Former bla bla bla Did you know that Former bla bla bla Did you know that Former bla bla bla");
builder.setIcon(android.R.drawable.btn_star_big_on);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String userData=scoreTxt.getText().toString();
int userNumber=Integer.parseInt(userData);
Intent intent=new Intent(Luzon1.this, Luzon2.class);
intent.putExtra("parameter name", userNumber);
startActivity(intent);
Luzon1.this.finish(); System.exit(0);
} });
AlertDialog alert = builder.create();
alert.show(); // Show Alert Dialog
Thank you so much in advance. and any code snippet will be a great help.
Setting the layout from inside onClick()
If you want this code to be in the onClick() method in your previous activity (the one shown above), use the following code:
#Override
protected void onClick(DialogInterface dialog, int which) {
Random generator = new Random();
int number = generator.nextInt(NUMBER_OF_LEVELS) + 1;
Class activity;
switch(number) {
case 1:
activity = LevelOne.class;
break;
case 2:
activity = LevelTwo.class;
break;
case 3:
activity = LevelThree.class;
break;
case 4:
activity = LevelFour.class;
break;
case 5:
activity = LevelFive.class;
break;
...
case 20:
activity = LevelTwenty.class;
break;
}
Intent intent = (getBaseContext(), activity);
startActivity(intent);
}
Try this:
Declare a public static int count = 0 before onCreate..
then, In onClick increment the count by 1(count++)..
Use switch statement like,(Dont forget to reset counter to 0 when count becomes 20)
void onClick(){
count++;
switch(count) {
case 1:
setContentView(R.layout.yourLayout1);
break;
case 2:
setContentView(R.layout.yourLayout2);
break;
case 3:
setContentView(R.layout.yourLayout3);
break;
case 4:
setContentView(R.layout.yourLayout4);
................................
................................
case 20:
setContentView(R.layout.yourLayout20);
break;
}
if(count==20){
count = 0;
}
}
And also instead of incrementing count each time user clicks, you can use Math.random() and assign it to count (Remember to check(use if statement) Math.random() is returning a value which is below or equal to 20..)
Hope it will help you..