如何从片段的recyclerview ViewHolder类更新活动中的textview

时间:2018-07-30 09:04:13

标签: android android-recyclerview

每当从活动类中删除textview项时,在更新活动类中的recyclerview时都会遇到问题。 getActivity()在这里不可用。谢谢大家。

public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView delete_from_cart;

    public ViewHolder(final View itemView) {
        super(itemView);

        delete_from_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // here I want to change the textview
                DatabaseHelper dh = new DatabaseHelper(context);
                dh.deleteCartDetail(product_name.getText().toString());
                delete(getLayoutPosition());

            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

可以有不同的方法来实现。下面是一些。

  1. 使用 interface callbacks ,它将在该Adapter类的构造函数中传递。 (最适合您的要求。
  2. from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC mathcaptha = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "captchaOperation"))).text.replace(" =", "") print(eval(mathcaptha)) 类中传递活动参考,并调用活动方法以更新textview。像Adapter;
  3. 您可以使用 EventBus 。这可以在您的应用程序中进行交流。

答案 1 :(得分:0)

from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
data = yf.download("MSFT", start="2017-01-01", end="2017-04-30")
print(data)

[*********************100%***********************]  1 of 1 downloaded
                 Open       High    ...     Adj Close    Volume
Date                                ...                        
2017-01-03  62.790001  62.840000    ...     60.664047  20694100
2017-01-04  62.480000  62.750000    ...     60.392612  21340000
2017-01-05  62.189999  62.660000    ...     60.392612  24876000
2017-01-06  62.299999  63.150002    ...     60.916084  19922900
2017-01-09  62.759998  63.080002    ...     60.722206  20256600
2017-01-10  62.730000  63.070000    ...     60.702820  18593000