LongItemClickListener on Listview android - 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
}});

Related

about setOnItemClickListener(new OnItemClickListener()

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

How I can capture the event when the spinner don't change item

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

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

show button on longpress the grid item in android gridview

Here i have using following code. Now my grid showing the download button on long press.If i long press another item it shows the button.But the problem is that previous button is there.I want to hide the old .(Hide the old download button on new Long press on another grid item).
How can i do ? Please check it and give me a idea to implement this part ?
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
}}
Thanx in advance.
You can remember previous long-clicked item:
private View previousItem;
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
if (previousItem != null) {
downloadImg=(ImageView) previousItem.findViewById(R.id.download);
shareImg=(ImageView) previousItem.findViewById(R.id.share);
downloadImg.setVisibility(View.GONE);
shareImg.setVisibility(View.GONE);
}
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
previousItem = view;
}
}
This may help for same activity.
grid1.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg1=(ImageView) view.findViewById(R.id.download1);
shareImg1=(ImageView) view.findViewById(R.id.share1);
downloadImg1.setVisibility(View.VISIBLE);
shareImg1.setVisibility(View.VISIBLE);
downloadImg2=(ImageView) view.findViewById(R.id.download2);
shareImg2=(ImageView) view.findViewById(R.id.share2);
downloadImg2.setVisibility(View.GONE);
shareImg2.setVisibility(View.GONE);
}}
grid2.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg1=(ImageView) view.findViewById(R.id.download1);
shareImg1=(ImageView) view.findViewById(R.id.share1);
downloadImg1.setVisibility(View.GONE);
shareImg1.setVisibility(View.GONE);
downloadImg2=(ImageView) view.findViewById(R.id.download2);
shareImg2=(ImageView) view.findViewById(R.id.share2);
downloadImg2.setVisibility(View.VISIBLE);
shareImg2.setVisibility(View.VISIBLE);
}}
If you using custom adapter. You can create in your CustomAdapter method setButtonVisible(int position). And in the getView check this setted position and show or hide Button in view. Your method onClick kwill be something like this:
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
adapter.setButtonVisible(position);
adapter.notifyDataSetChanged();
}
You can:
Make a field or final variable with last button. in onItemLongClick check if it's not null and then make it invisible. On the end of onItemLongClick fill in the field variable with current button.
In your case (with provided code I suggest that downloading and shareImg are final variables common for all views that could be clicked):
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
if (downloading != null) {
downloadImg.setVisibility(View.INVISIBLE);
}
if (shareImg != null) {
shareImg.setVisibility(View.INVISIBLE);
}
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
}}
Make a button disappear after some time. But in this case the variable should be the method scope. E.g. :
final ImageView downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
downloadImg.postDelayed(new Runnable(){
#Override
public void run() {
downloadImg.setVisibility(View.INVISIBLE);
}
}, 1000);

I have a listview, on click appears a context menu. Any way to know the context menu over which listview item appeared?

Have a listview, on click appears a context menu. Any way to know the context menu over which listview item appeared?
int listViewPosition;
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
listViewPosition = position;
}
});
Then you can use listViewPosition in
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
Log.d("ListView clicked Item", ""+listViewPosition);
// rest of code
}
You get the selected item from the listview:
lv.getSelectedItem()
You can use:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
Where position is the position of the item that was clicked in the ListView.

Categories

Resources