// RecyclerAdapter (On click listener for the image)
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer p = getAdapterPosition()
Drawable d = imageView.getDrawable();
((MainActivity) itemView.getContext()).showOrganisation(p,d);
}
});
// Main Activity
public void showOrganisation(Integer p, Drawable d) {
myDialogOrg.setContentView(R.layout.popup_organisation);
TextView selectButton = myDialogOrg.findViewById(R.id.popOrgClose);
ImageView imageView = myDialogOrg.findViewById(R.id.popUpLogo);
imageView.setImageDrawable(d);
myDialogOrg.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
selectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences pref = v.getContext().getSharedPreferences("CheckBoxStateOrganisationer", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
// Send mysql query to database that a charity is selected
// Code
response{
// Store State of checkbox
editor.putBoolean("CheckBoxOrganisationer"+p, true);
editor.commit();
}
////////////////////////////////
// update state of checkbox in//
////////////////////////////////
-> I've added code here <-
// Dismiss popup window
myDialogOrg.dismiss();
}
});
myDialogOrg.show();
}
What I've tried is Fragment.adapter.notifyItemChanged(p);
and Fragment.adapter.notifyDataSetChanged();
The outcome is that the cardView in the recyclerview gets cleared and the original layout of the recycled cardView is displayed.
Is there a way to reload the "MyViewHolder" of the recycler.adapter for one position so that the cardview updates?
// Organisation_Fragment
Initiates the adapter
When the button in the AlertDialog is clicked and the alert is dismissed the checkbox of a specific item(position) in the recyclerView should appear selected.
In this case, I usually add a method in my Adapter which reacts to the data change
Adapter:
public void updateData(List<String> newList){
suggestions.clear();
if(newList!=null) suggestions.addAll(newList);
notifyDataSetChanged();
}
Your Activity:
private AutoCompleteAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
adapter = new AutoCompleteAdapter
(context, suggestions, etAnswer);
recyclerView.setAdapter(adapter);
}
your click listener
selectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your other code
////////////////////////////////
// update state of checkbox in//
////////////////////////////////
-> I've added code here <-
adapter.updateData(yourNewList);
}
});
Use the adapter method
adapter.notifyItemChanged(position, your Object);
Related
This is the onBinderViewHolder on the Adapter I used to connect data to Recyclerview
public void onBindViewHolder(Adapter_PO_Brands.BrandViewHolder holder, int position) {
DrugData DD = mDrugData.get(position);
holder.txtBrand.setText(DD.getBrand());
holder.txtGeneric.setText(DD.getGeneric());
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ShowAddPOQty(holder.txtQty,DD);
});
}
This is the ShowAddPOQty Method use to generate the dialog
public void ShowAddPOQty (TextView Qty ,DrugData dd) {
dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog_po_qty);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
TextView txtQty=dialog.findViewById(R.id.txtQty);
TextView Increase=dialog.findViewById(R.id.increase);
TextView Decrease=dialog.findViewById(R.id.decrease);
MaterialButton btAdd=dialog.findViewById(R.id.btn_Add);
MaterialButton btCancel=dialog.findViewById(R.id.btn_Cancel);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Qty.setText("Qty : "+txtQty.getText().toString());
dialog.cancel();
});
dialog.show();
}
The Thing I wanted to do is , When I click an item on Recyclerview , I want to show a dialog . The user set a quantity and click on Add button on dialog. Then I need to update that value on the recyclerview item that user clicked. The above code update the value in various items in the recyclerview. Not only in the item the user clicked.
Deperately needed your help thanks.
If you are using this method in adapter then do:
take which object of list you want to change in method and after changing that object instance notify the recycler adapter
public void ShowAddPOQty (TextView Qty ,DrugData dd) {
dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog_po_qty);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
TextView txtQty=dialog.findViewById(R.id.txtQty);
TextView Increase=dialog.findViewById(R.id.increase);
TextView Decrease=dialog.findViewById(R.id.decrease);
MaterialButton btAdd=dialog.findViewById(R.id.btn_Add);
MaterialButton btCancel=dialog.findViewById(R.id.btn_Cancel);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Qty.setText("Qty : "+txtQty.getText().toString());
// here change your object values
dd.setValue(value);
notifyDataSetChanged();
dialog.cancel();
});
dialog.show();
}
I have a listview with a series of elements.
The click on an item of listview shows me a custom dialog.
In custom dialog I have a layout with:
A spinner
Two buttons (OK / ANNULLA)
This is the normal situation:
When I select the spinner, he shows a list of items.
When I select an item from the spinner, the text that was present on the buttons disappear in this way:
ps: this does not happen on Android 6.0, but it happens in the lower versions (such as 5.0)
The Code:
public void showDialogTagAssociation (Activity activity, Handler handler,
String msg, final MyOperator elemento, final BluetoothDevice device,
final int position){
mHandler = handler;
//-----------------------------------------------------
// DIALOG
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
//-----------------------------------------------------
//---------------------------------------------------------------------
// LAYOUT
dialog.setContentView(R.layout.alert_dialog_custom_tag);
**// Spinner element
spinner = (Spinner) dialog.findViewById(R.id.spinner);**
// Spinner click listener
**spinner.setOnItemSelectedListener(new OnSpinnerItemClicked());**
//---------------------------------------------------------------------
//----------------------------------------------------------------------
// BUTTON OK
dialogButtonOK = (Button) dialog.findViewById(R.id.acd_btn_ok);
dialogButtonOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Devo assegnare il tag al nome....");
Log.d(TAG, "Nome: " +tmpNome+"\n" +
"TAG: "+device.getName()+" - "+device.getAddress());
dialog.dismiss();
}
});
// BUTTON ANNULLA
dialogButtonNO = (Button) dialog.findViewById(R.id.acd_btn_no);
dialogButtonNO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
//----------------------------------------------------------------------
dialog.show();
}
**private class OnSpinnerItemClicked implements android.widget.AdapterView.OnItemSelectedListener {**
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//NOME
tmpNome = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Thanks for the future help
Remove that piece of code:
spinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
and the thing which you want will still work.
Here there is minor issue Like I had Recyclerview in dialog fragment.ie name of bank in recyclerview When we select one bank in recyclerview and after dialogfragment dismiss that name should be appear on Button ie when we selected Union Bank from dialog fragment it should appear on button.Issue is when we click on button then its text changes rather then on time of dismiss listener
here is Dialog dismissal code:
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getContext(), mRecyclerView, new ClickListener() {
#Override
public void onClick(View view, final int position) {
Employee e = bank.get(position);
Toast.makeText(getContext(), e.getBank_id() + "" + e.getBank_name(), Toast.LENGTH_SHORT).show();
getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
Employee e = bank.get(position);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor edit = sp.edit();
edit.putString("bankname", e.getBank_name());
edit.commit();
}
});
c.onItemSelect(e.getBank_name());
onDismiss(getDialog());
}
Here is onclick event where dialog opens and where the value should be printed:
select_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm=getFragmentManager();
DialogRecyclerview dr = new DialogRecyclerview(AccountManagement_banks.this,callback);
dr.setRetainInstance(true);
dr.show(getSupportFragmentManager(), "Dialog");
SharedPreferences st = PreferenceManager.getDefaultSharedPreferences(AccountManagement_banks.this);
String mode=st.getString("bankname","");
select_button.setText(mode);
Toast.makeText(getApplication(),mode,Toast.LENGTH_SHORT).show();
}
});
Same in:
#Override
public void onItemSelect(String text) {
select_button.setText(text);
}
Here I had created new Interface:
public interface CallBack {
void onItemSelect(String text);}
just create a callback and implement it on your main class (where you want to display the name) and pass the callback instance to adapter. Now dialog fragment, now when you are selecting any item just call callback function which is overridden in main calss and inside this function just change the text of your button.
public interface CallBack {
void onItemSelect(String text);
}
implement this in your main class like
public class MainActivity extends Activity implements CallBack {
.
.
.
public void onItemSelect(String text){
button.setText(text);
}
.
.
}
when you are opening your dialogfragment from your main activity just pass MainActivity.this as an argument in the dialog constructor. And in your Dialog class constructor write your code like this
private Callback callback;
public YourDialog(Context context, Callback callback){
this.callback = callback;
}
and when you selecting list item just call
callback.onItemSelect(e.getBank_name());
Hope it will help you out.
I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}
I am trying to use a spinner control that will enable the user to delete any list element.
I have an 'add' button to add elements to the list, and a 'delete' button that removes the currently-displayed item from the list.
It works as expected except when the user deletes the last item in the list. At that point, all of the list's items are deleted.
My code is as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// grab our UI elements so we can manipulate them (for the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
// create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
((ArrayAdapter)m_adapterForSpinner).setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
// add listener for addButton
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
clearButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
// add listener for addButton
private void addNewSpinnerItem() {
if (m_addItemText.getText().length() == 0) {
Toast.makeText(getApplicationContext(), "The textView is empty", Toast.LENGTH_LONG).show();
} else {
CharSequence textHolder = "" + m_addItemText.getText();
((ArrayAdapter) m_adapterForSpinner).add(textHolder);
}
m_addItemText.setText("");
}
private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO
}
});
}
Does anyone have any ideas or suggestions on how to make this work?
The problem with your code is that the deletion is inside the onItemSelected callback, which gets called every time you are deleting an entry, thus deleting recursively until you effectively do not have any more entries to select. If you add a log inside that method:
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
you will see what I mean. I'm sure you can come up with more elegant code, but a quick and dirty hack is to set up a boolean flag to stop the recursion after the first deletion. See the snippet below and add the commented lines to your own code:
public class SpinnerTest extends Activity {
Spinner m_myDynamicSpinner;
EditText m_addItemText;
ArrayAdapter m_adapterForSpinner;
public static boolean cleared = false; // <--- set up a static boolean here
#Override
public void onCreate(Bundle savedInstanceState) {
// all your code unchanged
clearButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cleared=false; // <--- nope, we did not clear the value yet
clearSpinnerItems();
}
});
}
// code unchanged
private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
if (!cleared) // <--- did I do it already?
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
cleared=true; // I did it!
}
// code unchanged
i cann't understand your question.any way you can get the position of the selected item by using getSelectedItemPosition() method.