Change pages with Spinner - android

I created a spinner populated with values from a string array in my "strings.xml. When a user selects an item in the spinner I would like it to redirect them to that page, and then be able to return to the main page via a back button.
How do you send them to another page via a spinner selection?

Add an on tem selection listener. In the listener's onItemSelected method, you'd use position to determine the selected position in the array. Based on the position, you know which string was clicked. Then you'd create an Activity for each "page" and start the appropriate activity.

Related

RecyclerView Data passing to same Activity

I have a cart program, in activity is simple, there was a recyclerview and a button. In recycler view I can edit its stock.
Now, I want to get that product_name and product_stock from that I've ever clicked and make changes.
Now, when every stock has been clicked, I want click button on activity, so I want that data I've ever clicked on recyclerview stored to array, so that button can do their action / function. Can you guys lead me, how to import data from recyclerview to its activity itself.
It was Android program to do shopping cart, so I click the data in recycler view it stored to activity array.
I don't have any idea how to store that from recyclerview to activity's array.
My expected result, should be, when I click buy button on activity, every changed stock on recyclerview is shown.
You can simply implement an interface in your adapter like this for getting the values from clicked position in recycleview.
public interface FetchRecyclerViewItems{
void getItems(String product_name,String product_stock);
}
And simply create a setter method for this interface in adapter like this,
private FetchRecyclerViewItems fetchRecyclerViewItems;
public void setFetchRecyclerViewItems(FetchRecyclerViewItems fetchRecyclerViewItems){
this.fetchRecyclerViewItems = fetchRecyclerViewItems;
}
Then set the values like this on your click like this in OnBindViewHolder,
your_view.setOnClickListener(new View.SetOnClickListener)....{
....
fetchRecyclerViewItems.getItems(product_nameFromPosition,product_stockFromPosition);
}
And implement this interface in your activity and you will get the product_name,product_stock values there.
Make sure to initialize the FetchRecyclerViewItems in your activity.

Display db content in another acitivity by clicking on item in listview

So I have this problem, my listview is populated from Sqlite DB and I can click on any item in the listview. When I click on an item I want to display, in a new activity these "name, surname, age etc" details but these should be retrieved from my database.
Can anyone help me?
Thank you.
Use onItemClickListener method in your listview. When user clicks an item pass the item into next activity and in next activity retrieve data based on the item click. This tutorial will help you to get a good idea.
Import and Use External Database in Android.
Set an onItemClickListener for your ListView and when an item is clicked, you could either:
Place into a Bundle the details you want to be given to the Activity intent
Or
Place as an extra/bundle to the Activity intent the ID of the item you retrieved, then retrieve the details you want from the DB using the given ID inside of the next Activity

How can I change a Preference when WearableListView snaps to new list item?

I've implemented a WearableListView in order to allow the user to choose a preference in my app. Currently, whenever the user clicks on an item, I save that item's tag in a preference. This code lives in the onClick method of the class implementing WearableListView.onClick
I've also noticed that when I change the item selected in the list (the item in center screen)
that persists upon leaving and coming back to this list. So I'm wondering how can I access that offset value? Or what method is called in order to make your current item persist once you leave and come back to a list? I would like the user to not need to click on the list item, but simply scroll away and that item tag save.
I cannot test it right now, but please take a look at WearableListView.OnScrollListener class.
There is a promising-looking method called onCentralPositionChanged(int centralPosition). Just add it via addOnScrollListener(WearableListView.OnScrollListener listener) method and update your preferences in onCentralPositionChanged callback:
public void onCentralPositionChanged(int centralPosition) {
// update your preferences according to centralPosition
}

ListView dynamic onClickListener USING Database ID

I'm creating a restaurant menu app with categories and items loaded from database and put them in a listview. Every category has it's own items. I would like to following behaviour: when I click on some category it should go to another activity and list it's items of the category id that listed in DB.
How do I create a dynamic onItemClickListener to send that ID to another activity to load it's items?
Its not about dynamic listener. Listener should be only one which would send chosen category id to somewhere where you could use further. Like sending it in intent's bundle to some activity.

Advice needed with context menu

I have a XML layout which has two edit text fields, one for "title" and the the other for "story". When the user enters his data in these text fields and presses the back button the entry gets saved in a list view as the title set. The list view is present in an activity say A1. Now A1 extends Activity.
Whenever an item in the list is "long clicked" a context menu appears with edit, delete and read buttons. If the edit button is pressed I need to open another activity which can edit the data entered in the text fields corresponding to the item clicked. Also I'd be needing the id and the position of the item clicked in the list.
I am using list variable of type ListView to add my adapter. Also I am checking the edit, delete and read options of the context menu in the `public boolean onContextItemSelected(MenuItem item)' method.
How can I get the id and the position of the item clicked from here?
in adapter you make a getter and setter of your item. When long click listener, put setter your item in there.
You should store your "title" and "story" in database and you can get it form database in a new activity
I would think the best way to do this is to create an instance variable (declared after class definition and before onCreate) for the listview and assign the list view to it in onCreate(). Then you will be able to access the listview from your onContextItemSelected() method and pass them to your new activity
If you could post some of your code we should be able to help more.
I second Th0rndike's comments above. It is much easier to help someone if the question is readable and there is a good chance the answer will be accepted.

Categories

Resources