I wrote this code and I want to use it when the user decide to exit my application.
when I activate it Eclipse telling me that there is an error with the yes button. This line is the problematic:yes.setOnClickListener(new View.OnClickListener() {
and this is the full code:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
final Dialog exitDialog = new Dialog(this);
exitDialog.setTitle("Exit");
exitDialog.setContentView(R.layout.exit_dialog);
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
exitDialog.dismiss();
finish();
}
});
exitDialog.show();
}
There are 2 possible problems
1st because of this keyword it is referencing the current Dialog class so give your ClassName.this
2nd because of final keyword remove the final keyword and give the listener in onCreate(Bundle SavedInstances) method
How did you initialize the button yes? The recommended way is:
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_quit)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
Dialog dialog = builder.create();
dialog.show();
}
You can also refer to Android dialog guide.
You should use
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
exitDialog.dismiss();
finish()
}
});
instead of OnClickListener.
Another (better) option is to use JOptionPane.showConfirmDialog(...);
Related
this is the class from where I want to call other class using button. The bTicketRes button and bAddBusData works fine but the 3rd button i.e bEditSms when clicked, the app cashes.
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId())
{
case R.id.bTicketRes:
TicketReservationView.setDatabase(database);
Intent it = new Intent(MainView.this,TicketReservationView.class);
startActivity(it);
break;
case R.id.bAddBusData:
BusDataManager.setDatabase(database);
Intent ib = new Intent(MainView.this, BusDataManager.class);
startActivity(ib);
break;
case R.id.bEditSms:
CustomSms.setDatabase(database);
Intent is = new Intent(MainView.this,CustomSms.class);
startActivity(is);
break;
and this is the class which i want to call using button in above class
public class CustomSms extends Activity implements OnClickListener {
private static Database database;
private Button bSaveSms,bBack1;
private EditText customSms,timeDelay;
private SQLiteDatabase db;
public String savSms, timeDly;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_sms);
customSms = (EditText)findViewById(R.id.etcustomSms);
timeDelay = (EditText)findViewById(R.id.ettimeDelay);
bSaveSms = (Button)findViewById(R.id.bSaveSms);
bBack1 = (Button)findViewById(R.id.bBack1);
bSaveSms.setOnClickListener(this);
bBack1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bSaveSms:
db = database.getWritableDatabase();
database.deleteOld(db, savSms, timeDly);
savSms = customSms.getText().toString();
timeDly= timeDelay.getText().toString();
database.insert_message_data(savSms, timeDly, db);
showToast("Message Saved Successfully");
//Toast.makeText(v.getContext(), "Data Saved Successfully", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.bBack1:
customSms.setText("");
timeDelay.setText("");
Intent is = new Intent(CustomSms.this, MainView.class);
startActivity(is);
break;
}
}
public static void setDatabase(Database dbase) {
// TODO Auto-generated method stub
database = dbase;
}
public void showToast(final String title)
{
final View view = getLayoutInflater().inflate(R.layout.toast_layout, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
TextView tv = (TextView)view.findViewById(R.id.toastTitle);
final AlertDialog ad = alertDialogBuilder.create();
ad.setTitle("Notification");
ad.setView(view);
tv.setText(title);
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
ad.dismiss();
}
});
ad.show();
}
when I click button in above class to call the below class, suddenly app crashes and the above class is restarted. I want that below class should be called upon pressing button in above class
I need some help in my homework here...
I want to do a listview with itens from editText.. but I dont know how to delete and edit the item, I tryed to use ContextMenu. I will post my code, and if you think that i have to change anything, tell me.. I'm begginer.
private Button bt;
private ListView lv;
private EditText et;
private ArrayList<String> Arraylt = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
bt = (Button)findViewById(R.id.bt);
lv = (ListView)findViewById(R.id.lv);
et = (EditText)findViewById(R.id.et);
lv.setOnCreateContextMenuListener(this);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int posicao, long id) {
Object o = lv.getItemAtPosition(posicao);
et.setText(String.valueOf(o));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addadicionar();
}
});
}
public void addadicionar() {
Arraylt.add(et.getText().toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arraylt));
et.setText("");
}
hey this is my note pad code it will help you .. have a look
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
ArrayAdapter <String> adapter=new ArrayAdapter<String>(menu.this,android.R.layout.simple_list_item_1,data);
Cursor cursor2=mydb1.rawQuery("SELECT * FROM notes;", null);
data.remove(info);
lv.setAdapter(adapter);
cursor2.moveToPosition(info.position);
int id= cursor2.getInt(cursor2.getColumnIndex("id"));
mydb1.delete("notes", "id=?", new String[] {Integer.toString(id)});
list(view); // this will refresh my list by calling my display list method here
return true;
default:
return super.onContextItemSelected(item);
}
try to remove the item from listItems(List), then call adapter.notifyDataSetChanged();
try this code :
// note : you need to create xml file in res/menu folder which conatain deleter and editer item
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.Deletar:
// edit stuff here
return true;
case R.id.editar:
ArrayAdapter <String> adapter=new ArrayAdapter<String>(menu.this,android.R.layout.simple_list_item_1,data);
data.remove(info);
lv.setAdapter(adapter);
list(view); // this will refresh my list by calling my display //list method here
return true;
default:
return super.onContextItemSelected(item);
}
Yes, I win!.. now, I can edit and delete the item from listview...
private Button bt;
private ListView lv;
private EditText et;
private ArrayList<String> Arraylt = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
bt = (Button)findViewById(R.id.bt);
lv = (ListView)findViewById(R.id.lv);
et = (EditText)findViewById(R.id.et);
lv.setOnCreateContextMenuListener(this);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
Object o = lv.getItemAtPosition(posicao);
et.setText(String.valueOf(o));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addadicionar();
}
});
}
public void addadicionar() {
Arraylt.add(et.getText().toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arraylt));
et.setText("");
}
sorry for the bad indent..
I have a treeViewer in which I want to execute some code(doSomething() method) only when mouse up is pressed, not on mouse down. The following piece of code executes it even when mouse down is triggered as selectionlistener is already triggered. What should I do to get the exact behaviour ?
treeViewer.getTree().addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent e) {
doSomething()
}
#Override
public void mouseDown(MouseEvent e) {
// nothing happens
}
#Override
public void mouseDoubleClick(MouseEvent e) {
// nothing happens
}
});
treeViewer.addPostSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(SelectionChangedEvent event) {
doSomething()
}
});
try this,assuming viewer is your TreeViewer,but it does not work when the mouse was actioned on another control.
viewer.getTree().addMouseListener(new MouseListener() {
#Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Mouse UP");
}
});
I"m tring it refresh my listview when my sqllite database is change (when delete or update query).
the querys itself works just fine, but it doesn't update the listview layout, only when i"m exiting the acitvity and renter it the liseview is changing.
I tried the methodes:
notifyDataSetChanged()
requery()
the code of the activiy is:
public class ShowListActivity extends ListActivity {
private ItemsDataSource itemsDataSource;
private String source[] = new String[] {MySQLiteHelper.KEY_NAME, MySQLiteHelper.KEY_QUANTITY, MySQLiteHelper.KEY_CHECKED};
private int dest[] = new int[] {R.id.itemTitle, R.id.itemQuantity, R.id.itemCheck};
public void goBackMethod(View view) {
Intent intent = new Intent(this, MainScreen.class);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_show_list);
} catch (Exception e) {
e.getMessage();
}
ApplicationController applicationController = (ApplicationController)getApplicationContext();
itemsDataSource = applicationController.itemsDataSource;
final Cursor mCursor = itemsDataSource.getAllItems();
startManagingCursor(mCursor);
CustomCursorAdapter adapter = new CustomCursorAdapter(this, mCursor);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectAction(id);
}
});
}
private void selectAction(final long position) {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("בחר פעולה");
builder
.setMessage("בחר בפעולה שברצונך לבצע:");
builder.setPositiveButton("עדכן פריט קניה",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do update
}
});
builder.setNeutralButton("מחק פריט קניה",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
itemsDataSource.deleteItem(position);
Toast toast = Toast.makeText(getBaseContext(), "הפריט הנבחר נמחק בהצלחה", Toast.LENGTH_SHORT);
toast.show();
}
});
builder.setNegativeButton("חזור לרשימת הקניות",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
the code of the customadapter is:
public class CustomCursorAdapter extends CursorAdapter implements Adapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
mInflater=LayoutInflater.from(context);
mContext=context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemTitle= (TextView)view.findViewById(R.id.itemTitle);
itemTitle.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)));
TextView itemQuantity = (TextView)view.findViewById(R.id.itemQuantity);
itemQuantity.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY)));
CheckBox itemCheck = (CheckBox) view.findViewById(R.id.itemCheck);
itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED))==1);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view=mInflater.inflate(R.layout.listview_item_row, parent, false);
TextView itemTitle= (TextView)view.findViewById(R.id.itemTitle);
itemTitle.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)));
TextView itemQuantity = (TextView)view.findViewById(R.id.itemQuantity);
itemQuantity.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY)));
CheckBox itemCheck = (CheckBox) view.findViewById(R.id.itemCheck);
itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED))==1);
return view;
}
}
in your code you have not added your adapter class. I am sure that you will be adding the data in list view from an array list.
So in the time of updating the list view by adding or removing a data to the list view, first either add or remove the data from your array list and the call as follows
listView.invalidateViews();
then call your set Adapter method
To clarify what double submit is: When the user clicks on a submit button twice, the server will process the same POST data twice. To avoid this (apart from disabling the button after a single submit), most web frameworks like Struts provide a token mechanism. I am searching for the equivalent of this in GWT.
If you want to avoid submitting twice, how about:
boolean processing = false;
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (!processing) {
processing = true;
button.setEnabled(false);
// makes an RPC call, does something you only want to do once.
processRequest(new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
// do stuff
processing = false;
button.setEnabled(true);
});
});
}
}
});
That's the gist of it.
This will be helpfull for you -
final Button btn = new Button("Open");
btn.addSelectionListener(new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
btn.setEnabled(false);
openMethod(name, new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
btn.setEnabled(true);
}
public void onSuccess(Void result) {
MessageBox.alert(info, "Opened Window", null);
btn.setEnabled(true);
window.hide();
}
});
}
});