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.....
Related
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!
I need to capture the event when the spinner don't change item. This is my code:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
This code works when I change item in spinner. But when I always click on the select Item how I can capture this event? Anyone can help me?
Its very simple.
Impl. the OnItemClickListener for the Spinner.
Docs: http://developer.android.com/reference/android/widget/Spinner.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
}
});
I have a ListView and I am trying to handle the click event on list of items via the following code
listView.setOnClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
// Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
}
But I am getting compile time error which is as following
The method setOnClickListener(View.OnClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new AdapterView.OnItemClickListener(){})
How can this error be resolved ?
Use setOnItemClickListener() to set a listener for item clicks. setOnClickListener() would register a regular View.OnClickListener on the ListView widget itself.
Replace setOnClickListener by setOnItemClickListener :-)
use setOnItemClickListener()
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String item = (String)adapter.getItemAtPosition(position);
}
});
Use setOnItemClickListener instead of setOnClickListener
adding onClickListener to views in the getView() method is using when
you have 2 or more views that should have their own onClickListener,so
for your is better onItemClickListener
i.e. rewrite your code as
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
// Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
}
For more info see
Which: ListView.setOnItemClickListener or TextView.setOnClickListener?
I have a listView vith some items.
I would like to get from my onClickListener the name (String) of the selected item.
I know how to get the selected position but how to find the String of this element?
Here is my on click listener:
journalNames.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
}});
My listView is populated with some query from the database.
Thank you.
What about,
journalNames.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String selectedFromList = (journalNames.getItemAtPosition(position).getString());
}});
YOu can find it either on view or on parent. In eclipse just type view. and see what methods you get after you type .(dot). I think this is the best.
parent.getAdapter().getItem(position);
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()
}