You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView添加数据并调用notifyDataSetChanged后仍不显示的问题

RecyclerView添加数据后无显示的问题

现有一个RecyclerView控件suggestList,点击其任意itemView时,通过adapterPosition从适配器对应的ArrayList中获取数据。在MainActivity中创建了StockPrice()方法,该方法会在itemView点击时被调用,将suggestList传递过来的数据存入stockPriceList,而stockPriceList用于另一个RecyclerView。已完成相关配置,但添加数据并调用stockPriceAdapter.notifyDataSetChanged()后,第二个RecyclerView仍无数据显示。

suggestList适配器代码

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView symbol, name, exchange, type;
    public ConstraintLayout listContainer;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        name = itemView.findViewById(R.id.name);
        symbol = itemView.findViewById(R.id.symbol);
        exchange = itemView.findViewById(R.id.exchange);
        type = itemView.findViewById(R.id.type);
        listContainer = itemView.findViewById(R.id.search_item_views);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MainActivity mainActivity = new MainActivity();
                stockModelClass positionedStock =  stockModelClassArrayList.get(getAbsoluteAdapterPosition());
                mainActivity.OnClickItemViewListener(positionedStock.getName(), positionedStock.getSymbol());
               // Run this one
                mainActivity.StockPrice();

                stockModelClassArrayList.clear();
                notifyDataSetChanged();
            }
        });

    }
}

MainActivity相关代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    suggestList = findViewById(R.id.suggestList);
    searchBar = findViewById(R.id.searchBar);
    stockModelClassesArray = new ArrayList<>();

    requestQueue = Volley.newRequestQueue(MainActivity.this);

    suggestList.setLayoutManager(new LinearLayoutManager(this));

    suggestAdapter = new stockSuggestAdapter(stockModelClassesArray, MainActivity.this);

    suggestList.setAdapter(suggestAdapter);

    searchBar.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            loadStockApi();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    // Setting up Main Activity ArrayList.
    mainWatchList = findViewById(R.id.defaultWatchList);
    stockPriceList = new ArrayList<>();
    mainWatchList.setLayoutManager(new LinearLayoutManager(this));
    stockPriceAdapter = new StockPriceAdapter(stockPriceList, MainActivity.this);
    mainWatchList.setAdapter(stockPriceAdapter);
}



 public void StockPrice() {

    String name = Name;
    String Symbol = symbol;
    String price = "$20";
    String ap = "7d0e928bad8b4a428b2d4d5351bd8a51";
    String url = "https://api.twelvedata.com/price?symbol=" + symbol + "&apikey=" + ap; // For accessing Price

    stockPriceList = new ArrayList<>();
    stockPriceAdapter = new StockPriceAdapter(stockPriceList, MainActivity.this);

    stockPriceList.add(new defualtWatchListModel(Name, symbol, price));
    stockPriceAdapter.notifyDataSetChanged();

}

问题原因

  1. 错误创建Activity实例:适配器中通过new MainActivity()创建了新的Activity对象,这不是当前运行的界面实例,调用它的方法完全不会影响屏幕上的RecyclerView。
  2. 重复初始化集合与适配器:StockPrice()方法每次调用都会新建stockPriceList和stockPriceAdapter,和之前绑定到RecyclerView的实例毫无关联,调用notifyDataSetChanged()自然无效。
  3. 变量大小写混乱:Name、symbol这类变量大小写不一致,可能导致数据传递失败。

修复方案

1. 适配器改用接口回调传递点击事件

避免直接创建Activity实例,通过接口让MainActivity自己处理点击逻辑:

首先定义回调接口:

public interface OnStockItemClickListener {
    void onStockItemClicked(String name, String symbol);
}

修改适配器构造方法与点击逻辑:

public class stockSuggestAdapter extends RecyclerView.Adapter<stockSuggestAdapter.ViewHolder> {
    private ArrayList<stockModelClass> stockModelClassArrayList;
    private OnStockItemClickListener clickListener;

    // 构造方法传入回调接口
    public stockSuggestAdapter(ArrayList<stockModelClass> list, OnStockItemClickListener listener) {
        this.stockModelClassArrayList = list;
        this.clickListener = listener;
    }

    // ... 其他原有代码不变

    public class ViewHolder extends RecyclerView.ViewHolder {
        // ... 控件定义不变

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // ... 控件初始化不变

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAbsoluteAdapterPosition();
                    // 防止无效位置
                    if (position != RecyclerView.NO_POSITION) {
                        stockModelClass positionedStock = stockModelClassArrayList.get(position);
                        // 通过接口通知MainActivity
                        clickListener.onStockItemClicked(positionedStock.getName(), positionedStock.getSymbol());
                        stockModelClassArrayList.clear();
                        notifyDataSetChanged();
                    }
                }
            });
        }
    }
}

2. 修改MainActivity逻辑

实现回调接口,去掉StockPrice()中重复初始化的代码:

public class MainActivity extends AppCompatActivity implements stockSuggestAdapter.OnStockItemClickListener {
    // ... 原有变量定义不变

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ... 其他初始化代码不变

        // 初始化适配器时传入当前Activity作为回调
        suggestAdapter = new stockSuggestAdapter(stockModelClassesArray, this);
        suggestList.setAdapter(suggestAdapter);

        // ... 其他初始化代码不变
    }

    // 实现回调方法
    @Override
    public void onStockItemClicked(String name, String symbol) {
        StockPrice(name, symbol);
    }

    // 修改StockPrice方法,接收参数,复用原有集合与适配器
    public void StockPrice(String name, String symbol) {
        String price = "$20";
        String ap = "7d0e928bad8b4a428b2d4d5351bd8a51";
        String url = "https://api.twelvedata.com/price?symbol=" + symbol + "&apikey=" + ap;

        // 直接往已绑定的集合中添加数据
        stockPriceList.add(new defualtWatchListModel(name, symbol, price));
        // 通知适配器更新
        stockPriceAdapter.notifyDataSetChanged();
    }
}

3. 额外优化

  • 统一变量命名规范,比如全部使用小写开头的驼峰命名,避免大小写错误。
  • 后续调用API获取真实价格时,要在Volley的异步回调中更新UI,避免阻塞主线程。

内容的提问来源于stack exchange,提问作者Prateek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:34:53