Recycler View is Not Showing Even After Adding Data in Array List & adapter.notifyDataSetChanged()

27 views Asked by At

There is a RecyclerView suggestList and on clicking any itemView of it, we get adapterposition() which has passed through a function to get values from ArrayList used for suggestList adapter. Now, I have created a function StockPrice() in MainActivity which is also called when itemView is clicked, and In arrayList stockPriceList we save the passed data by itemView from suggestList recyclerView. stockPriceList is used for another recyclerView, and it's all set, but when I am adding Passed data (from suggestList recyclerView) and also calling stockPriceAdapter.notifyDataSetChanged() used for second recyclerView, It's not showing any Data in the RecyclerView.

suggestList Adapter:

 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 in which we have StockPrice function.

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

There are 1 answers

2
Divyansh Gemini On

The problem is occurring because adapter is not set to the RecyclerView.

Solution: Set the stockPriceAdapter to the stockPriceList before notifying it for the dataset changed.

Updated Code:

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 = new ArrayList<>();
    stockPriceAdapter = new StockPriceAdapter(stockPriceList, MainActivity.this);
    stockPriceList.setAdapter(stockPriceAdapter);

    stockPriceList.add(new defualtWatchListModel(name, Symbol, price));
    stockPriceAdapter.notifyDataSetChanged();
}