I have a view that extends Gallery (In order to create a cover flow).
Now I need the title of the currently selected (speak: The one that's shown in the front) to be displayed at the top.
So far, that's no problem since I can just use:
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(mSpinnerAdapter.getItem(position)!=null) {
mSelectedTextView.setText((CharSequence) ((MyDataItem)mSpinnerAdapter.getItem(arg2)).getName());
}
}
The data comes from a web service and is therefore slow, so as long as the data is not loaded, I am displaying a progress bar. I am doing this by telling the adapter to deliver a progress bar in "getView(..)"¨and a "1" in "getCount()" as long as the data structure is not fully loaded (Hence the if(mSpinnerAdapter.getItem(position)!=null) in the code above).
Now the problem is this: When the data from the web is loaded, I call onDatasetChanged() on the adapter and the gallery view is being updated (which works), but without onItemSelected() being called. The consequence of this is that the title is not set for the first item.
How can I solve this? Or is my technique to display the progress bar wrong alltogether?
Thank you!
Related
I am trying to build Tic-Tac-Toe game( just for human play with android device, I have implemented a smart algorithm called minimax for my bot) by using android GridView. I have built everything successfully( UI, minimax algorithm for my bot) but there is still a hard to understand problem, that is when I click on Gridview item, I want GridView must update immediately, then my bot start it calculating.So, I have implement my code to set on GridView item click as followings:
gridviewBoard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
//if it is a legal moves of player
if (isLegal(false, i)){
//put X into board
put(false, i);
//I want to update gridview immediately by using notifyDataSetChanged()
chessAdapter.notifyDataSetChanged();
//then start my bot calculating
// findBestMove is a method to find the best move for my bot
// by using minimax algorithm
int nextMove = findBestMove();
}
}
}
Howerver, the gridviewBoard did not update immediately, the gridviewBoard only updates when the findBestMove function is computed while it takes a long time to calculate. Summary, I want to update gridviewBoard immediately after item click and before my bot calculating. Can someone help me, and sorry for my bad English grammar.
I have been working on a certain project which involves the use of web service. I have a list of products that will display in UI as Image,Button to Like,Add To List and some other. I have implemented these items in a Custom ListView and added the products in the Adapter class as always. If I select a Like button for a certain image then the button should change to Unlike, the problem that I am facing here is that the button changes for all the items i.e. all the buttons in the list view gets changed to Like which is idiotic.
Question :
How do I pinpoint a certain list item to get changed (or) How do I change a single button in a list ?
I am short of logic to be used here. Any help will be much appreciated.
Without code it is difficult to help specifically, but have you tried using something like,
listview.getChildAt(position);
to reference a particular list item.
Depending on your code, you can modify this:
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
//on click of any item.
Toast.makeText(getApplicationContext(), "Your position"+position, Toast.LENGTH_LONG).show();
//This is just for example to obtain the position of the listview item.
TextView example= (TextView)findViewById(R.id.sometext);
example.setText(String.valueOf(position));
}
});
Hope this gives you some information..:)
I have a gallery where I'm displaying a number of ID cards as images.
When the user clicks on an image, I want to change the image to another one which shows the 'back' of the card.
I have bound an arraylist of objects to the gallery, each object contains the front and back images.
I have two image views, one for the front and one for the back image. The gallery will initially show all front images ( the 'back' image view visibility is set to GONE). My idea was to change the visibility of the relevant image view when the user clicks.
That's about as far as I've got, buy I'm not sure how to implement the click functionality to change the views.
Any assistance would be greatly appreciated.
A Gallery is a specialization of AdapterView, which also contains ListView, Spinner, GridView... All of these have in common to display n times the same 'kind' of view with different data.
These particular views use an Adapter to handle the view creation and population process. The getView method is called on the adapter, providing this class with the data for creating the view for a particular row (cell, gallery item ...).
The getView method is called when the AdapterView deems it necessary, and the developer has no control on which view gets created or when it does.
Therefore, it is practically impossible to modify a child of the AdapterView directly. It would result it the modification being arbitrarily overwritten, or even transferred to another row of the view.
Instead, to act on the child views, it is necessary to act on the data that are used to populate the view. (Note that this enforce the MVC model, as the view remains a reflection of the model, rather than being manipulated by the controller.)
In this particular example, the view is to display a different image based on the checked status of the data. To achieve that, add a checked member to the model :
public class MyModel {
// ... Other members
public boolean checked = false;
}
Use that value in getView :
#Override
public View getView(int position, View convertView, View parent) {
MyModel current = getItem(position);
// ... Inflate and populate
if (current.checked) {
// Specific checked behavior
} else {
// Not checked behavior
}
}
The checked value is to be modified in the controller, probably a OnItemClickListener:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Here, there can be several ways to access the clicked item.
// I assume myAdapter is the adapter used for the gallery
MyModel current = myAdapter.get(position);
current.checked = !current.checked;
myAdapter.notifyDataSetChanged();
}
I have two spinners that I want to be "tied" to one another in a mutually-exclusive sense: If you select an item in one, that item's text turns red and appears at the top, while the other one goes back to displaying the initial ("title") selection (if another item was previously selected), and its text turns white.
This is all done via the onItemSelected listeners:
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
issueSpin.setSelection(0);
((TextView) issueSpin.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));
((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
....
and vice versa for the "issue spinner". My problem is that, if I'm going from one spinner to the other and I select the top item, the onItemSelectedListener doesn't register because the item being selected is already selected.
I've been told that this is not possible. Or, rather, I've been told that it is impossible for an onItemSelected listener to fire on an item that is already selected. While I realize this is technically true, this problem seems relatively simple and I'm sure there must be a workaround to produce the desired effect.
I have a few questions regarding some that I'm pondering:
Is there any way to set all items in a spinner as unselected, while still displaying one of them?
Could I utilize a different type of event (i.e. 'setOnTouchListener', 'setOnClickListener', etc), presumably on the top item, in conjunction with the onItemSelectedListener?
Should I utilize a different type of event by itself, perhaps on the Views inflated in the spinner themselves, without the onItemSelectedListener?
Can you help me find a better strategy than those alluded to in the bulletpoints above?
Instead of using a Spinner, use a Button that visually resembles a non-active Spinner. When the button is clicked, show a custom Dialog containing a ListView. You can use the setSelection method and/or the position in the Adapter's getView to show which item is currently selected. The ListView's OnItemClickListener will get notified that something got clicked regardless of whether the item was selected or not.
When you find which item was clicked, hide the Dialog and notify the button-containing Activity of this, so that it can update both Buttons if needed.
I have implemented something really similar to this using DialogFragments and a DialogFragmentParent and it works fine.
Have you consider something like below,
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("sectionSpin");
}
}
And for other one
issueSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("issueSpin");
}
}
and now
private void updateView(String spinner){
if (spinner.equals(sectionSpin)){
//update spinner per requirement
}
else{
//update spinner per requirement
}
}
You can also consider some additional variable or parameters to keep track if the item selected for first time or not.
So, you could make the first item in the spinners not valid selections like "[Choose one]" or "[Select Title]" forcing the listener to fire every time.
This would have the added benefit that you will know user did select something before the next step.
Of course, this would depend on desired application flow.
How can I get the position of current image displaying in Gallery view while it is swiping?
So I don't think that is possible because of the way adapter views work. Basically most if not all calls to the adapter view's adapter have to be on the UI thread to work properly. And the animation is definitely on the UI thread. So since they both can't be happening at the same time on the same thread, you can't actually (accurately) read the position until the swiping animation is over.
If you ask the adapter getSelectedItemPosition() on any other thread it might give you the current position but might also be stale for some time since I am guessing the UI thread is going to update the value without checking for an exclusive lock.
We can get the position when we are swiping the image in gallery view,you overwrite Gallery View's setOnItemSelectedListener.
We have two methods from that listener like
avatar_gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view,
int position,
long id){
Log.v("Selected", ""+position);
}
#Override
public void onNothingSelected(AdapterView<?> parent){
}
}