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

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
}

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.

need to reload mainActivity controller on click of button located in customized listview

To make my question more understandable let me start with an image of my view.
I have an xml file named Menu, that has customized list view in it. I have created another xmlview named MenuCell as below.
Now tapping on add button I'm adding Item to the cart. which is working perfectly fine except not updating value of a cart (top right corner) on click event. But If I navigate to different view and come back to this view at this point I'm getting number of items added in the cart reflected properly. So How Can I reload my controllerview when I tap in my arradepter view's ImageButton.
this is my adapter code
holder.imageButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addItem(position);
notifyDataSetChanged();
}
});
void addItem(int position) {
count++;
}
Where count is item added count.
If anyone can tell How am I able to reflect this count of my arrayadpter class to my other controller class that holds actual list view.
Any Help will be appreciated
Thanks in advance.
You need to use callback feature to get notified to your activity , hence add button is part of list component so you can update your list view.
Here are few links for understanding of call back using interface
http://cleancodedevelopment-qualityseal.blogspot.in/2012/10/understanding-callbacks-with-java.html
http://stackoverflow.com/questions/11577695/what-is-a-call-back-interface-in-java
After :
notifyDataSetChanged();
just add:
YourList.invalidateViews();
YourList.scrollBy(0, 0);
Send the reference of your controller to your list adapter class. In the controller I guess you have a method that computes some data and after that call update the view that holds cart data summary. Just make sure that update is on UI thread.
Your List is not refreshing at the instant because you have to refresh data of your adapter and then call notifyDataSetChanged();
Suppose You have the data in an array which is visible in textview.Then In your function addItem add the data into ur array and then call notifyDataSetChanged();
This will immediately tell the list view that watever data it is containing is changed so time to refresh.
If you can paste more of ur adapter code i can be helpful

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.

Change pages with Spinner

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.

Avoid Multiple Clicks

I am having an app which contains number of list views. That means One listview opens another listiview on click of an item from first listview. Then it opens third one, on click of item from second list and so on. Now problem here is that the listview takes some time to populate data, giving user a feel that the click is not acknowledged and user presses it again. Due to this the first click opens the 2nd list and 2nd click opens third list, which is not desired. How can I take care of this. I tried using listview.setEnabled(false) but that doesn't help. I don't want to show a progress bar. Can i disable the 2nd click or any other way to handle this problem?
Set a global boolean $bBeenClicked, make it true on the first click and check it on the second; set it back to false when the list has finished loading.
You should look into enhancing the performance of your ListViews. Unless they need to access something over the internet or have some other similar constraint, they shouldn't take so long to load. A good ListView should only load what can be shown on the screen and then load more as needed when the user scrolls down. There are a few techniques you can use like making sure you reuse the convertView aswell. I would recommend that you watch this video. It goes into the best ways to implement ListView so that it performs well.
You could add haptic feedback for when the user clicks on a listview item to tell the user that it was clicked.
How to enable haptic feedback on button view
You shoudl load the new List in a PpgressDialog:
public static void open_new_list(int level)
{
verlauf = ProgressDialog.show(ctx, "Wait...", "...load new List",true,false);
new Thread(){
#Override
public void run(){
Looper.prepare();
//load the new Listview
//display via runnable
}
verlauf.dismiss();
}
}.start();
}
edit:
Without Progressbar you might remove the adapter after klicking, then the user isn't able to klick twice

Categories

Resources