Reverse onItemClick event - java

I have a button that fades in another button and also deletes items selected on a ArrayList and ListView. If delete a single item and go on to browse the web it recollects the urls and will display them in the ListView. But if I select it again(single item) it will delete it with no need to press the Delete button. I would like it that you can only delete if you press the button. I cant word it correctly but I believe the closest word is Reverse or Undo or maybe even Cancel. I am trying to figure it out using a variable called int click. And if(click == 1) then undo/reverse/cancel onItemClick.
MainActivity.Java
public void onHistoryClose(View view){
FadeOut = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out);
FadeIn = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
FadeOut.setDuration(350);
b_HistoryActivity.startAnimation(FadeOut);
b_HistoryDeleteAll.startAnimation(FadeOut);
b_HistoryActivity.setVisibility(View.GONE);
b_HistoryList.setVisibility(View.GONE);
if(b_HistoryActivity.getVisibility() == View.GONE){
w_Toolbar.startAnimation(FadeIn);
bottomNav.setVisibility(View.GONE);
}
if(click == 1){
}
}
public void onHistoryDelete(View view){
click = 1;
Toast delete = Toast.makeText(MainActivity.this, "Select to Delete",Toast.LENGTH_SHORT);
delete.show();
b_HistoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String item = HistoryAdapter.getItem(position);
HistoryAdapter.deleteItem(item);
HistoryAdapter.notifyDataSetChanged();
view.getFocusables(position);
view.setSelected(true);
}
});
b_HistoryDeleteAll.setVisibility(View.VISIBLE);
FadeIn = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
b_HistoryDeleteAll.startAnimation(FadeIn);
}

Related

How do I save the state of a selected item from recycle view?

I have multiple items in my chatListLinearLayout, I would like to update my messageStatus to say something such as "Item selected" for the selected item. I managed to do this successfully but I would like to save this so the next time the user reopens the app it will remain.Could someone assist me?
displayMessagesRecycleView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, final int position) {
CircleImageView profileImage = view.findViewById(R.id.users_profile_image);
LinearLayout chatListLinearLayout = view.findViewById(R.id.chatListLinearLayout);
final TextView messageStatus = view.findViewById(R.id.user_status);
chatListLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
messageStatus.setText("Item selected");
}
});
You can setSelected(true); for view and set one boolean in you object model
and for save selected item state you need to save your list into sharedPrefs or sqlite database

How do I make button select all checkboxes in my recyclerView?

The result of my code is that all the checkboxes are checked only after I scroll down past the checkboxes in the recyclerView - when I scroll back up, they are checked.
How can I have them checked as soon as I click the button?
In my activity I have:
private void publicButton() {
publicContacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//loop through the contacts
int count = PopulistoContactsAdapter.theContactsList.size();
for (int i = 0; i < count; i++) {
PopulistoContactsAdapter.theContactsList.get(i).setSelected(true);
}
}
});
}
And for the recyclerViewAdapter:
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
//bind the views into the ViewHolder
//selectPhoneContact is an instance of the SelectPhoneContact class.
//We will assign each row of the recyclerview to contain details of selectPhoneContact:
//The number of rows will match the number of phone contacts
final SelectPhoneContact selectPhoneContact = theContactsList.get(position);
//if the row is a matching contact
if (viewHolder.getItemViewType() == 1)
{
//in the title textbox in the row, put the corresponding name etc...
((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
//((MatchingContact) viewHolder).check.setText("Cheeckbox" + position);
((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).isSelected);
((MatchingContact) viewHolder).check.setTag(position);
((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pos is the row number that the clicked checkbox exists in
Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();
Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();
if (theContactsList.get(pos).getSelected()) {
theContactsList.get(pos).setSelected(false);
} else {
theContactsList.get(pos).setSelected(true);
}
}
});
}
}
The onClick in the Adapter works as planned for individual checkboxes; their state is preserved. It's just when I try to select all of them with the button in the activity that the problem arises.
The simplest answer by far is to simply call notifyDataSetChanged() on your adapter after you make all of your setSelected() calls.
From your code, I can't tell if PopulistoContactsAdapter is an adapter instance, or if PopulistoContactsAdapter.theContactsList is a static variable... you'll need an adapter instance to call notifyDataSetChanged(). If you only have a reference to the RecyclerView, you can call myRecyclerView.getAdapter().notifyDataSetChanged().

Show BottomSheet after change the layout

I have a BottomSheet "buttomSheet" (in this sheet it is a list view that is GONE) and a normal Button "btnShowListView" (out from the "buttomSheet").
I want that when I click on "btnShowListView" the list view in the bottomSheet will be Visible its work but only after 2 clicks on button "btnShowListView"...
this is my code:
final View bottomSheetView = findViewById(R.id.bottomSheetLayout);
listView = (ListView) bottomSheet.findViewById(R.id.listView);
buttomSheet = BottomSheetBehavior.from(bottomSheetView);
btnShowListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listView.setVisibility(View.VISIBLE);
bottomSheetView.requestLayout();
bottomSheetView.invalidate();
bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
You can make the listview already visible, there's no need to hide it; you can get te same effect by setting the state of the bottom sheet behavior to HIDDEN.
I tested the code below and it should work also for you.
final View bottomSheetView = findViewById(R.id.bottomSheetLayout);
bottomSheetBehavior= BottomSheetBehavior.from(bottomSheetView);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
btnShowListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});

