I am working on a note application whereby a list view displays notes after storing them in internal storage which is controlled from my Utilities class. I just implemented a delete option in a context menu. The delete option works fine to remove each selected list view item. However when i refresh the list activity or add a new note, the deleted notes keeps re-appearing. Am thinking the file needs to be deleted also from the internal storage but am facing problems doing that.
Main Activity
public boolean onContextItemSelected(final MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Note mLoadedNote = (Note) mListNotes.getAdapter().getItem(info.position);
mLoadedNote.getTitle();
switch (item.getItemId()) {
case R.id.delete:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
.setTitle("Delete " + mLoadedNote.getTitle())
.setMessage("are you sure?")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String mfileName = getIntent().getStringExtra(Utilities.EXTRAS_NOTE_FILENAME);
//my question if(mLoadedNote != null && Utilities.deleteFile(mfileName)) {
ArrayAdapter<Note> arrayAdapter = (ArrayAdapter<Note>) mListNotes.getAdapter();
arrayAdapter.remove(arrayAdapter.getItem(info.position));
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, mLoadedNote.getTitle() + " is deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "can not delete the note '" + mLoadedNote.getTitle() + "'", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("NO", null); //do nothing on clicking NO button :P
alertDialog.show();
}
return super.onContextItemSelected(item);
}
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?
I have an AlertDialog in android studio as shown below:
final EditText columninput = new EditText(this);
columninput.setInputType(InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose column number");
adb.setView(columninput);
adb.setMessage("Please specify the number of columns you would like to decrypt your message with.");
adb.setCancelable(true);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
boolean bool = true;
try {
col = Integer.parseInt(columninput.getText().toString());
}
catch (Exception e) {
bool = false;
Toast t = Toast.makeText(getApplicationContext(), "Incorrect input", Toast.LENGTH_LONG);
t.show();
}
if (bool) {
dialog.dismiss();
}
}
});
adb.show();
method1(toRead, col);
Note that toRead is another variable dealt with outside of this context; it should not be causing any errors. When I run my application, the AlertDialog is never shown, and method1 runs immediately. However, when I comment out the last line (method1(toRead, col)), the alertdialog appears when the application is run. Why is this, and how can I ensure that the alertDialog is always shown?
This issue appears because, when you open AlertDialog, the activity goes into pause state.
So, when you hide the last line and don't call the method the activity is able to go into Pause state and the AlertDialog appears successfully.
While in the other case, when the last method is called, what happens is:
before your AlertDialog gets created, the method1() gets called.
I hope this helps.
May this helps you:
Example:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete")
.setMessage("Are you sure you want to delete Product?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dbni.delete(ItemID);
arrayStudent = dbni.selectAll();
list_view.setAdapter(new Adapter_viewitem(mactivity, arrayStudent));
Toast.makeText(getApplicationContext(), "Product deleted successfully", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", null).show();
I'm wondering if this is possible.
I have a guessing game that shows an alert when they either get it wrong or right at the end of the game. What I want to know is, can an alert be shown as a toast in another activity?
This is one of the alerts:
pressMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean correct = false;
final AlertDialog.Builder alert = new AlertDialog.Builder(Task3Activity.this);
alert.setTitle("Unlucky");
alert.setCancelable(false);
alert.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?")
My game has a button that returns the user to another activity from where they came to play the game. The code for this is below:
public class GameCentral extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_central_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.commonmenus, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.guessingGame)
{
//Toast.makeText(this, "Guessing Game option is Clicked", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, Task3Activity.class));
}
else if (id ==R.id.placeOne) {
Toast.makeText(this, "Game Placeholder One option is Clicked", Toast.LENGTH_SHORT).show();
}
else if (id ==R.id.placeTwo) {
Toast.makeText(this, "Game Placeholder Two option is Clicked", Toast.LENGTH_SHORT).show();
}
else if (id ==R.id.placeThree) {
Toast.makeText(this, "Game Placeholder Three option is Clicked", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
The toasts that reside in here are just for the menu options from the action bar.
I'm not sure how I would even start to make the alert in one activity show as a toast in another activity once an exit button had been pressed.
Thanks
I am performing delete and delete all operations in my app on button click. when I click on delete button item is deleted from list view as well deleted from the server.When I click on delete all list view is not refreshing or updating at the same time.In delete all case server is updated but listview is not updated. I am using notifyDataSetChanged() method.How I can resolve this problem?
public void alertMessage()
{
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
switch (which){
//for cancel button
case DialogInterface.BUTTON_POSITIVE:
Category_Dashboard_Page activity = (Category_Dashboard_Page) context;
activity.swipeCategorylistView.closeAnimate(position);
break;
//for delete
case DialogInterface.BUTTON_NEGATIVE:
Category_Dashboard_Page activity1 = (Category_Dashboard_Page) context;
activity1.swipeCategorylistView.closeAnimate(position);
activity1.categoryList_items_obj.remove(position);
activity1.categoryAdapter.notifyDataSetChanged();
new DeleteList().execute();
break;
//for delete all
case DialogInterface.BUTTON_NEUTRAL:
Category_Dashboard_Page activity2 = (Category_Dashboard_Page) context;
activity2.swipeCategorylistView.closeAnimate(position);
for(int i=0;i<categoryList_items_obj.size();i++)
{
Category_Dashboard_Page.CategoryList_Item category_list_item = categoryList_items_obj.get(i);
System.out.println(category_list_item.getCategory_id());
if(category_list_item.getCategory_id().equalsIgnoreCase("all"))
{
categoryList_items_obj.remove(i);
}
}
// notifyDataSetChanged();
System.out.println(String.valueOf(getCount()));
System.out.println(String.valueOf(categoryList_items_obj.size()));
activity2.categoryAdapter.notifyDataSetChanged();
new DeleteAllList().execute();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Are you sure?");
builder.setMessage("You want to delete this article from all categories.")
.setPositiveButton("Cancel ", dialogClickListener)
.setNegativeButton("Delete ", dialogClickListener)
.setNeutralButton("Delete all", dialogClickListener).show();
}
You may want to call notifyDataSetChanged() on the corresponding adapter in onPostExecute() of DeleteList, since you are deleting the elements from the list through Aysnc task. Let me know if this solved your problem. If not, please share some more code of your project.
You need to remove the objects from the adapter also. Try the following code -
public void alertMessage()
{
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
switch (which){
//for cancel button
case DialogInterface.BUTTON_POSITIVE:
Category_Dashboard_Page activity = (Category_Dashboard_Page) context;
activity.swipeCategorylistView.closeAnimate(position);
break;
//for delete
case DialogInterface.BUTTON_NEGATIVE:
Category_Dashboard_Page activity1 = (Category_Dashboard_Page) context;
activity1.swipeCategorylistView.closeAnimate(position);
activity1.categoryList_items_obj.remove(position);
activity1.categoryAdapter.remove(position);
activity1.categoryAdapter.notifyDataSetChanged();
new DeleteList().execute();
break;
//for delete all
case DialogInterface.BUTTON_NEUTRAL:
Category_Dashboard_Page activity2 = (Category_Dashboard_Page) context;
activity2.swipeCategorylistView.closeAnimate(position);
for(int i=0;i<categoryList_items_obj.size();i++)
{
Category_Dashboard_Page.CategoryList_Item category_list_item = categoryList_items_obj.get(i);
System.out.println(category_list_item.getCategory_id());
if(category_list_item.getCategory_id().equalsIgnoreCase("all"))
{
categoryList_items_obj.remove(i);
categoryAdapter.remove(i);
}
}
// notifyDataSetChanged();
System.out.println(String.valueOf(getCount()));
System.out.println(String.valueOf(categoryList_items_obj.size()));
activity2.categoryAdapter.notifyDataSetChanged();
new DeleteAllList().execute();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Are you sure?");
builder.setMessage("You want to delete this article from all categories.")
.setPositiveButton("Cancel ", dialogClickListener)
.setNegativeButton("Delete ", dialogClickListener)
.setNeutralButton("Delete all", dialogClickListener).show();
}
I'm currently developing an app that will allow the user to choose an app and launch it at a later time (there is more functionality but this is the main thing I'm having an issue with.)
I'm looking for a way to get a list of the applications (user installed or updateable ex. Gmail, GMaps, etc...) And throw it into an AlertDialog similar to how you add a shortcut to the Homescreen (Long press -> Applications).
This is the thread I'm using that has the code to get the list of applications that I need. However how would I turn this into an AlertDialog?
Here is the code from the thread.
public void getApps()
{
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
}//end getApps()
And here is the code I use for displaying an AlertDialog similar to what I want to use.
//PseudoCode does not compile
public void displayAppList(View v)
{
final CharSequence[] items = {getApps()};
AlertDialog.Builder builder = new AlertDialog.Builder(SchedulerActivity.this);
builder.setTitle("Choose an App To Launch");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
appChoiceString[count] = items[item];
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(SchedulerActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(SchedulerActivity.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Any help as to getting this to display properly would be awesome.
Why not just use the standard intent chooser? (See this). Otherwise, you probably want to explain what is not displaying the way you want, and how you really want it to look in detail.