I have to remove the single item from list view in android while clicking long click. Please let me suitable code for removing a item in list view?
Here is the my code
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto - generated method stub
}
}
Use remove() method in your ArrayAdapter.
yourarrayAdapter.remove(yourarrayAdapter.getItem([POSITION]));
OR
yourarrayList.remove([POSITION]);
yourarrayAdapter.notifyDataSetChanged();
Try this in your click callback, to remove the view from its parent:
((ViewManager) parent).removeView(view);
Related
I have a ListView implementing onItemClickListener and on its first click, I am highlighting the list item and adding it to a arrayList (Which I am sending it to next activity). What I want is to remove the ListItem from the arraylist when I click on the listitem second time, That is when deselecting the list item. This is my code that I implemented for adding. Please guide how can I remove elements.
`listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present =listView.getItemAtPosition(position).toString();
presentStudent.add(present);
}
});`
You need to check if the list contains the object, add it if not and remove it if it's in.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present = listView.getItemAtPosition(position).toString();
if (presentStudent.contains(present)) {
presentStudent.remove(present);
} else {
presentStudent.add(present);
}
}
});
For example I have Custom Adapter List View, and my list item is a layout that contains different elements: textview, imageView and so on. How can I set the color of imageview of selected list item? Suppose I want to add this item in favourite and I want to change the color of star favourite for yellow color. Thank you)
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), PlaceName[position] + " in favourite",
Toast.LENGTH_SHORT).show();
//Do smth here, set the color of element on item, add to favourite and something else
return true;
}
});
Well, you have this line:
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
https://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html
According to the docs, view is the layout that you actually clicked, so you can get the child of that View using something like this:
ImageView favorite = view.findViewById(R.id.yourFavoriteImageView);
Note that if you scroll through your list, the layout might render again, and your change won't be visible again.
view.setBackgroundColor(Color.parseColor("#222222"));
guys what's up?
I'm still new to android and creating some testing app.
I have two Lists in my view. (Left and Right).
List Filled with values.
I trying to add selected list item value to other lists.
For example, If I clicked on an item in a left list. I wanna add selected item to Right list,
implemented OnItemClickListener into my code and OnItemClickListener onItemClick method is firing for both lists.
Any way to identify clicked list in an onItemClick method?
any suggestions are greatly appreciated.
After reading official API. Found a solution.
We can get the ID on the selected list.
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId() == R.id.leftList){
// It's a left list
}else{
// It's a Right list
}
}
I'm not sure if I understand your question, but if you are trying to identify which list was clicked, you can call getId on the view in onItemCLick:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (parent.getId()){
case R.id.left_list:
...
break;
case R.id.right_list:
...
break;
}
}
This is my code:
clientGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
gridClientChooseMode = !gridClientChooseMode;
clientAdapter.notifyDataSetChanged();
return false;
}
};)
and this is the adapter for the gridview:
class ClientListAdapter extends ArrayAdapter<ClientData> {
public ClientListAdapter(Context context, List<ClientData> list) {
super(context, R.layout.view_item, list);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (gridClientChooseMode) {
checkBox.setVisibility(View.VISIBLE);
}else{
checkBox.setVisibility(View.GONE);
}
}
}
checkBox will change its visibility to VISIBLE when gridClientChooseMode=true (when long click on item of the gridview).But after that I cannot turn off the check box because the OnItemLongClickListener didn't work anymore, I mean it works only at first time. Any help is highly appreaciate. Thanks in adavance.
#Elltz is right about the CheckBoxes stealing focus. In the layout root view for you grid items, you need the following to prevent the CheckBoxes from getting focus:
android:descendantFocusability="blocksDescendants"
whiles checkbox is visible it takes away the focus, so either you do put your onlongitemclick listener functions in your checkbox widget onlongclicklistener in your adapter, so it responds to itself and set itself to gone or invisible or you intercept touch events for the grid or listview, and handle all focus/touch events
I'm now implementing an application that contains an Activity includes ListView, when the user selects any item in the list view, the background & text color of this view are changed, So, i placed the code of this changes in the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) method ..but,this is not the problem.
The problem is, when i open the activity, i need to make an initial selection "before the user selects any thing"..so i made listView.setSelection(index);, but, unfortunately, this code doesn't invoke the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)..So, the view doesn't changed "background and text color".
Any solution for that..?!!!
Thanks,
try onItemSelected for selection
selecting a iem in list view does not fires onItemClick
Create a method in yout Adapter to set the selection (or do it in the constructor):
public myAdapter (COntext context, int initialSelectedPos){
setSelectedPos(initialSelectedPos);
}
public void setSelectedPos(int pos){
mSelectedPos = pos;
}
Then check in your getView is given pos is the same than mSelectedPos.
#Override
public View getView(..., int pos){
/*convertView stuff*/
if (pos == mSelectedPos){
//Put the background as it is selected
}else{
//...
}
return view;
}
In your OnItemClick method from your OnItemClickListener call the setSelectedPos method of your Adapter.
You will solve the issue you commented and also when your selected view is no longer visible on screen and comes back to screen, will still be marked as selected ( I am pretty sure it was appearing with the original background).
after setting selection
listView.setSelection(index);
and then after call the
listView.getAdapter().notifyDatasetChanged();
i think this will solve your problem