about setOnItemClickListener(new OnItemClickListener() - android

My database data is stored in list view, when I click on one of the list view, how can I get each item according to parameter “position”?
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});

You can do it by-:
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
break;
case 1:
break;
}
}
});
But this will be complex when we have to many items in listview.
Thanks

Try this...
this sample will help you...
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
for more

If you set your database data in listview like below
ArrayAdapter<Object> mDatabaseData = new ArrayAdapter<Object>(this,android.R.layout.simple_expandable_list_item_2, array);
lv.setAdapter(mDatabaseData);
then you can get each item according to parameter “position” as below
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, get data for selected position
Object selectedItem=mDatabaseData.get(position);
}
});

Related

error while trying to set onItemClickListener in ListView up

error screenshot
I am trying to make clickable items in ListView, and recivieng this kind of error.
ListView chHE = (ListView) findViewById(R.id.lvHE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item, cities);
chHE.setAdapter(adapter);
chHE.getOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long l) {
if (position == 0) {
toast.show();
}
}
});
Error:(52, 13) error: method getOnItemClickListener in class AdapterView
cannot be applied to given types;
required: no arguments
found:
reason: actual and formal argument lists differ in length
where T is a type-variable:
T extends Adapter declared in class AdapterView
chHE.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if(position==0){
toast.show();
}
}
});
maybe you have write in a mastake?change get=>set
you should remplace getOnItemClickListener to setOnItemClickListener
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
Try doing this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//doSomeThing
}
});

LongItemClickListener on Listview android

I have a question, Can we apply OnItemClickLisyener and OnLongItemClickListener on the same ListView?
I want to delete items of listview on long item click, listview is already handling ItemClickListener to view files.
What should I do now?
Can someOne explain how to do this?
Here is example
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//do your stuff
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
//Do your stuff
}});

How do I add Click Listener to ListView?

I have a List View which is populated with countries I want to be able to click a an item and have it go to another activity in which i play an audio.
my list View:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources()
.getStringArray(R.array.countries)));
}
}
How do I go about adding a onClick just like how you would make one for a button??
ListView listView = getListView(); // hear bind your listview
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources().getStringArray(R.array.countries)));
listView.setAdapter(ListAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
}
});
}
}
Either use getListView() to access built in list view, and operate on that:
getListView().setOnItemClickListener();
Or, override onListItemClicked() in ListActivity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//--do stuff--
}
override onItemClick() method
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}
Since your activity extends ListActivity
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(launchActivity);
}
});
U can use context.getlistview() to get the listview. and add setOnItemClickListener to the listview like this.
context.getlistview().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do whatever you want here
}
});
To make it handy just implement OnItemClickListener to your class
Add this in onCreate
ListView listView = getListView();
listView.setOnItemClickListener(this);
then override onItemClick like this
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
// your code
}
You have to use the setOnItemClickListener method.
Here is the code
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// do your code here
}
});

getting item's name when clicking a ListView

Is there any way I can capture a name of an item clicked in a list view when using "onItemLongClickListiner" ? I know I can capture position etc but I need a name
I suppose that you ListView is filled up with String object:
public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id) {
String name = (String) parent.getItemAtPosition(position);
}
AdapterView.getItemAtPosition(position) gets the data associated with the specified position in the list.
You can use lv.getItemAtPosition(position) to get the text based on the index position
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3)
{
String name = arg0.getItemAtPosition(position).toString()
}
});
For more info
getItemAtPosition
Try this..
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//inflated row layout textview
TextView tagText = (TextView) view.findViewById(R.id.txt_text);
String tag = tagText.getText().toString();
Toast.makeText(getApplicationContext(),
"Element Name " + tag + " Clicked", Toast.LENGTH_SHORT).show();
}
});
It has worked for me.
listtview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
String selected = (String) adapterView.getItemAtPosition(i);
Toast.makeText(getApplicationContext(),"This is"+selected,Toast.LENGTH_LONG).show();
}
});

Check selected item of ListView

I have a ListView and need to check, which item has been clicked.
The way I have it, it only shows my the selected item when I use the scrollwheel of my mouse (when testing it in the Eclipse emulator). When I test it on a real mobile device, it doesn't react at all.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
myFunction(listView.getSelectedItem());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use setOnItemClickListener instead of setOnItemSelectedListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get value for listItems using position
String str =listItems[position];
myFunction(str);
}
});
I think better you need to use on onListItemClick listener
public void onListItemClick(ListView parent, View v, int position, long id)
{
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected" + items[position],30).show();
}

Categories

Resources