Android listview OnItemClickListener getting -1 - android

when i am clicking on listview item i am getting item position as -1. This is my code.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int index = listView.getSelectedItemPosition();
System.out.println("benbenarji:"+index);
}
});

Value -1 is for INVALID_POSITION, this means there is no selection on the list. You can use position from on onItemClick to learn which item was clicked.

Related

Is there a way to store the ListView item number in an int in Android?

I imported a text file into a ListView in android.
If I click an item in listView, it's line number should be stored in an int.
For example (this is a ListView (text file)):
Corn (1)
Apple (2)
Melon (3)
If a line is clicked for example Melon, it's line number (3) should be stored in an int.
The code:
// returns index to -1
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
boolean co = true;
int lineNumber = list.getSelectedItemPosition();
Toast.makeText(MainActivity.this, String.valueOf(lineNumber), Toast.LENGTH_SHORT).show();
}
});
Is that possible?
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(MainActivity.this,"Item No:"+position,Toast.LENGTH_SHORT).show();
}
});
Note Item index start with 0.
Method like this may also help you
listView.getSelectedItemPosition()
listView.getCheckedItemPosition()
Note: This above two line is not meant for item click.
First of all, ListView index starts from zero, so the index should be like this
Corn (0)
Apple (1)
Melon (2)
And easily you can get the position of the selected item as it's declared as a parameter in
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
And also you can get the name of the selected item by this line in onItemClick()
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// TODO: Implement this method
String title = parent.getItemAtPosition(position);
}
I just used int position included in the method:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();

how can i get spinner item's id?

I have a spinner named spinnerArray
hot can I get the selected item's ID of spinner by integer data type after click a button?
(when I have selected First one of spinner, I want to get integer 0,Second one,integer 1)
Use
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
// position is what you want i think
}
#Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
And to get the position when clicked on other views use spinner.getSelectedItemPosition();
You can do this in your button's onClick method:
mySpinner.getSelectedItemPosition() + 1;
getSelectedItemPosition() gives you the item position starting at 0, so you need to add 1.
i think you are looking for position in spinner
code sample
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Are you looking to see when the items on the spinner have been clicked? Then OnItemSelectedListener will allow you to override onItemClick(AdapterView parent, View view, int position, long id).

How I can delete the item from list view

I have a listview and a button in my layout file.
I'm adding items to listview on click of that button.
I need a way that make my item in the list disappear when I click the item.
set up an onitemclicklistener on you list view, remove the item when it is onclick and then refresh the list
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* remove the item at position */
notifyDataSetChanged();
}
});

Android Custom ListView - How to prevent auto Refresh(update) of ListView Adapter on Scrolling

I want to stop listview automatic update(refresh) thing on LIstView scrolling..
OR
How can i assign a new adapter to the same ListView on scrolling???
Please help!!
try to set number as a Tag view.setTag(number) and get it in OnItemClickListener :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if (position > 0 && position < mProducts.alternatives.size() + 1){
int number = (Integer)view.getTag();
}
}
});

Android ListView and OnClickListener: How to get the selected item

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

Categories

Resources