spinner value not passing from one activity to another - java

ViewToken.class:
spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);
spinnerGenre1 = (Spinner) findViewById(R.id.spinner);
docname = spinnerGenre1.getSelectedItem().toString();
session = spinnerGenre.getSelectedItem().toString();
next=(Button)findViewById(R.id.button4);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent next=new Intent(ViewToken.this,Tokens.class);
next.putExtra("docname", docname.toString());
next.putExtra("session", session.toString());
startActivity(next);
}
});
Tokens.class
Intent i2 = getIntent();
final String docname = i2.getStringExtra("docname");
final String session = i2.getStringExtra("session");
The spinner value from ViewToken.class is not passed to Tokens.class

Change this in ViewToken.class
next.putExtra("docname", docname);
next.putExtra("session", session);
You can do the following in your Tokens.class
Bundle extras = getIntent().getExtras();
String docname = extras.getString("docname");
String session = extras.getString("session");

The calls to getSelectedItem() should be made in the onClick listener so that it gets the most up-to-date values selected.
So instead the onClick() method will be:
#Override
public void onClick(View v) {
docname = spinnerGenre1.getSelectedItem().toString();
session = spinnerGenre.getSelectedItem().toString();
Intent next=new Intent(ViewToken.this,Tokens.class);
next.putExtra("docname", docname);
next.putExtra("session", session);
startActivity(next);
}
It is likely that the calls to getSelectedItem() where being made before anything was selected and therefore the values being put() inside the intent where incorrect.

get the values from the spinners just before send them.
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
docname = spinnerGenre1.getSelectedItem().toString();
session = spinnerGenre.getSelectedItem().toString();
Intent next=new Intent(ViewToken.this,Tokens.class);
next.putExtra("docname", docname.toString());
next.putExtra("session", session.toString());
startActivity(next);
}
});

int positionOfSelectedDataFromSpinner;
iv.putExtra("position", positionOfSelectedDataFromSpinner);
Create a New Method
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
positionOfSelectedDataFromSpinner= i;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And In Second Activity
int positionToShowToSpinner = iv.getIntExtra("position",0);
spinner.setSelection(positionToShowToSpinner);

Related

Retrieve Spinner Selected Item To Use in Condition Statement

I'm able to save my spinner selected item states. I'm just having trouble sending and retrieving selected items to use in if statement in next activity.
I know I must use sharedpreferences, but I'm having a bit trouble when it comes to spinner items.
Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
LastSelectedItem = getSharedPreferences("PriorSelected", MODE_PRIVATE);
go_back_btn = (Button) findViewById(R.id.go_back_btn);
themeSpinner = (Spinner) findViewById(R.id.themeSpinner);
go_back_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LastSelectedItem = getSharedPreferences("PriorSelected", MODE_PRIVATE);
SharedPreferences.Editor editor = LastSelectedItem.edit();
editor.apply();
Intent intent = new Intent(getApplicationContext(), home.class);
startActivity(intent);
}
});
int LastSelection = LastSelectedItem.getInt("LastSelection", 0);
editor = LastSelectedItem.edit();
ArrayAdapter<CharSequence> themeAdapter = ArrayAdapter.createFromResource(settings.this, R.array.theme_array, android.R.layout.simple_spinner_item);
themeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
themeSpinner.setAdapter(themeAdapter);
themeSpinner.setSelection(LastSelection);
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editor.putInt("LastSelection", position).apply();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
SharedPreferences settings = getSharedPreferences("app_pref", 0);
SharedPreferences.Editor editor = settings.edit();
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//To save
editor.putInt("position",position);
editor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//To retrieve in next activity
SharedPreferences settings = getSharedPreferences("app_pref", 0);
int snowDensity = settings.getInt("position", -1); //0 is the default value
instead of share preference you can store value to int and pass it to next activity.
int position=-1;
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
position=position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
position=-1;
}
});
go_back_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), home.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
Second Activity:
int position= getIntent().getIntExtra("position", -1);
It's fine, I found the answer.
// send data to next activity
go_back_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int Myposition = themeSpinner.getSelectedItemPosition();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("LastSelection", Myposition);
editor.apply();
Intent intent = new Intent(getApplicationContext(), home.class);
startActivity(intent);
Below is code to retrieve on next activity:
// Retrieve and implement conditional statement
private void ToggleTheme() {
final SharedPreferences LastSelectedItem = getApplicationContext().getSharedPreferences("PriorSelected", Context.MODE_PRIVATE);
int LastSelection = LastSelectedItem.getInt("LastSelection", 0);
if (LastSelection == 1) {
homebutton.setBackgroundResource(R.drawable.light_theme_buttons);

I can't open a chat Intent by pressing a button

I can't open a new Intent by a button but only in listview I followed a guide but it solved it listview and I need a normal click where is the problem in my code
The code only works with the listview...
buttonadd.setOnClickListener(new AdapterView.OnClickListener(){
#Override
public void onClick(AdapterView<?> parent, View view, int postition, long id){
String curentgruopname = parent.getItemAtPosition(postition).toString();
Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
groupchatintent.putExtra("groupname", curentgruopname);
startActivity(groupchatintent);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postition, long id) {
String curentgruopname = parent.getItemAtPosition(postition).toString();
Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
groupchatintent.putExtra("groupname", curentgruopname);
startActivity(groupchatintent);
}
});
Maybe it works when you initialize the OnClicklistener in the OnCreate Method.
Here is an Example:
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button = findViewById(R.id.button);
button.setOnClickListener(buttonClick);
}
View.OnClickListener buttonClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(testActivity.this, nextActivity.class);
startActivity(intent);
}
};

