How to delete longpressed item in a grid viw - android

I am having a grid view ,I am passing data which I want to display in grid view to the gridview adaptor class.Now I want to remove the item when an item is long pressed.
I want to delete in onItemLongClickListener .

What you need to do is this:
Create a custom adapter for your GridView. In that adapter, provide a method for removing an item from the list of items it maintains, for example 'void removeItem(int position)`
Call setOnItemLongClickListener to your grid. In this method, you get the position of the item that the long click occurred on. From this method, call the method to remove the item with the position you just received.
Notify the GridView that the data has changed using notifyDataSetchanged method. If you want the GridView UI to update immediately, you need to use Handler for this request to make sure it happens on the UI thread.

if mThumbIdsList is the integer array of all the gridview items ids then you can try this code. this may help you.
final ImageAdapter adapter = new ImageAdapter(this);
gridview.setAdapter(adapter);
gridview.onItemLongClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.mThumbIdsList.remove(position);
adapter.notifyDataSetChanged();
}
});

Related

How to have a checkbox inside of a listview, to have a listener outside adapter activity?

So, I have menu items being displayed in listview, along with checkboxes, for the user to select which he likes. And I have an overhead cart icon which keeps track of no of checked items, by a function that is written in the adapter view.
My problem:
When I have implemented the cart increment function inside of OnItemClick listener, and then when I check a few items, it doesn't get updated unless I make a click outside of checkbox (anywhere else in the listview).
I have tried disabling descendant focusability and modifying focusability as per other SO answers. They haven't solved my issue.
My calling activity
adapter = new MenuAdapter(CreateOrder.this,itemslist);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selecteditems = adapter.getSelectedList();
Log.e("Selected Items",selecteditems.toString());
size = selecteditems.size();
qo.setText(String.valueOf(size));
Log.e("No of main items ",String.valueOf(size));
}
});
checkBoxState = new boolean[linkedList.size()];
You have that in getView(). So for every call of getView() you throw away the former.
In getView() you use checkBoxState[position] which then of course is undefined or null.
So take it out and put it in the constructor of your adapter and initialise it as you do with that int array.
By the way: why did not you tell that your checkboxes did not hold state during scrolling?

How can i set click listener on individual item in a listitem?

I have a listview binded to an arrayadapter of strings... How do i set different clicklistener on each item in the list view
Register an item click listener on your ListView with http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
In your AdapterView.OnItemClickListener#onItemClick you will get the view and an id.
You can use the id if you have a given order in your arraylist, or you could use View#findViewById and get the content of the view to figure out which item it is.
That means, you would not set a listener per item, rather you would have one listener for all items, and then do different things based on which item it was.
I think you could use it like this.
adapter = new ArrayAdapter(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textView, new ArrayList<String>());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what ever you want to give on item click
}
});
here by checking the position inside onItemClick you can assign specific tasks.

How to set items in adapter onclicklistener temporary to null? And later set it back?

I have a listview with items from the adapter. And when I try and retrieve new information, I'm putting a RelativeLayout with loading images and stuff on top of it.
The problem is, is that my listview and adapter items are still clickable. They can only be clickable after the loading is done. When the loading is done I am triggering (notification) a setVisibility(View.gone) on that LoadingLayout.
I tried:
listview.setFocusable(false);
listview.setClickable(false);
listview.setEnabled(false);
But it is not working
I guess the:
row.setOnClickListener(this);
in the getview method of the adapter is still on?
How do I make it not clickable? Do I get a list of views or something and make them unclickable, but then again. Only the views that are onscreen should be not clickable for that moment? Or is there some other clever method? (I thought the overlaying relativelayout would be clever ... guess not)
Declare a variable in class where you are calling setAdapter to list view as follows
OnItemClickListener myAdapterItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO your row onclick actions
}
};
Before loading all the stuff, you can set
listview.setOnItemClickListener(null);
After loading it,
listview.setOnItemClickListener(myAdapterItemClickListener);

HOW TO refresh the content of a single row in my ListView using OnItemClick

I have a ListView with a CheckBox.
What I am trying to implement is an onItemClick (in my activity) that check/select my CheckBox if I click on the row, as you can see below:
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyItem row = ListofRows.get(position);
row.setSelected(true);
myAdapter.notifyDataSetChanged();
}
When I click in the row, the ListView is refreshed because of the method notifyDataSetChanged()
THE PROBLEM IS: this method refreshes the whole ListView and as a result, the ScroolBar goes to the beginning (the user is driven to the header of the list) even if the user is at the bottom of the list.
WHAT I NEED IS: Refresh just this single clicked row so the user won't be driven to the beginning of the list.
Are there any solutions to this problem?
use the selected index
View v = getListView.getChildAt(index);
Now update the view with the new values.

how to display the selected items from a listview in the next listview

I have a custom listview. I have to select some items from this listview and display it in the next layout listview when a button click event is clicked.
lv5=(ListView)findViewById(R.id.ListView05);
lv5.setAdapter(new ArrayAdapter<String>(this,R.layout.productselecttext,R.id.pstext,arr));
lv5.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(view.findViewById(R.id.oi).getVisibility()==ImageView.VISIBLE){
ImageView icon = (ImageView)view.findViewById(R.id.oi);
icon.setImageResource(R.drawable.vi);
}
}
});
In this way I have selected using an imageview. How can I get only the selected items and display in another listview?
It all depends on what you call a selected item. We need some more code to understand how you "selected" items in the first listview.
But generally speaking, all you have to do is to build a new Adapter for the second list view that just lists the selected item. You could even recylcle the first adapter and the first view, just eliminating the items that are not selected from the adapter's list and calling
notifyDatasetChanged()
on your first adapter to update your first list.
Regards,
stéphane

Categories

Resources