How to update references in OnItemSelected()? - android

Is it possible to update references in OnItemSelected after notifyDataSetChanged was called?
e.g
ImageView onFront;
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
onFront = (ImageView) view;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I am using onFront to rotate image, but after I call notifyDataSetChanged - I cannot.

Make your code to remember which view was clicked.
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
view.setTag(postion);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Then use this position in getview of adapter
int pos = (Integer)view.getTag();
if(pos == position){
//Make your change in view to make it look different
}

To update the reference and reflect it into view , you should call notifyDataSetChanged(). This is the only way to tell your adapter that something has changed , Who is controlling view.

Try calling this method after your method:
listview_adapater.invalidate();

Try to make onFront static like this.
private static ImageView onFront;

Related

OnItemSelectedListener for Spinner in Android development

I'm using a spinner in order to select a certain value, and in the activity that contains the spinnr, I've implemented OnItemSelectedListener.
in the OnCreate() method, I've defined:
spinner1.setOnItemSelectedListener(this);
but it doesn't get to the method onItemSelected(), my guess is that I'm not sending the right variable ("this").
what should I send to setOnItemSelectedListener in order for this to get to the method?
Thank You!
activity that contains the spinnr, I've implemented
OnItemSelectedListener
If OnItemSelectedListener listener is implemented in Activity then override onItemSelected in Activity :
#Override
public void onItemSelected(AdapterView<?> parent,
View selectedItemView, int position, long id) {
// your code here
}
Try this.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Do something
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Also you can look at this. http://developer.android.com/guide/topics/ui/controls/spinner.html

ListView ContextMenu in Fragment

In Fragment onCreateView i have the following code
consumerlist=(ListView)view.findViewById(R.id.consumerlist);
consumerlist.setAdapter(new consumerListAdapter(getActivity(), dataList));
consumerlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "ID"+id, 1);
}
});
Button btn=(Button)view.findViewById(R.id.button1);
registerForContextMenu(consumerlist);
return view;
if i write registerForContextMenu(btn) then it works.but list view row long click does nothing.Even my listView onItemClick listener does not work.how to fix it?Anybody helps me greatly appreciated.Thanks in advance.
try this:
consumerlist.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
registerForContextMenu( view );
openContextMenu( view );
return false;
}
});
also this tutorial should help you a lot ...
//EDIT
Move
registerForContextMenu(consumerlist);
to
public void onActivityCreated(Bundle savedState) {
Remove scollview in layout or define it as not focusable make the code working.

OnItemSelectedListener of ListView is not getting envoked

I have a list view and using two listeners - OnItemClicked and OnItemSelected. The clicked listener is working properly but onItemSelected Listener is not getting invoked. I need OnItemSelected listener because sometimes selected is set pragmatically.
allClues.post(new Runnable() {
public void run() {
listView.setSelection(ind);
}
});
I am expecting that when selection is set its OnSelection listener will be invoked. But its not happening.
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iSelected = arg2 ;
Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
What could be wrong here ?
OnItemSelectedListener is used for the Spinner not for the Listview For Listview you have to use OnItemClickListener
You have to use OnItemClickListener like this
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int i,
long j) {
// do ur code
}
});

Custom Drag Sort Listview with Radiobutton

I have a DSLV with custom adapter which has two textviews and a radiobutton. I want the radiobutton of a particular row to be setchecked(true) when that row is clicked. To do that I used the below mentioned code.
public OnItemClickListener listclk = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(!rad.isChecked())
{
rad.setChecked(true);
}else{
rad.setChecked(false);
}
}
};
But its not working properly. Please Help!!...Thanks
Try this:
(assuming your DSLV is called "list")
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
finally the problem got sorted out by adding ..
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return namelist.size();
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
after the getview().

android how to give click event on gallery item

http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
Just go through the above link. there is an example sample code for Android Coverflow widget.
I want to give click event on the images. How can I do it.
Plz suggest me any way.
Thank you
Follow below code
mCoverFlow.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
// TODO Auto-generated method stub
}
});
Hmmm, since the widget extends from Gallery, presumably you can just add an onItemClickListener:
coverFlowView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> gallery, View view, int position, long id) {
//do stuff!
}
});
in the same way, you can set an onItemSelectedListener, which fires whenever an item is "selected" (comes in the center of the gallery:
coverFlowView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> list, View view, int position, long id) {
//do stuff!
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Categories

Resources