How to link activity to element of a spinner? - android

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.

Related

ListView item deletion not happening

The aim is that store the string in the normal listview and then user touch(click) it move to next page along with user selected item. Once user click "Reject button" in the Operation.java, the should go-off from the List.java activity. It is not happening. It shows "Unfortunately, System has stopped".
MainActivity.java
public static int loop_exute=0 // first page variable
Intent i = new Intent(getApplicationContext(), List.class);
startActivity(i);
List.java
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class List extends ActionBarActivity implements OnItemClickListener {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
Button buttonSum;
static String[] name;
ArrayList<String> planetList = new ArrayList<String>();
int pos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mainListView = (ListView) findViewById( R.id.mainListView );
Bundle extras = getIntent().getExtras();
if(MainActivity.loop_exute==0) // acces 1st page value because the string array value sholuld load only once in its life time
{
MainActivity.loop_exute=MainActivity.loop_exute+2;
name = new String[] { "AAA", "BBB", "CCC", "DDD"
};
planetList.addAll( Arrays.asList(name) );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
mainListView.setAdapter( listAdapter );
//mainListView.getChildAt(0).setBackgroundColor(Color.RED);
show();
}
else // second time call
{
Intent intent = getIntent();
if(intent.hasExtra("MESSAGE"))
{
Bundle bd = getIntent().getExtras();
if(!bd.getString("MESSAGE").equals(null))
{
String object=bd.getString("MESSAGE");
int pos=planetList.indexOf(object);
planetList.remove(pos);
listAdapter.notifyDataSetChanged(); //show();
}
}
}
}
public void show()
{
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Intent i = new Intent(getApplicationContext(), Operation.class);
i.putExtra("Value2",name[pos]);
startActivity(i);
}
});
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
Operation.java
reject = (ImageButton) findViewById(R.id.imageButton2);
reject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), List.class);
i.putExtra("MESSAGE",value1); //send the list item value what we select in previous activity
startActivity(i);
}
});
In the setOnClickListener under Operation.java, you are trying to start another activity. Instead, insert code to manually destroy the activity. You can also use the finish() method. Here's a link to know more about it:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

changing to a specific activity when a listview item is clicked

I have a list view with 3 items , I want to move to a new activity depending on the item being clicked , that is each item when clicked should move to a different activity . I have tried the solution which were given to similar problems , but I still didn't figure out how to make it go to another activity
package com.example.wizer;
import java.util.ArrayList;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
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;
public class Options extends Activity implements OnItemClickListener {
WifiManager wifi;
BroadcastReceiver receiver;
IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(rcver, filter);
ListView list = (ListView) findViewById(R.id.l1);
ArrayList < String > List = new ArrayList < String > ();
List.add("List available networks .");
List.add("List APs .");
List.add("List Networks according to thier signal strength .");
ArrayAdapter < String > adp = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, List);
list.setAdapter(adp);
wifi.startScan();
}
// On create
final BroadcastReceiver rcver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
};
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_options, menu);
return true;
}
public void onItemClick(AdapterView <? > arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (arg3 == 0) {
Intent NL = new Intent(Options.this, Networks.class);
startActivity(NL);
} else if (arg3 == 1) {
Intent NL = new Intent(Options.this, AP.class);
startActivity(NL);
} else if (arg3 == 2) {
Intent NL = new Intent(Options.this, Signal.class);
startActivity(NL);
}
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
This line will give you the row number and a reference to the view for that particular cell. In your View, you can add a property with the class so you can later open that activity. This will require a custom view class and a custom adapter to fill it.
Alternatively, if your list will be simple, you can create an Array of classes that match the ordering of the rows.
Class[] classes = new Class[2];
classes[0] = Integer.class;
classes[1] = View.class;
Then you can just use the row number to get the correct class reference.
Edit:
your code should be like this
public class Options extends Activity implements OnItemClickListener {
Wizer wizer;
BroadcastReceiver receiver;
IntentFilter filter;
HashMap<String,Class> map=new HashMap<String,Class>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
wizer.Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
filter=new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(rcver,filter );
map.put("List available networks .",Networks.class);
map.put("List APs .", AP.class); // your second class name
map.put("List Networks according to thier signal strength .",Signal.class); // third class name
}
final BroadcastReceiver rcver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
};
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_options, menu);
return true;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Class c= map.get(arg0.getItemAtPosition(arg2).toString());
Intent NL =new Intent(Options.this,c);
startActivity(NL);
}
}
Try this
Take a HashMap
HashMap<String,Class> map=new HashMap(String,Class);
map.put("List available networks .",Networks.class);
map.put("List APs .", Second.class); // your second class name
map.put("List Networks according to thier signal strength .",Third.class); // third class name
And then in onItemClick
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Class c= map.get(arg0.getItemAtPosition().toString());
Intent NL =new Intent(Options.this,c);
startActivity(NL);
}
Note:When you have less items don't use ListView, use LinearLayout with scrollview instead
Here is the answer :
I had to add this after the list declaration
list.setOnItemClickListener(this);

Spinner listener not working

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();

Deleting from base with ContextMenu

So i am preety new with this SQLite i finaly managed to create base and work with it etc. Now i created ContextMenu thath you will be able to delete item from listView on which context menu was opened. Here is my current code.
import java.util.List;
import java.util.Random;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.view.MenuItem;
public class Ann extends ListActivity{
private CommentsDataSource datasource;
EditText edit;
ListView bump;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ann);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
registerForContextMenu(getListView());
}
public void onClick(View view) {
#SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
edit = (EditText)findViewById(R.id.editTxt);
Editable txt=(Editable)edit.getText();
String vnos = txt.toString();
comment = datasource.createComment(vnos);
adapter.add(comment);
edit.setText("");
break;
}
adapter.notifyDataSetChanged();
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
protected void onResume() {
datasource.open();
super.onResume();
}
#Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
Now i know thath i need to make method for public boolean onContextItemSelected(MenuItem item)
but how to manage now to delete item which was selected.
Can I ask why you don't use a SimpleCursorAdapter to create the list? It looks like you're getting a cursor and turning into an array... why take that extra step? It's useful in that if you make an addition/deletion to the database, you can simply use the requery() method to regenerate/update the list.
Anyway, should you decide to go with a SimpleCursorAdapter, onContextItemSelected(MenuItem item) would be the way to go. An example from one of my projects:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if (item.getTitle() == "Delete Member") {
mDbHelper.deleteMember(info.id);
mMemberCursor.requery();
return true;
}
return super.onContextItemSelected(item);
}
Note that I created my menu view programatically, so you may need to change the if statement to something that will work better for your set-up with an inflated menu resource.
info.id is the row id from the database that represents the line of the list you clicked.
mDbHelper.deleteMember() is a method from my database helper for deleting records.
Hope this helps!
The context menu is not listItem specific, ie. id does not know which item was clicked.
Go for an onItemClicked or onItemLongClick version.
To do the latter implement the onItemLongClick interface on you activity
public class Ann extends ListActivity implements OnItemLongClickListener
and add the onItemLongClick method
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// do something with your datasource and the position
return false;
}

add two number using spinner in android

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

Categories

Resources