Perform click event on Spinner to execute code in onItemSelected

I want to perform a click on a Spinner element automatically after my activity has been loaded completely.
I use this code to set up the spinner and adapter:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_data);
getActionBar().setDisplayHomeAsUpEnabled(true);
mSpinnerDay = (Spinner) mTable.findViewById(R.id.spieltag_choice);
mAdapterSpinnerDay = new ArrayAdapter<CharSequence>(this, R.layout.custom_spinner);
mAdapterSpinnerDay.setDropDownViewResource(R.layout.custom_spinner);
mSpinnerDay.setAdapter(mAdapterSpinnerDay);
}
private void setUpSpinnerListener(final IGameData data) {
mSpinnerDay.post(new Runnable() {
public void run() {
mSpinnerDay.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
for (GameDayData d : data.getGameDay()) {
if (d.getName().equals(adapterView.getSelectedItem().toString())) {
TableRow row = (TableRow) mTable.findViewById(R.id.row_punkte_tag);
TextView t = (TextView) row.findViewById(R.id.punkte_tag);
t.setText("Punkte: " + d.getScore());
TableRow row2 = (TableRow) mTable.findViewById(R.id.row_position_tag);
TextView t2 = (TextView) row2.findViewById(R.id.position_tag);
t2.setText("Position: " + d.getPosition());
return;
}
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
}
});
}
public void onTeamCheckReadComplete(IGameData data) {
for (GameDayData d : data.getGameDay()) {
mAdapterSpinnerDay.add(d.getName());
}
}
I try to perform the click with following code after I have set the adapter to the spinner:
mSpinnerDay.setSelection(0, true);
View view = (View) mSpinnerDay.getChildAt(0);
long id = mSpinnerDay.getAdapter().getItemId(0);
mSpinnerDay.performItemClick(view, 0, id);
But this does not work. Could somebody tell me how I can perfom a click on a spinner element automatically? When I select the spinner item over touch event in the application everything works fine.
Regards,
Sandro
Corrected Solution
As I understand it, you have a spinner with items A, B, C, and D. You want item A pre-selected. You also want the user to be able to select A, B, C, and D and perform an action based on that.
In the onCreate() method:
mSpinner.setAdapter(myAdapter);
mSpinner.setOnItemSelectedListener(this); // have the activity implement
// OnItemSelectedListener interface
doAction(0);
Then implement the onItemSelected action like so:
#Override
void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
doAction(position);
}
You must implement the doAction(int position) method to handle how to update your activity based on the position of the item in your adapter. Clear?
Read more about this item and how to use it here:
http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html
I dont think you are calling setUpSpinnerListener from the code that you have posted. Even if you are calling it, i dont think it is advantageous to post a runnable to setuplistener.
Move all the code in runnable to just after setAdapter

performClick not triggering

I am trying to trigger a click on the Carousel. I want that if I press the forward button, it automatically triggers the click on the carousel and then move forward. The manual click (physical touch) is working but performClick() is not. The code is as follows
//************* Forward Button: Select Objects *************
Button forwardButton = (Button)this.findViewById(R.id.ForwardButton);
forwardButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(NewFieldTrip.this, SelectObjects.class);
//ImagePosition = (int)carousel.getSelectedItemId();
carousel.performClick();
i.putExtra("SelectedScene",ImagePosition);
startActivity(i);
}
});
carousel.setOnItemClickListener(new CarouselAdapter.OnItemClickListener(){
#Override
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
Toast.makeText(NewFieldTrip.this, "Select Position=" + position, Toast.LENGTH_SHORT).show();
ImagePosition = position;
}
});
A helping hand would be great :)
EDIT:
public void scrollToChild(int i){
CarouselImageView view = (CarouselImageView)getAdapter().getView(i, null, null);
float angle = view.getCurrentAngle();
if(angle == 0)
return;
if(angle > 180.0f)
angle = 360.0f - angle;
else
angle = -angle;
mFlingRunnable.startUsingDistance(-angle);
}
What type is carousel of? ListView?
So if carousel is ListView then what event are you expected on ListView clicking?
performClick() trigger OnClickListener, which you didn't set to cаrousel. You set OnItemClickListener, so you have to call performItemClick(...). Try it.
UPD:
Try to do folowing:
1) make method Carousel.scrollToChild(int i) public
2)
int itemCount = carousel.getAdapter().getCount();
int item = new Random().nextInt(itemCount);
View view - carousel.getAdapter().getView(item, null, null);
int itemId = carousel.getAdapter().getItemId(item);
carousel.scrollToChild(item);
carousel.performItemClick(view, item, itemId);

Categories

Resources