I have approx 150 items in my custom listview. I am trying to implement the following:
list.setOnItemClickListener(new OnItemClickListener()
{
1. I get position of the item
2. Get the data from an ArrayList
3. Add the data to database.
This part is working fine.
But if I want to remove a particular item from the database by clicking on the item I am not able to achieve this part?
Reason because I am not sure if the item is clicked or not? I have this problem mainly when I want to implement search in the listView and add the selected item to database and remove selected item from database.
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
if (notfromsearch == true)
{
String profile_ID = profileList.get(position).get(KEY_ID);
String profile_DISPLAY_NAME = profileList.get(position).get(KEY_DISPLAY_NAME);
//add to database now
}
elseif (notfromsearch == false)
{
String SearchResult_profile_ID = searchProfileResults.get(position).get(KEY_ID);
String SearchRResult_profile_DISPLAY_NAME = searchProfileResults.get(position).get(KEY_DISPLAY_NAME);
//add to database now
}
}
});
But how to implement remove from database? I am not able to differentiate if the item is clicked before or not to remove from database.
Can somebody help me out with this?
Thanks!
On your list view, use
setOnItemClickListener
and you can do what you want next .
use context menu for deleting items from the database
on long press display delete/edit option and perform further operation as needed.
check here for context menu sample
EDIT
You can also put button/image in the listview to delete item and then implement your delete operation in the click method.
For Checkbox selection
model class
class CustomClass
{
boolean checked;
//other fields
}
in your custom adapter class
ArrayList<CustomClass> data = new ArrayList<CustomClass>();
and in getView() method
checkboxItem.setChecked(data.get(position).getChecked());
and don't forget to change the checked boolean value on checkbox change listener..
Related
how to store only selected item list data in recycler view from other activity list.
i used this code -- successfully select data but don't know how to add only selected data items in new activity recycler view -
i used this code snippet ite working fine-
//
https://en.proft.me/2018/03/3/multi-selection-recyclerview/
StringBuilder stringBuilder = new StringBuilder();
for (ExcercisesSelectedModel.DataBean data : getList()) {
if (selectedIds.contains(data.getId()))
stringBuilder.append("\n").append(data.getName());
Change
public interface OnClickAction {
public void onClickAction();
}
to
public interface OnClickAction {
public void onClickAction(Item item);
}
In adapter class, on item click
receiver.onClickAction(item);
In Activity class
private List<Item> selectedItem = new ArrayList()
public void onClickAction(Item item) {
selectedItem.add(item);
}
Now use this selected item list in new activity.
Edit: Don't forget to use or rename the interface to Callback, something along the lines of ActivityCallback or OnClickCallback.
This is for the sake of naming conventions.
A simple flag isSelected in parent list will save your day.
So whenever user select/unselect the item just change the value of isSelected flag to true or false.
Now you have a final list from which you can easily identify selected items, simple store in separated list named selectedItemList.
At last use the selectedItemList and fill your Recycleview. Hope it make sense.
I am populating a listview with strings from an ArrayList.
When a listview item is clicked i would like to change the background colour to green. <- Issue number one because I cannot change the item which is clicked.
After an item is clicked, I am adding its index to the list of items the user has selected, when the listview is first loaded I need it to set the background colour of all the listview items which have been selected to green too. <- Issue number 2 - I have been trying to do this with a for loop but do not know how to refer to a specific item in the listview to set the background colour!
Essentially, i think if someone can help me in how to change the colour of a selected listview item, i should be able to do the same thing but in a loop for all the userFoodPref which are saved?
animalsNameList = new ArrayList<String>();
userFoodPref = new ArrayList<Integer>();
getUserSelection();
getAnimalNames();
// Create The Adapter with passing ArrayList as 3rd parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, animalsNameList);
// Set The Adapter
animalList.setAdapter(arrayAdapter);
// register onClickListener to handle click events on each item
animalList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
// argument position gives the index of item which is clicked
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(userFoodPref.contains(i)
){
userFoodPref.remove(i);}
else {
userFoodPref.add(i);
View item = animalList.getSelectedView();
item.setBackgroundColor(0x0000FF00);
}
String selectedAnimal=animalsNameList.get(i);
Toast.makeText(getApplicationContext(), "Animal Selected : "+selectedAnimal, Toast.LENGTH_LONG).show();
}
});
}
If I understand well, the problem is that when you set the backround of the item and then by example scroll the list and comeback to the previous position, it doens't remember that the backround is green for this specifix item.
I have faced this problem and to solve it easily :
Create a list a string for your name and a boolean (true = green, false = not green) and create an adapter for it and simply add
if (list.get(position).getBoolean) {
Currentitem.setBackgroundColor(0x0000FF00)}
And when you click on a item simply set the boolean of the item position to true and call notifydatasetchanged()
I have a list view with multiple items, where i need to select and deselect the list items, and also delete the selected items.
So i have looked into the example in the below link but its for android:minSdkVersion="11"
but i am working on minSdkVersion="10".
Link : http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
And yes we can do with checked text view, check box and radio button, but the requirement is like that i cannot use that.
Is there any other way that we can acheive this?
Make custom list adapter and get the click of that each view and maintain flag in adapter. If flag is true that means item selected otherwise item deselected, according to that you can change item view like disable that particular item or show some check box.
What I did was created an ArrayList that stores all the position of selected items, and toggle the background colors on clicks.
In my Adapter I define:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
With the following method :
public void toggleSelected(Integer position)
{
if(selectedIds.contains(position))
{
selectedIds.remove(position);
}
else
{
selectedIds.add(position);
}
}
which addes\removes items from the ArrayList
In my getView method :
if (selectedIds.contains(position)) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
else
{
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(Color.parseColor("#000000"));
}
This checks if the position is storred in the ArrayList. if it does, paint it as selected. if not, the opposite.
all is left is the OnItemClick listener, i added :
((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
When YourAdapter is the adapter of your ListView
Hope this helps anyone, as it's a generic answer :)
Thanks to eric.itzhak here : How to change background color of selected items in ListView?
I am working with a ListView trying to add/delete items. The addition bit was fairly easy, the removing though is proving to be trickier.
I was thinking to use a multiple choice list, but to start with something simpler I chose a single choice mode just to test it out.
I have an array of strings containing the items, an array adapter to notify when Data has changed.
expenseAdapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1,
expenseList);
myListView.setAdapter(expenseAdapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View item, int position,
long index) {
((ListView)parent).setItemChecked(position, true);
item.setSelected(true);
}
});
I have also added a listener for the item onClick Event. Visually the item gets selected/deselected the issue is when I click the button which triggers the deletion of the item the selected index in the list is always -1 although the item appears to be selected.
Delete button with onClick event
public boolean doDelete(View view)
{
ListView myListView= (ListView)findViewById(R.id.list);
String s=(String)myListView.getSelectedItem();
expenseList.remove(s);
expenseAdapter.notifyDataSetChanged();
return true;
}
Any ideas what is happening or what I'm doing wrong?
use this
xmlfile : https://www.dropbox.com/s/eky9zb275mgt4py/activity_list__addand_delete.xml
javaFile : https://www.dropbox.com/s/idqyyosbutgqqbs/List_AddandDelete.java
your selection ( focus ) is removed when ever you will shift your focus to button .
I have seen your code and understand that problem, you are getting -1 index when you are removing item from List because that item is not existing in that list so change your code please try this.
I changed getSelectedItem() to getSelectedItemId() here it will return selected item id instead of Item so you can remove that item from List based on item id which will be the Index of item in List.
public boolean doDelete(View view)
{
ListView myListView= (ListView)findViewById(R.id.list);
long id = myListView.getSelectedItemId();
expenseList.remove(id);
expenseAdapter.notifyDataSetChanged();
return true;
}
Hope it will help you.
I am using a custom list view in my application. In custom list view in all rows I have placed an image button. If I click that image button, I have to delete clicked button row in the custom list view. Can anybody tell me how to do this? The below coding is not working:
imgcross=(ImageView)v.findViewById(R.id.imgcross);
imgcross.setId(position);
if(v.getId()==R.id.imgcross)
{
Log.d("image id is",Integer.toString(imgcross.getId()));
myScheduleList.removeViewAt(imgcross.getId());
Toast.makeText(MyScheduleDay0RequestedMeeting.this, "Cross Button is Clicked", Toast.LENGTH_LONG).show();
}
if (v.getId()==R.id.imgcross)
{ //Integer index=(Integer)imgcross.getTag();
//Log.d("image id is",Integer.toString(index));
int index=imgcross.getId(); (imgcross.getId());
MyScheduleBean.listName.remove(index);
MyScheduleBean.dateValue.remove(index);
MyScheduleBean.dateValue.remove(index);
CAdapter = new CustomAdapter(this,MyScheduleBean.listName,MyScheduleBean.dateValue,MyScheduleBean.meeting,R.layout.myschedule_day0_requestedmeetingrow,to);
myScheduleList.setAdapter(CAdapter);
}
Thanks
You need to delete the data from the underlying Adapter. Done properly, this will automatically update the ListView. Otherwise, also call notifyDataSetChanged() on the Adapter, and that will cause the ListView to update.
I've been searching too long for this answer - thank you very much.
After deleting the record from the database with:
DBRecordOperation.deleteRecord(dbRecord);
simply remove the record from the ListView with:
adapter.remove(dbRecord);
I'm doing this on a long click in the ListView. Full code for onContextItemSelected is:
#Override
public boolean onContextItemSelected(MenuItem item){
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
DBRecord dbRecord = (DBRecord) getListAdapter().getItem(info.position);
int dbRecordId = dbRecord.getId();
DBRecordOperation.deleteRecord(dbRecord);
adapter.remove(dbRecord);
return true;
}