0

I have ActivityCheckout.java an adapter AdapterServiceCourier inside activity. The adapter class is for showing radio-button price. How can I update textview with the view that is in ActivityCheckout. like

public TextView getTextViewPriceOngkir()
{

    TextView txtView = (TextView)findViewById(R.id.price_ongkir);
    return txtView;
}

when I used in adapter AdapterServiceCourier:

ActivityCheckout ac = new ActivityCheckout();
            TextView tv = ac.getTextViewPriceOngkir();
            tv.setText("8");

its error like:

E/UncaughtException: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>

AdapterServiceCourier.java :

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(AppConfig.APP_PUPUKKUJANG_MART, Context.MODE_PRIVATE);
    final String whois = sharedPreferences.getString(AppConfig.USER_WHOIS,null);
    row_index = -1;
    holder.itemView.setTag(service.get(position));
    final ServiceCourier p = service.get(position);
    holder.service.setText(p.getService());
    holder.desc.setText(p.getDescription());
    holder.cost.setText(p.getCost());
    holder.etd.setText(p.getEtd());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    View.OnClickListener rbClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton checked_rb = (RadioButton) v;
            if(lastCheckedRB != null){
                lastCheckedRB.setChecked(false);
            }
            lastCheckedRB = checked_rb;
            SharedPreferences sharedPreferences = context.getSharedPreferences(AppConfig.APP_PUPUKKUJANG_MART, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("ongkir_service", p.getService());
            editor.putString("ongkir_description", p.getDescription());
            editor.putString("ongkir_cost", p.getCost());
            editor.putString("ongkir_etd", p.getEtd());
            editor.apply();

            ActivityCheckout ac = new ActivityCheckout();
            TextView tv = ac.getTextViewPriceOngkir();
            tv.setText("8");
        }
    };
    holder.radiobutton.setOnClickListener(rbClick);
}

Display: image click radio button, price must set to ongkir price enter image description here

Pallavi Tapkir
  • 781
  • 7
  • 28
willy
  • 13
  • 8

4 Answers4

1

this is observable design pattern :

  1. your activity should implements Observer interface

  2. your adapter should extend Observable class

  3. you should add your activity to your adapter

  4. with notiftyObserver method you can update your activity

please note with this design pattern you can notify multiple activity.

devgun
  • 1,003
  • 13
  • 33
0

YourAdapter instance = new YourAdapter(context,arrayList,textView);

Now in constructor of adapter you can get access to that textview.

YourAdpater.java

TextView textView;
YourAdapter(Context context,Arraylist<ModelClass> arraylist,TextView textView)
{
this.textView = textView;
} 

Now in adapter you can update textview values.

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
0

In Your Adapter Class OnClick RadioButton

View.OnClickListener rbClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        RadioButton checked_rb = (RadioButton) v;
        if(lastCheckedRB != null){
            lastCheckedRB.setChecked(false);
        }
        .....
        .....
        //Create Statis Mathod in ActivityCheckout and Access Hear
         ActivityCheckout.updateTextView(String DataUWantToAdd);
    }
};

And In Yout ActivityCheckout Class

add static Mathod Dont miss That

class ActivityCheckout
{
 .....
  //on create and etc
  public static void updateTextView(String DataUWantToUpadate)
  {
    yourTextViewObject.setText(DataUWantToUpdate);
  }

}
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
0

You can access Activity's method into adapter using context.Make one method with string parameter in activity which set string to your textview and call that method in your adapter when you want to change the text.