changing layout and activity in spinner - java

First of all, I am fairly new at this, so bear with me.
I am trying to make a spinner where if you select an item, the layout and the activity will change, but I cant even implement the simplest of spinners.
so far the only code I've got is the one below, but no matter what I put in it doesn't do anything, so I must just not understand it, please be very specific in your answers. thank you. the first answer got me some of the way, but the "spinner spin..... spin.setAdapter(aa)" part is not accepted under OnCreate
String[] generations = { "Gen2", "Gen3", "Gen4", "Gen5", "Gen6","Gen7" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,generations);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
Intent intent2 = new Intent(this, gen2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(this, Gen3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(this, gen4.class);
startActivity(intent4);
break;
case 4:
Intent intent5 = new Intent(this, gen5.class);
startActivity(intent5);
break;
case 5:
Intent intent6 = new Intent(this, MainActivity.class);
startActivity(intent6);
break;
case 6:
Intent intent7 = new Intent(this, gen7.class);
startActivity(intent7);
break;
}
}

Here is a example, hope it helps you understand it better:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
switch (position){
// Check what position was selected
case 1:
//start activity depending on position
Intent intent = new Intent(this, ActivityOne.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
break;
case 3:
Intent intent = new Intent(this, ActivityThree.class);
startActivity(intent);
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#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_main, menu);
return true;
}
}

Related

How to add click to specific item in listView

I did ListView with some items, now I want when I click on specific item to send me to 'next' activity.
I write this code:
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
Intent intent = new Intent(DrugaStranica.this, MainActivity.class);
startActivity(intent);
}
});
But it is work for all item some, I want specific item to send me to specific activity.
Please use below code it may be helpful to solve your issue.
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
Intent intent;
switch (pos) {
case 0:
intent = new Intent(DrugaStranica.this, TestOneActivity.class);
break;
case 1:
intent = new Intent(DrugaStranica.this, TestTwoActivity.class);
break;
case 2:
intent = new Intent(DrugaStranica.this, TestThreeActivity.class);
break;
case 3:
intent = new Intent(DrugaStranica.this, TestFourActivity.class);
break;
default:
intent = new Intent(DrugaStranica.this, MainActivity.class);
break;
}
startActivity(intent);
}
});

How can i set absolute positions to items on a listView?

I created some Activities on my app,so i created a listView with the name of those activities , so i can click on them and open the activity that i want (with setOnClickListener method and switch.) and a i created a searhView, so i can filter them by its name (with setOnQueryTextListener).
Let's use 3 names as examples: Alfred,Beth,Bill
In the "normal" listView Alfred is 0, Beth is 1, Bill is 2. So i made this:
switch( position )
{
case 0: Intent newActivity = new Intent(telaPesquisa.this, alfred.class);
startActivity(newActivity);
break;
case 1: Intent newActivityy = new Intent(telaPesquisa.this, beth.class);
startActivity(newActivityy);
break;
case 2: Intent newActivity2 = new Intent(telaPesquisa.this, bill.class);
startActivity(newActivity2);
break;
When i search in the searchView for "B" just Beth and Bill appears, and that is right, but now Beth is case 0, and Bill is case 1, so it will not open the activity that i want.
How can i set absolute values to the itens? like Alfred is always 0, Beth always 1 and Bill awalys 2?
Here is my entire code.
public class telaPesquisa extends Activity {
ListView lv;
SearchView sv;
String[] teams={
"Alfred",
"Beth",
"Bill",
};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_pesquisa);
lv=(ListView) findViewById(R.id.listView);
sv=(SearchView) findViewById(R.id.searchView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//nt firstPosition = lv.getFirstVisiblePosition()+3;
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( position )
{
case 0: Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case 1: Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case 2: Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});
sv.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
}
sorry for my english
Modify the listener as this:
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( itemValue )
{
case "alfred": Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case "beth": Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case "bill": Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});

How to send null string using spinner on android

How to submit spinner selecteditem text via btnup and why selectedReport on btnup cases is declared not used?I have search hard enough to solve this but i still blankon what i need to do in order to get text on spinner to suit the coding.
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
Button bLogout;
ImageView viewImage;
Button b,btnup;
private String selectedReport = null;
} private void createSpinnerDropDown() {
//get reference to the spinner from the XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Array list of report to display in the spinner
List<String> list = new ArrayList<String>();
list.add("Crime");
list.add("Bribery");
list.add("Schools problem");
list.add("Homeless");
list.add("Rural Problems");
list.add("Public Transport");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selectedItem = parent.getItemAtPosition(pos).toString();
switch (parent.getId()) {
case R.id.spinner:
if (selectedReport != null) {
Toast.makeText(parent.getContext(), "Report you select is " + selectedItem,
Toast.LENGTH_LONG).show();
}
selectedReport = selectedItem;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String selectedReport = spinner.getSelectedItem().toString();
break;
}
}
Declare your views before onCreate in your class:
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
ImageView viewImage;
Button b, bLogout, btnup;
Spinner spinner;
String selectedReport;
In your onCreate, add the following:
String[] list = {"Crime", "Bribery", "Schools Problem", "Homeless", "Rural Problems", "Public Transport"}
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(this, "Report selected: " + list[position], Toast.LENGTH_LONG).show();
//You can remove this switch statement, if you don't need it.
switch (position) {
case 0:
//Do something
break;
case 1:
//Do something
break;
case 2:
//Do something
break;
case 3:
//Do something
break;
case 4:
//Do something
break;
case 5:
//Do something
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
I am assuming here that you have already set an onClickListener to your buttons in onCreate.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
selectedReport = spinner.getSelectedItem().toString();
break;
}
}

