簡體   English   中英

如何將文本從recyclerview適配器更新到其他布局xml

[英]How to update text from recyclerview adapter to other layout xml

我在ActivityCheckout.java內部有一個ActivityCheckout.java適配器AdapterServiceCourier 適配器類用於顯示單選按鈕的價格。 如何使用ActivityCheckout中的視圖更新textview 喜歡

public TextView getTextViewPriceOngkir()
{

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

當我在適配器AdapterServiceCourier

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

其錯誤如下:

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);
}

顯示:圖像單擊單選按鈕,價格必須設置為ongkir價格, 在此處輸入圖像描述

這是可觀察的設計模式:

  1. 您的活動應實現觀察者接口

  2. 您的適配器應擴展Observable類

  3. 您應該將活動添加到適配器

  4. 使用notiftyObserver方法可以更新您的活動

請注意,使用這種設計模式可以通知多個活動。

YourAdapter實例=新的YourAdapter(context,arrayList,textView);

現在,在適配器的構造函數中,您可以訪問該textview。

YourAdpater.java

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

現在,在適配器中,您可以更新textview值。

在您的適配器類中,OnClick單選按鈕

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);
    }
};

在您的ActivityCheckout類中

添加靜態Mathod不要錯過That

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

}

您可以使用上下文將Activity的方法訪問到適配器中。在activity中使用一個帶字符串參數的方法來將string設置為textview,並在您想要更改文本時在適配器中調用該方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM