my app crashes with this code.. it doesnt even start up.. any ideas guys thanks
my app crashes with this code.. it doesnt even start up.. any ideas guys thanks
package com.about.bysk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;
public abstract class AboutActivity extends Activity implements
OnItemSelectedListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spin);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(null, "a", 5);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
this makes my app crash. please help..
You have to set Listener for your spinner and your class must implement OnItemSelectedListener
public class YourClass extends Activity implements OnItemSelectedListener { ... }
Then you must set Listener for your spinner:
spinner.setOnItemSelectedListener(this);
Or you can use it like anonymous class
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { ... }
#Override
public void onNothingSelected(AdapterView<?> parentView) { ... }
});
Note: If you want to show Toast, you must call show() method.
You forgot to setlistner ... Also you din call show method with toast !!
you have not set the lisnter to spinner
as per you code do as below ...
1-public class AboutActivity extends Activity implement OnItemSelectedListener{
2- spinner.setOnItemSelectedListener(this);
3- Toast.makeText(AboutActivity.this,"RootBox",Toast.LENGTH_LONG).show();
you can't pass null as context to Toast
Toast.makeText(AboutActivity.this, "a", Toast.LENGTH_LONG).show();
Related
I am creating a spinner given in the code as follow. How can i connect it to another activity(say Bangalore.java). I tried the something available on stackoverflow but its not working.
package com.example.searchbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.example.searchbox.run;
import com.example.searchbox.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] cityNames = {"Jaipur","Bangalore","Agra"};
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
cityNames));
/*how can i connect activity of the spinner to another activity*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You can do something like this:
MyOnItemSelectedListener myonitemselectedlistener =new MyOnItemSelectedListener (savedRoomNames);
YOURSPINNER.setOnItemSelectedListener(myonitemselectedlistener);
And then:
private class MyOnItemSelectedListener implements OnItemSelectedListener{
AdapterView<?> arg0;
View arg1;
int arg2;
long arg3;
public MyOnItemSelectedListener(String[] gespeicherteRaeume) {
this.savedRooms=gespeicherteRaeume;
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
this.arg0=arg0;
this.arg1=arg1;
this.arg2=arg2;
this.arg3=arg3;
//here you have to look which item is arg2 and then if == yyour item start your new activity via intent
}
public void onNothingSelected(AdapterView<?> arg0) {
}
I would use an OnItemSelectedListener and then start the activity once the correct item has been caught.
I have a listview in my activity. I want to show the data of it when clicked on it on second activity. But is not able to do so. Help. This is the code. What to do so that my data is fetched from listview and is shown in next activity?
ListActivity.java
package com.example.task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends Activity implements OnItemClickListener{
ListView list1;
LoginDataBaseAdapter loginDataBaseAdapter;
ArrayList<HashMap<String, String>> datalist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
list1=(ListView)findViewById(R.id.list);
datalist=loginDataBaseAdapter.getAllAnimals();
ArrayAdapter<HashMap<String, String>> adapter=new ArrayAdapter<HashMap<String,String>>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,datalist);
list1.setAdapter(adapter);
list1.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
you can simple use this:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String mydata= datalist.get(arg2).toString();
// pass this data to your second activity
Intent n = new Intent(YourActivityName.this ,SecondActivity.class);
n.putExtra("key", mydata);
startActivity(intent);
}
Now retrieve in your SecondActivity on oncreate() method:
Intent n= getIntent();
String data = intent.getStringExtras("key");
String yourdata;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
yourdata= list1.getItemAtPosition(arg2).toString();
// pass this data to your second activity
}
I think you are trying to achieve like this..
String DATA;
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long arg3) {
DATA= YOUR_LIST.getItemAtPosition(pos).toString();
Intent intent = new Intent(getApplicationContext(),SECOND_ACTIVITY.class);
intent.putExtra("DATA",DATA);
startActivity(intent);
}
Hope it will help you..!!
Well apart from passing data by intents, you can also use Global Variables.
You can check out an example here.
You can set your global variable/s to the value that is applicable when list item is selected (using your onItemClick listner) and then access it in other activity.
Eg:
CLASS MyApp
class MyApp extends Application
{ public String whatever;}
CODE: ACTIVITY 1
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
MyApp as = ((MyApp)getApplicationContext());
as.whatever = position + "Clicked";
}
CODE: ACTIVITY 2
class Blah extends Activity
{
#Override
public void onCreate(Bundle b){
...
MyApp as = ((MyApp)getApplicationContext());
Log.d(as.whatever);
...}
}
MainActivity
String data;
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
data = adapter.getItem(position);
Intent intent = new Intnet(MainActivity.this, NextActivity.class);
i.putExtra("data", data);
startActivity(intent);
}
to open your data on NextActivity
Intent intent = getIntent();
String passData= intent.getExtras().getString("data");
I have an activity where it contains an ActionBar (with four tabs) a fragment respectively assigned to each of these tabs. In these fragments I've assigned some ListAdapters filled with string values, clickable that furthermore I want to operate. When clicking on an item I want that app to send from that fragment to another. I know that I have to use FragmentManager() and FragmentTransaction() but since I'm new to Android dev I demand of any kind of help, help that is appreciated.
Here's the snippet code of one of the tabs(UserFragment.java):
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/** This is a listfragment class */
public class UserFragment extends ListFragment
{
/** An array of items to display in ArrayList */
String user_items[] = new String[]
{
"Account",
"Addresses",
"Payment Providers",
"Profile",
"Transactions",
"Wallet"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, user_items);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart()
{
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
The Profile.java activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Profile extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
//Button test = (Button) findViewById(R.id.btnTest);
}
}
You need to define OnItemClickListener for your ListFragment to handle item click events. For example:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// start your new activity here
}
});
I found what was wrong. After a long search I learnt that if the onCreateView() method is static than it's all good to set listener/s but in this case while we fill an array-adapter of string than it's a no-go since first of first it has to created its View therefore doesn't let the app to make any further listeners. In order to make that available, the onActivityCreated(Bundle) should be initiated/created between onCreateView() and onStart() methods and insert the rest of the code.
Here's the solution to link a ListFragment to another FragmentActivity class:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
}
i want to add two numbers using spinner view. here in my code two spinners .After i run the emulator it displays straight result only. it does not display spinner control and i'm not able to select the two numbers. Pls give one solution. Thanks in advance. Here code
package com.kk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}
This might be because the spinner's "onItemSelected" method gets called initially as soon as your code enters the onCreate method. Maybe you have to maintain flag values to do this.
These links might help you get started with it,
Spinner onItemSelected called erroneously (without user action)
Spinner onItemSelected() executes when it is not suppose to
Try to exchange
android.R.layout.simple_dropdown_item_1line
to
android.R.layout.simple_spinner_item
my coding is working well .But I want to store the edited spinner value in the database how it be done.here is my code
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class tooo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner localSpinner = (Spinner)findViewById(R.id.color_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.color_array, R.layout.my_normal_spinner_item_style);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localSpinner.setAdapter(adapter);
}
}
my simple_spinner_dropdown_item.xml
Add follwoing after setting the adapter in your code
localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
String text = localSpinner.getSelectedItem().toString();
//The above text variable has the selected value of spinner
}
public void onNothingSelected(AdapterView parentView)
{
}
});
Why didnt you edited question in your last post rather than creating a new post