Listview inside Navigational Drawer Not responding to item click android

I am using DrawerLayout which has Listview
When I click on the Listview item , it does not responds to the click ( not launching another activity )
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
final ActionBar actionBar=getActionBar();
actionBar.setIcon(null);
actionBar.setTitle("");
setContentView(R.layout.main_activity);
// Custom ActionBar initializations.
getActionBar().setCustomView(R.layout.action_bar_custom);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
getActionBar().setDisplayUseLogoEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setDisplayShowCustomEnabled(true);
//getActionBar().setIcon(R.drawable.ic_drawer);
// Remember me.
sharedPref=getSharedPreferences("myPref", MODE_PRIVATE);
editor=sharedPref.edit();
//Listview
mDrwawerList=(ListView)findViewById(R.id.list_slidermenu);
listDataHeader = new ArrayList<HashMap<String,String>>();
listDataHeader_temp = new ArrayList<String>();
navigational_adapter=new NavigationListAdapter(MainActivity.this, new ArrayList<HashMap<String,String>>());
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.nav_btn,0, 0){
public void onDrawerClosed(View drawerView){
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrwawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView txtCategoryName=(TextView)view.findViewById(R.id.txt_category);
String category_name=txtCategoryName.getText().toString();
Log.e("category_name", "karjeevcategory_name "+category_name);
if (category_name.equals("Home")) {
}
else if (category_name.equals("Diamond of the Week")) {
}
else if (category_name.equals("About Us")) {
}
else if (category_name.equals("FAQ")) {
Intent intent=new Intent(MainActivity.this,FAQActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else if (category_name.equals("Press & Download")) {
}
else if (category_name.equals("Contact Us")) {
}
}
});
new PerformGetCategory().execute();
}
Replace your existing code with this,and create a Intent inside particular position case of listview, if FAQ item is at 5th position, create Intent inside case 4:
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
displayView(position);
}
});
And inside displayView(int position) keep this:
private void displayView(int position) {
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
Intent intent=new Intent(MainActivity.this,FAQActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
break;
case 5:
break;
case 6:
break;
default:
break;
}
}
Hope this solves your problem.
I solved my problem from my end ...
There was layout issues , due to which the listview was not able to handle the click ..
I pasted my litsview code to bottom of the other layout code which was inside DrawerLayout

Use spinner in external class because needed in all other activities

My question is how can i put my spinner in an external java class and implement it in all other activities (works as a menu), here is my spinner code:
final Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(0, false);
// this will be called when you select any item in this spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// get the text at that position
switch(position) {
case 0: {
Intent NewPost = new Intent(getApplicationContext(), StartingPoint.class);
startActivity(NewPost);
break; }
case 1: {
Intent NewPost = new Intent(getApplicationContext(), Simmering.class);
startActivity(NewPost);
break; }
case 2: {
Intent NewPost = new Intent(getApplicationContext(), LugnerCity.class);
startActivity(NewPost);
break; }
case 3: {
Intent NewPost = new Intent(getApplicationContext(), Gmunden.class);
startActivity(NewPost);
break;}
case 4: {
Intent NewPost = new Intent(getApplicationContext(), Salzburg.class);
startActivity(NewPost);
break; }
case 5: {
Intent NewPost = new Intent(getApplicationContext(), Linz.class);
startActivity(NewPost);
break; }
case 6: {
Intent NewPost = new Intent(getApplicationContext(), Saalbach.class);
startActivity(NewPost);
break; }
case 7: {
Intent NewPost = new Intent(getApplicationContext(), Innsbruck.class);
startActivity(NewPost);
break;}
case 8: {
Intent NewPost = new Intent(getApplicationContext(), Reutte.class);
startActivity(NewPost);
break; }
case 9: {
Intent NewPost = new Intent(getApplicationContext(), Bregenz.class);
startActivity(NewPost);
break; }
case 10: {
Intent NewPost = new Intent(getApplicationContext(), Kufstein.class);
startActivity(NewPost);
break; }
case 11: {
Intent NewPost = new Intent(getApplicationContext(), Bratislava.class);
startActivity(NewPost);
break; }
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
it shoud be saved for example in menu.java and should be called in every activity, how do i do that correctly? thank you in advance.
If you have the same menu in all your activities, the best way to do this is by creating a superclass that extends Activity and let all your other activities extend this activity.
public class BaseActivity extends Activity { // menu code }
public class StartingPoint extends BaseActivity { //... }
Try creating your own Spinner class:
public class MenuSpinner extends Spinner {
public MenuSpinner(Context context) {
super(context);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(adapter);
setSelection(0, false);
.
.
.
}
}
And then add it in your layout
<com.yourpackage.MenuSpinner
android:id="comboCasino"
.
.
. />
If you want more detailled information visit http://developer.android.com/training/custom-views/index.html
Create a constructor for menu.java
menu(Context mcontext){
this.mcontext = mcontext;
}
Now in place of getApplicationContext() call it by passing of object of class where you want to use Spinner property.
Menu object = new Menu(object_of_current_class);
object.ShowSpinner();

Categories

Resources