Now i am going on with the Listview in that i should delete the particular row which i selected here i select the row with the help of button in each row i have a button if i click the 2nd row button 2nd row should be deleted.
public class Adapter extends ArrayAdapter<Data> {
private final View.OnClickListener deleteButton = new View.OnClickListener() {
#Override
public void onClick(View v) {
selected = (Data)v.getTag();
}}
public class DetailsFragment extends Fragment implements Adapter
.Listener {
#Override
public void Deleted(Data list) {
int itemCount = adapter.getPosition(list);
for(int i=0;i<adapter.getCount();i++) {
Data present= adapter.getItem(i);
if(itemCount==i) {
adapter.remove(present);
}
}
}
other approach:
#Override
public void Deleted(Data list) {
for(int i=0;i<adapter.getCount();i++) {
Data present= adapter.getItem(i);
if(Adapter.selected ==present) {
adapter.remove(present);
}
}
}
Tried with many link few below:
http://wptrafficanalyzer.in/blog/deleting-selected-items-from-listview-in-android/
http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
My problem was when ever i try to delete the selected row it deletes from bottom of the list.
Here my position of the data ,id everything is assigned correct but it default removes from bottom
How can i solve this is there any other apporach to solve this problem.
Basicly there are two steps that you need to follow;
Delete the row data from the list.
There supposed to be a list which contains the data that you show in your listview. First you need to delete the data from this list.
Notify the adapter.
Your adapter is connected with the data list. After you implement changes on your data list, you need to inform your adapter about these changes which means;
adapter.notifydatasetchanged()
after you call this method adapter will reload the data to listview without the deleted items.
See here according to one of the tutorials you posted here is how the data is being deleted. After deleting the date you have to notify the adapter just like the tutorial and you are not doing that. and also take a look at you for loop
/** Defining a click event listener for the button "Delete" */
OnClickListener listenerDel = new OnClickListener() {
#Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
};
Hope it helps.... :)
Related
Currently: i have a recycler view where it has delete button and an edit text for each item.
Each item in the recycler view contains edit text that the user can input.
The scenario is i have 4 item in the list resulting 4 edit text.
When i try to fill up edit Text in the item(n) and tried to delete
the item(n) and add again. The edit Text value in the edit text remains.
How can i clear the value of the edit text?
Here is my current code when i try to delete a specific item in the list:
mHeaderText.remove(pos);
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i = 1 ; mHeaderText.size() >= i ; i++) {
temp.add(i);
Log.d("ADDDING","ADDDING"+i);
}
mHeaderText.clear();
mHeaderText.addAll(temp);
notifyDataSetChanged();
Try this code ..
In adapter class in onBindView method when you perform click event then set edittext value as null.
edittext.setText("");
and if your edittext box in activity layout then make interface into adapter class and handling click event like this way...
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(String data); //pass any data to shared it.
}
after that..
#Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
final String data = mStringList.get(position);
holder.textView.setText(data);
holder.firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClick.getPosition(data); // shared data.
}
});
}
after that in activity adapter bind into recycler view it means adapter not null then called below line..
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
#Override
public void getPosition(String data) {
// hear perform any operation.
editTextbox.setText("");
adpater.notifyDataSetChanged();
}
});
Use
edittext.setText("");
to clear text where edittext is reference to EditText. Since you haven;t posted complete code so i can guide to this only. If you provide more code, then I can help you more.
One thing is sure. You will need to use
edittext.setText("") to clear the value.
But you need to be careful. You need to keep this as the first line in your delete row function, i.e. before you are removing that edittext item from your list.
Here is my code: I have placed the "remove row" function inside onBindViewHolder() method of Recyclerview Adapter.
holder.delImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// You need to place it in the start
holder.editText.setText("");
// Remove the item on remove/button click
myArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, myArrayList.size());
Toast.makeText(context, "Removed : " + position +" item", Toast.LENGTH_SHORT).show();
}
});
I have a recyclerView and have implemented onLongClick listener on the items. The Adapter is in the same class as the Activity.
I created a set: public Set<Integer> multiplePositions as instance variable of the Activity class and am initialising it in onCreate() method as multiplePositions = new TreeSet<>().Coming to my Adapter class in the onBindViewHolder method I created a click listener as follows:
holder.textCardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Adapter.this.onLongClick(holder, position);
return true;
}
});
holder.textCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
As you can see I am calling the method onLongClick. My onLongClick method looks like this:
public void onLongClick(ViewHolder holder, int position) {
multiplePositions.add(position);
for(Integer i : multiplePositions) {
Log.e("set", ""+i);
}
holder.textCardView.setBackgroundResource(R.color.long_press);
}
Here, whenever an item clicked I am adding the position in the Set but I don't know how do I iterate through this set and set the background color of all the item at the positions in the set.
I am aware that this can be achieved by creating a boolean variable isSelected in the model class but if I go with this method then other functionalities of my code won't work.
My main concern is if I scroll the recyclerView up or down then color of the selected positions should not disappear. The unselection part will be done in setOnClickListener().
Use SparseBoolean array to store the positions of the items clicked in the reyclerview.Check the answer in this link
https://stackoverflow.com/questions/46336645/hilighted-selected-items-colour-changes-when-not-in-view-in-recyclerview/46336844#46336844
Check here the purpose of using SparseBoolean array
https://stackoverflow.com/questions/18822708/what-is-the-clear-purpose-of-sparsebooleanarray-i-referred-official-android
EDIT:
Use SparseBooleanArray to keep track of selected items in recycler view adapter
Initialize the SparseBooleanArray as private memeber variable
private SparseBooleanArray mClickedItems=new SparseBooleanArray();
Inside your click function while clicking textCardView make the item position as true.
holder.textCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mClickedItems.put(position,true);
notifyDataSetChanged();
}
});
Then in the onBindViewHolder check if the position is already selected or not like this
if(mClickedItems.get(position)==true){
//Show selected color
holder.textCardView.setBackgroundResource(ContextCompat.getColor(holder.textCardView.getContext(),R.drawable.selected));
}else {
//show unselected color
holder.textCardView.setBackgroundResource(ContextCompat.getColor(holder.textCardView.getContext(),R.drawable.unselected));
}
my RecyclerView contains Text and Delete Button when delete is clicked specific row will be deleted from ArrayList
pending.remove(p);// pending is arraylist
after this updating RecyclerView by calling this function which is inside Adapter
swap(pending);
public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) {
if (pendingList != null) {
pendingList.clear();
pendingList.addAll(newList);
} else {
pendingList = newList;
}
notifyDataSetChanged();
}
but the problem is that correct item deleted successfully from ArrayList pending but on RecyclerView wrong update is displayed.
if i delete second item from array list pending but in RecyclerView updated
deleted item is last
when activity is reloaded then values in RecyclerView are showing correctly
searched lot of about it but did not found any way to solve
Instaead of using swap() to delete the item from your array list and adding them back into diffrent array list, I will suggest that you use notifyItemRemoved in your adapter. So, your adapter will look like below.
RecyclerViewAdapter.java :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DownloadingActivity.scheduleListType> p = pendingList.get(position);
//....
//....
holder.deleteBnt.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
pendingList.remove(p);
notifyItemRemoved(position)
}
})
}
I have implemented custom list view with two rows (name and number) and it is checkable.
The list view has multiple select option.
Whenever user searches for a name, cursor will returned the new list of items. I can't keep track for items which has been selected earlier once adapter gets changed with the new cursor items.
For example user searches for name "Jo" it returns 10 items, in which i have selected 2 rows. Once i remove the search, the cursor and adapter gets changed. I am not able to mark the items checked.
I want to override default checkable items based on position( have to write own which has to make items checkable based on _id(contact id))
( I tried overriding onFinishInflate method. But it didn't help).
Any help appreciated.
Thanks in advance.
what you need is an object to have your check-box data persist your adapter and listview. A hashmap of boolean arrays should suffice.
private HashMap<String, boolean[]> contactMap;
I'd imagine that you could load this in some database method or something and you could have the person's name, like "Jo", as an identifier if need be. the array indexes would correspond to the checkboxes in each listview row as they appear. Then in your adapter which i'd imagine is a CursorAdapter, you could have the following:
private boolean[] contactObj;
public void setContactObj(boolean[] contactObj) {
this.contactObj = contactObj;
notifyDataSetChanged();
}
public boolean[] getContactObj() {
return contactObj;
}
#Override
public void bindView(View view, Context context, Cursor c) {
final int position = c.getPosition();
final CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
cb.setChecked(contactObj[position]);
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isChecked()) {
contactObj[position] = true;
} else {
contactObj[position] = false ;
}
}
});
}
Basically, you have an adapter that only has capacity for one boolean[] which is able to adjust the checkboxes in your listview, modify as the boolean[] as the checkbox is being clicked and then return it in the event that you still need it.
What I have: I have a ListView with custom rows, having a CheckBox & two TextViews in each row. I have a button for "Select All".
What i want: I want that when I click the button, all the CheckBox in ListView get checked/unchecked.
What is the problem: In OnClick of the "Select All" button. i am doing this:
public void OnClickSelectAllButton(View view)
{
ListView l = getListView();
int count = l.getCount();
for(int i=0; i<count; ++i)
{
ViewGroup row = (ViewGroup)l.getChildAt(i);
CheckBox check = (CheckBox) row.findViewById(R.id.checkBoxID);
check.setChecked(true); // true for select all and false for unselect all etc..
}
}
Here l.getChildAt(i) is giving me the visible items only. And when the index goes out of visible items, the problem occurs. I want to check all the CheckBox in List, not just the visible ones.
It will occur, because ListView adapter reuses views. The way you are trying to do is incorrect. I don't think you ever should access listview rows through listview children.
Introduce a variable in your activity, that will hold the current state (boolean checkAll). When the user presses the button, it must set "checkAll" to true, and call notifyDataSetChanged() (for arrayadapter), or requery() (for cursoradapter) on your ListView's adapter. In adapter's getView() method introduce a check for this flag, so if (checkAll) {check the check box}
have you looked this Correct way to check all checkboxes in ListView?
int count = getListAdapter().getCount();
I think you should run this long-running task off the UI thread.
When you click button in OnClickListener:
new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < list.getAdapter().getCount(); i++) {
final int position = i;
mHandler.post(new Runnable() {
#Override
public void run() {
list.setItemChecked(pos, true);
}
});
}
}
}).start();
and in onCreate() :
this.mHandler = new Handler();
Each item in list view should be Checkable like CheckableRelativeLayout that implements Checkable interface.