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.
Related
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;
I took multiple spinners in single view. I know how to get when item is selected.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id)
{
parent.getItemAtPosition(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
But how can i know that which spinner user has clicked on in a single listener?
Any suggestion will be appreciated.
Thanks In advance.
Try the following way.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch(parent.getId()){
case R.id.spinner1:
//your code goes here
break;
case R.id.spinner2:
//your code goes here
break;
}
}
Refer here and here too.
parent in onItemClick refers to the Spinner that was clicked.
AdapterView.OnItemSelected
parent The AdapterView where the selection happened
Spinner is an AdapterView. Spinner
ListView lv=(ListView) findViewById(android.R.id.list);
lv.setOnItemLongClickListener(new OnItemLongClickListener()//error:change to <listeners>
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {
//here is where i got the classCastException.
String[] tmp = (String[]) arg0.getItemAtPosition(row);
//tmp[0] ist the Text of the first TextView displayed by the clicked ListItem
Toast.makeText(ReminderListActivity.this, tmp[0].toString(), Toast.LENGTH_SHORT).show();
return true;
}
});
change to lv.setOnItemClickListener
You need to fix your imports.
you probably import the wrong class and now eclipse thinks you want something else.
You should use below listener:
m_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1, int p_arg2, long p_arg3)
{
// TODO Auto-generated method stub
}
});
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) {
}
});
I have a listview with an imageview in each row.When i click on imageview it should change to another image view. pls help me with coding.
Try this code:
lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
view.findViewById(R.id.imageview).setBackgroundResource(R.drawable.home);
}
}