public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
m = lv.getAdapter().getItem(info.position).toString();
Toast.makeText(getBaseContext(), "You clicked !"+m, Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete this Fixture?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//listAdapter.remove(m);
Intent i = new Intent(Afc.this.getApplicationContext(), WebViewExample.class);
i.putExtra("Key", m);
startActivity(i);
}
//new class activity WebView/////////////
Bundle extras = getIntent().getExtras();
String addOn = extras.getStringExtra("key",m);
Toast.makeText(getBaseContext(), "You clicked !"+addOn, Toast.LENGTH_SHORT).show();
Hi I am passing a string with an intent, its giving me a error in the new activity on the variable m, inside the getStringExtra("key",m);. can anyone help?
Am I doing this the right way?
The key of putExtra is case sensitive. One time you use
key
and in the another
Key
You should use key or Key in both cases.
In any case use getStringExtra("key") instead of getStringExtra("key", m). As you can see in the documentation there isn't a getStringExtra method that takes two parameters. To explain: your variable m can't be resolved because you just declared it in your first class but not in the second.
Yes and getStringExtra("key",m);??? is Wrong USE getStringExtra("key") ;
Related
Ok, so i need to display some of my product's database data on the scanner builder window(as shown in the image) when this product gets scanned. Scanner window results
As you can see in the image, the barcode scanner is displayed, my question is: is it possible to print in this windows some other data except from the product's barcode? if yes how should i move?
this is my windows builder code
public void handleResult(Result result) {
//myResult= barcode text
myResult = result.getText();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(addquantityactivity.this);
updateData();
}
});
builder.setNeutralButton("Visit(if url)", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
startActivity(browserIntent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
}
Do you see where it says builder.setMessage(result.getText()); <--?
This is where you'd set the result. At the moment you're just putting in the result text which is the number. If you want something more, there is where you would put it.
EDIT:
I'm terrible at writing answers. The method you are using is defined here. It takes in a Character Sequence and will display it. Or it can take in an Int and show that.
EDIT2:
As an example of concatenating the barcode with some other information I would do this:
String myMessage = result.getText() + " and some other amazing information about this barcode";
builder.setMessage(myMessage);
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);
}
I'm trying to make a popup box with edit text field on Android Studio and would like to store the data entered by the user in a variable used in that class.
Something like this:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("New player")
.setMessage("Input new player's name")
.setView(input)
.setPositiveButton("Register", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
name = input.getText().toString(); //<---HERE !want to use this variable
}
})
.setNegativeButton("Cancel", null)
.show();
This doesn't work, so how could I extract the value of name from my popup window to use it in the main code?
Do it this way:
final String[] name = new String[1];
final EditText input = new EditText(this);
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("New player")
.setMessage("Input new player's name")
.setView(input)
.setPositiveButton("Register", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
name[0] = input.getText().toString(); <---HERE! want to use this variable
}
})
.setNegativeButton("Cancel", null)
.show();
Access it using name[0]
Clarification for the followup question by Jox in the comment below: To access the variable inside onClick it needs to be final. But, you cannot assign a value to a simple final variable. However, you can assign a value to a Array member. Hence, the array and not a string variable. Btw, Andriod Studio will do it for you this way itself, just follow the suggested fixes for erroring-out code.
You should declare the DialogInterface.OnCLickListener inside of your Activity. By either creating a listener and assingning it or having your activity implement the interface. And then you won't need to declare name as final.
The reason you have to declare name as final is because you're anonmously creating an object to listen to the click, which require a contract of anything external being used by this anonymous class must be declared as final.
I would recommend creating a listener in your Activity and then assign it to the setOnClickListener(x)
Try this, it works for me :
public class Activity extends AppCompatActivity implements DialogInterface.OnClickListener {
private EditText input;
private String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
input = new EditText(this);
}
public void onClickAlert(View v) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("New player")
.setMessage("Input new player's name")
.setView(input)
.setPositiveButton("Register", this)
.setNegativeButton("Cancel", null)
.show();
//variable str still equal to "" here
}
#Override
public void onClick(DialogInterface dialog, int which) {
str = input.getText().toString(); /*<---HERE! want to use this variable*/
//use it here
Log.d("Activity", "User input : " + str);
}
}
Implement the OnClickListener in your Activity and read the value of the text field in the callback fonction.
In my application when user click back , I make an alert dialog that included 2 buttons. first button is Exit that allow user to exit the application.
the second button is 5 star that allow user to rate me in the market.
it works correctly.
but the problem is that when I kill the application and I run it again, this process repeat. and I want if the user rate me before , I don't show the 5 star button to user again.
how can I save my state button in it?
I know that I must share preferences , but how?
int star = 0;
public void onClick(View v) {
int id = v.getId();
if(id == R.id.button1 && stringListCounter <lines.size() - 1) {
stringListCounter++;
} else if (id == R.id.button2 && stringListCounter > 0) {
stringListCounter--;
}
txtQuestion.setText(lines.get(stringListCounter));
}
public void onBackPressed() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(QuizActivity.this);
alertDialog.setTitle("please rate us");
alertDialog.setPositiveButton("5star", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(
Intent.ACTION_EDIT,
Uri.parse("http://cafebazaar.ir/app/my package name/?l=fa"));
startActivity(browserIntent);
star ++;
}
});
alertDialog.setNegativeButton("exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent1 = new Intent(Intent.ACTION_MAIN);
intent1.addCategory(Intent.CATEGORY_HOME);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
});
alertDialog.show();
}
here my star value is 0. when user rate me , the value of star become 1 . I
want save the my star value to 1 that this process don't repeat again.
please help
First Of Save Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("KeyValue", newHighScore);
editor.commit();
Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger("KeyValue");
after get preference easily check condition
you can create one method in shared preferences that will give your app state means when user is clicking on 5 start on that time you have to set the app state 1. and next time before calling this
alertDialog.setPositiveButton("5star", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(
Intent.ACTION_EDIT,
Uri.parse("http://cafebazaar.ir/app/my package name/?l=fa"));
startActivity(browserIntent);
star ++;
}
});
apply if condition and check the state of the app in shared preference means if state is 1 then don't allow to execute this functionality else allow.
I want to pass the integer (id) from listview (when it is clicked) to TheEndActivity.java . But my code cant retrieve the integer properly.
UsernameList.java (it is the class where the integer comes from)
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent i = new Intent( this, TheEndActivity.class );
i.putExtra( "int", (int)id);
Log.e(LOG_TAG, "id value "+id);
startActivity( i );
Intent intent = new Intent(UsernameList.this, QuizAppActivity.class);
startActivity(intent);
}
TheEndActivity.java (Its the class where i need to pass the integer here. it is created inside onCreate method)
Intent i = getIntent();
final int number = i.getIntExtra("int", -1);
and i need to use the integer (id) which i save it into database (the operation is done in TheEndActivity.java)
finishBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
helper.insertMark(currentGame.getRight(), number );
Log.e(LOG_TAG, "id value: " + number );
Intent i = new Intent(TheEndActivity.this, MainMenu.class);
startActivity(i);
}
});
Solved
The problem is i called 2 activities. Thank you guys
problem seems to be in calling 2 startActivities.
Are TheEndActivity and QuizAppActivity both activities?
You have to change TheEndActivity into an IntentService or AsyncTask if its something doing background activity like saving things into database.
If it's not, then launch only TheEndActivity and allow user to navigate to QuizAppActivity from there.
Also try changing
Intent i = new Intent( this, TheEndActivity.class ); to
Intent i = new Intent( UsernameList.this, TheEndActivity.class );
You are calling startActivity with an intent that doesn't have the int as an extra.