How do I enable click on listview? - android

I'm currently working on a code which is written by someone else in my office. I need to enable selection of items in the listview. I know I have to use setOnClickListener. Can someone guide me through this? I've written:
Edit:
listview.setOnClickListener( (View.OnClickListener) this );
Now what do I do?
I need to select an int value and pass it on to another function, which is used to retrieve a certain set of values from a db.

listviewName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//variable position will give you the required element/object from the array list.
}
});

YourListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long l) {
try {
Toast.makeText(this,"Position is===>>"+pos , Toast.LENGTH_LONG).show();
}
catch(Exception e) {
System.out.println("Nay, cannot get the selected index");
}
}
});
Hope it helps.

ListView list = (ListView)findViewById(R.id.quoteX2);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// - -- - -Your Code here
}
});
Hope it helps!

Related

Setting up OnListItemClick Listener

How do i setup a onListItemClick listener so that the i can return only the value from R.id.number from the adapter that ive setup below. I will later pass the value i obtain to another activity.Please Help
sd=new SimpleAdapter(MainActivity.this,alist,R.layout.item_list,new String[]{"No","Name"},new int[]{R.id.number,R.id.item_name});
list.setAdapter(sd);
Try this
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
String number=((TextView)view.findViewById(R.id.number)).getText().toString();
}
});

android - How to uncheck the already selected checkedtextview in sigle choice listview

I am trying to uncheck the already selected checkedtextview in single choice list.I tried with the following code but it's not working. Please can anyone help me.
How do we know, we are trying to select the already selected one pro grammatically?
Code
lstAttribs.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View v, int nItemPosition,long arg3)
{
if(lstAttribs.isItemChecked(nItemPosition))
{
lstAttribs.setItemChecked(nItemPosition, true);
}
else
{
lstAttribs.setItemChecked(nItemPosition, false);
}
}
});
Its too late but may help some one.
set your listview choice mode to single choice and declare global integer variable with value -1.
int cPos=-1;
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (cPos == position) {
if(listView.isItemChecked(cPos)){
listView.setItemChecked(position, false);
}else{
listView.setItemChecked(position, true);
}
} else {
listView.setItemChecked(position, true);
}
cPos = listView.getCheckedItemPosition();
}
});
Try lstAttribs.clearChoices().

Getting selected item from listview

I have implemented list view in android using custom adapter. In each there is two or more fields . i want to get specific field on which click is made. how will it possible ?any help appreciated.
set
setOnItemClickListener
on the listView. e.g
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//"Here you will get the position on which you have clicked"
// and if you want the item at that position you have to **adapter.getItem(position)**
}
});
see if it worked for you
Try This :
lstvw.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String) lstvw.getItemAtPosition(myItemInt);
System.out.println("array:> > > > > > >========================>"+selectedFromList);
}
});
You need to implemet implements OnItemClickListener and override onItemClick method. override onItemClick method like this:
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
yourList.get(position);//This will give you object on specfic pisition clicked
Intent myIntent = new Intent(getBaseContext(), classToCallOnClick.class);
.
.
.
.
}
Hope this will help you.....

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) {
}
});

How to handle ListView click in Android

How do I listen to click event on a ListView?
This is what I have now
ListView list = (ListView)findViewById(R.id.ListView01);
...
list.setAdapter(adapter);
When I do the following
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView,
int position, long id)
{
setDetail(position);
}
public void onNothingSelected(AdapterView parentView) {
}
});
That doesn't seem to do anything on click.
And all those code live within a class that extends Activity.
On your list view, use setOnItemClickListener
Suppose ListView object is lv, do the following-
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv.getItemAtPosition(position);
/* write you handling code like...
String st = "sdcard/";
File f = new File(st+o.toString());
// do whatever u want to do with 'f' File object
*/
}
});
You need to set the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
convertView = mInflater.inflate(R.layout.list_item_text, null);
convertView.setClickable(true);
convertView.setOnClickListener(myClickListener);
and declare the click listener in your ListActivity as follows,
public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
//code to be written to handle the click event
}
};
This holds true only when you are customizing the Adapter by extending BaseAdapter.
Refer the ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/List14.java for more details
The two answers before mine are correct - you can use OnItemClickListener.
It's good to note that the difference between OnItemClickListener and OnItemSelectedListener, while sounding subtle, is in fact significant, as item selection and focus are related with the touch mode of your AdapterView.
By default, in touch mode, there is no selection and focus.
You can take a look here for further info on the subject.
This solution is really minimalistic and doesn't mess up your code.
In your list_item.xml (NOT listView!) assign the attribute android:onClick like this:
<RelativeLayout android:onClick="onClickDoSomething">
and then in your activity call this method:
public void onClickDoSomething(View view) {
// the view is the line you have clicked on
}
You have to use setOnItemClickListener someone said.
The code should be like this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text or do whatever you need.
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
First, the class must implements the click listenener :
implements OnItemClickListener
Then set a listener to the ListView
yourList.setOnItemclickListener(this);
And finally, create the clic method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show();
}
you can take a look and download code here
Use setOnItemClickListener() api in your activity. Following is the sample.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<> parent, View view, int position, long id)
{
// your code here.
}
});
In Kotlin, add a listener to your listView as simple as java
your_listview.setOnItemClickListener { parent, view, position, id ->
Toast.makeText(this, position, Toast.LENGTH_SHORT).show()
}

Categories

Resources