Pull INT from Parse Database Android

I'm hoping this will be a fairly simple thing. In my parse database I have an column called seconds. I do not know the proper syntax to pull it as a Int, I can only pull it as a string. Is there a good way to do this? I've looked into conversion methods but none of them address actually taking an int from a position in an adapter. Here is my code. Alternatively if someone can tell me how to convert that string afterwards in a new activity that would be great.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(Welcome.this,
MainActivity.class);
// Pass data "name" followed by the position
i.putExtra("Name", ob.get(position).getString("Name")
.toString());
i.putExtra("Time", ob.get(position).getString("Time")
.toString());
i.putExtra("Seconds", ob.get(position).getInt("Seconds")
);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Second Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
Timer = i.getStringExtra("Timer");
textViewTime.setText(Timer);
Name = i.getStringExtra("Name");
Seconds = i.getIntExtra("Seconds");
final CounterClass timer = new CounterClass(180000,1000);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.start();
}
});
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
You should use "getInt("Seconds")" without "toInt()".

Why my listview is empty?

I have a arraylist in which users can input their text. And it is displayed in the screen as a listview. That works, but when i try to get the values of the arraylist it says that: Invalid index 0, size is 0. So im guessing for some reason the listview isnt populating?
This is how I add values to the list:
public class ZaidejaiActivity extends ActionBarActivity implements View.OnClickListener{
public Button mBtnIstrinti;
public Button mBtnPrideti;
public Button mBtnPradeti;
public EditText mYrasytiVarda;
public ListView mZaidejai;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidejai);
mBtnPrideti = (Button) findViewById(R.id.pridėtiBtn);
mBtnPrideti.setOnClickListener(this);
mYrasytiVarda = (EditText) findViewById(R.id.VardoYrasymoBtn);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the mZaidejai variable to your list in the xml
mZaidejai = (ListView) findViewById(R.id.sarašas);
mZaidejai.setAdapter(adapter);
mZaidejai.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// remove item from List.
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
list.remove(position);
AlertDialog.Builder builder = new AlertDialog.Builder(ZaidejaiActivity.this);
builder.setMessage("Delete?");
builder.setTitle("Confirm Action");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
adapter.notifyDataSetChanged();
}
//checked.clear();
});
builder.setNegativeButton("Cancel", null);
builder.create();
builder.show();
}
});
mBtnPradeti = (Button) findViewById(R.id.žaistiBtn);
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// count items
int i;
for (i = adapter.getCount() - 1; i >= 0; i--) {
String obj = adapter.getItem(i);
// send items to other activity
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", obj);
startActivity(pradetiZaidima);
}
}
});
}
#Override
public void onClick(View v) {
String input = mYrasytiVarda.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}else{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Klaida:");
alertDialog.setMessage("Blogai yrašytas vardas");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}
}
EDIT
In this activity I want to get the values of the list:
public class ZaidimasActivity extends ZaidejaiActivity {
public TextView mZaidejas;
public TextView mKlausimas;
public Button mKitasKlausimas;
public Button mGryzti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
/** //get the player list from ZaidejaiActivity
Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("playerList"); */
//show the first players name
mZaidejas = (TextView)findViewById(R.id.ZaidejoVardas);
mZaidejas.setText(list.get(0));
/** mGryzti = (Button)findViewById(R.id.GryztiMeniuBtn);
mKitasKlausimas = (Button)findViewById(R.id.KitasBtn);
mKitasKlausimas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
*/
Unfortunately your list==empty. So add some values on it.
list.add("ABC");
list.add("XYZ");
and then setAdapter
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
mZaidejai = (ListView) findViewById(R.id.sarašas);
mZaidejai.setAdapter(adapter);
Your not adding any values to it. And your list is empty and add some values to it like list.add("hyd");
list.add("city");
and pass those values to adapter
Rather doing
adapter.add(input);
Do
list.add(input);
adapter.notifyDataSetChanged();
So, as others have said you should change the adapter.add(input); on the onClick method to
list.add(input);
adapter.notifyDataSetChanged();
Also, on an unrelated matter, you should really move the list.remove(position); call to the following onClick method of the positive button, before the adapter.notifyDataSetChanged(); call, so it wont be removed if the user cancels the action :)
at the first time your list adapter is empty (getCount() = 0) and you do -1 in the for
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// count items
int i;
if (adapter.getCount() > 0)
{
for (i = adapter.getCount() - 1; i >= 0; i--) {
String obj = adapter.getItem(i);
// send items to other activity
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", obj);
startActivity(pradetiZaidima);
}
}
}
});
and for this code :
mZaidejas.setText(list.get(0));
but on Create your list is empty !!!

How to call activity from the items of the spinner

I have a page which consist of a spinner and a submit button. What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview. Each item in the spinner should open different .html page in the layout.
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it...
code for main activity.java is:
public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", "animalbites.html");
startActivity(intent);
}
}
});
}
}
code of CustomOnItemSelectedListener.java is:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
and the display.java is:
public class display extends Activity implements OnClickListener {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent=getIntent();
String mUrl=intent.getStringExtra("urlpath");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/"+mUrl);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
i am not exactly getting your problem, but i ll answer according to what i got:
Why are you concerned of adding an onClickListener to the spinner?
Just add onClickListener to the button and on the click of that button do:
String selecteditemName=(String)spinner1.getSelectedItem();
//spinner1 having items : "file1" , "file2"
Intent i=new Intent(this, yournewActivity.class);
i.putExtra("selected_item", selecteditemName);
startActivity(i);
Now in that activity (yournewActivity) containing the webview use this code in onCreate() method:
String item;
Intent i=getIntent();
item = i.getStringExtra("selected_item");
Now you have your spinner selection in the String 'item', you can now do whatever you want:
My point is that : You should use your spinner selection as ID for populating the webview.
If you still want to use any IDs other than the ones present in the spinner then
make an extra java class Idgetter,
class idgetter
{
public static String getID(String name)
{
if(name.equals("file1"))
return "requiredfile1.html";
else if(name.equals("file2"))
return "requiredfile2.html";
}
}
Now, Do exactly as the above code says and after you have the spinner selection in the variable item , use:
String id=idgetter.getID(item); //in younewActivity
You are finally done.
By using Intent's putExtra method.
Intent mIntent = new Intent(this, display.class);
mIntent.putExtra("urlpath", "put value in here");
startActivity(mIntent);
Populate the spinner with number of html pages and Use spinner.getSelectedItem().toString() inside the addListenerOnButton() method. By doing do, you will get the selected html page and pass it using intent to the next layout.
CODE:
`public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
int final websiteA = 1;
int final websiteB = 2;
int final websiteC = 3;
String selectedHtmlPage = "";
.........
.........
.........
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
switch(arg2)
{
case websiteA :
selectedHtmlPage = "websiteA.html";
break;
case websiteB :
selectedHtmlPage = "websiteB.html";
break;
case websiteC :
selectedHtmlPage = "websiteC.html";
break;
}
}
public void addListenerOnButton()
{
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", selectedHtmlPage);
startActivity(intent);
}
}
});
}
}`
Solved it, its simple. Here is what I did:
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
}
- - -
#Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", mLink);
startActivity(intent);
}
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg2==0){
mLink="Beef html/BBQ_Meatballs_Recipes.html";
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

Categories

Resources