即使在数组列表和adapter.notifyDataSetChanged()中添加数据后,回收器视图也不会显示

问题描述 投票:0回答:1

有一个 RecyclerView

suggestList
,单击其中的任何
itemView
时,我们会得到适配器位置(),它已通过一个函数从用于
suggestList adapter
的 ArrayList 中获取值。现在,我在
StockPrice()
中创建了一个函数
MainActivity
,单击 itemView 时也会调用该函数,并且在 arrayList
stockPriceList
中,我们保存 itemView 从
suggestList
recyclerView 传递的数据。
stockPriceList
用于另一个recyclerView,一切都已设置,但是当我添加传递的数据
(from suggestList recyclerView)
并调用用于第二个recyclerView的
stockPriceAdapter.notifyDataSetChanged()
时,它没有在RecyclerView中显示任何数据。

建议列表适配器:

 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,其中我们有 StockPrice 函数。

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

}
java android android-recyclerview android-arrayadapter
1个回答
0
投票

出现此问题是因为

adapter
未设置为
RecyclerView

您已在

stockPriceAdapter
方法中将
stockPriceList
设置为
onCreate()
,但您在
stockPriceAdapter
方法中将新实例重新分配给
stockPrice()
并在该新实例上调用
notifyDataSetChanged()
方法(未设置)到 RecyclerView)。

解决方案: 您不需要在

ArrayList
方法中重新实例化
StockPriceAdapter
stockPrice()
,因为您已经在
onCreate()
方法中初始化了它们

更新代码:

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;
    stockPriceList.add(new defualtWatchListModel(name, Symbol, price));
    stockPriceAdapter.notifyDataSetChanged();
}
© www.soinside.com 2019 - 2024. All rights reserved.