Showing Error red .add() when adding Data into ArrayList

53 Views Asked by At

When I am trying to add Data in arraylist(listModels) in another class which is FetchStockApi it is showing red Error .add()(Cannot resolve symbol 'add'). But If I don't use FetchStockApi, and without taking it's instance, Add stuff in some ArrayList Created in MainActivity and use it for adapter it works and program works.

MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText suggestSearch;
RecyclerView suggestionList;

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

    suggestSearch = findViewById(R.id.searchBar);
    suggestionList = findViewById(R.id.suggestionList);

    // FetchStockApi Class
    FetchStockApi fetchStockApi = new FetchStockApi();

    // Adapter
    suggestListAdapter adapter = new suggestListAdapter();
    adapter.setStringArrayList(fetchStockApi.listModels);

    suggestionList.setLayoutManager(new LinearLayoutManager(this));
    suggestionList.setAdapter(adapter);

   

FetchStockApi.java

package com.example.stockapipractice2;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;

public class FetchStockApi {

ArrayList<suggestListModel> listModels = new ArrayList<>();
String stockName = "Nifty 50";
String stockSymbol = "NFTY";
String exchange = "NSI";
String type = "Index";
suggestListModel SuggestListModel = new suggestListModel(stockName, stockSymbol,exchange,type);
listModels.add(SuggestListModel);
// This above add is showing the Error
}

I just want to add Data in FetchStockApi and later use it in MainActivity for Recycler View adapter.

1

There are 1 best solutions below

6
David Wasser On

You have 2 separate instances of listModels. One in MainActivity and one in FetchStockApi. This is why making changes to the instance in FetchStockApi has no effect on the instance in MainActivity.

If you want to share the same instance, then you need to pass a reference of the instance you create in MainActivity to FetchStockApi so that it can manipulate that instance.


You can pass the listModels reference to FetchStockApi when you instantiate it. To do this, create a constructor for FetchStockApi like this:

public FetchStockApi(List<suggestListModel> listModels) {
    this.listModels = listModels;
}

Also change the declaration of listModels in FetchStockApi to this:

List<suggestListModel> listModels;

It will get initialized in the constructor.

Now you need to pass a reference to listModels from MainActivity to FetchStockApi when it gets constructed. Change MainActivity to instantiate FetchStockApi like this:

FetchStockApi fetchStockApi = new FetchStockApi(listModels); 

Now FetchStockApi can manipulate the same instance of listModels that MainActivity is using and that MainActivity has passed to the ListAdapter.

The only problem you now have is that after FetchStockApi has made changes to listModels, you need to somehow inform the ListView that the underlying data has changed, so that it can redraw the ListView. You could create an interface and have MainActivity implement this interface and have FetchStockApi call a method on that interface after it makes changes to listModels, but it is easier (although not as elegant) to just declare a public method in MainActivity like this:

void notifyListModelsChanged() {
    suggestionList.notifyDatasetChanged();
}

then add a member variable that will hold a reference to MainActivity to FetchStockApi like this:

MainActivity mainActivity;

then add a reference to MainActivity in the FetchStockApi constructor, like this:

public FetchStockApi(List<suggestListModel> listModels, MainActivity mainActivity;) {
    this.listModels = listModels;
    this.mainActivity = mainActivity;
}

then pass a reference to MainActivity to FetchStockApi in the constructor, like this:

FetchStockApi fetchStockApi = new FetchStockApi(listModels, this);

and have FetchStockApi call this method after it updates listModels, like this:

mainActivity.notifyListModelsChanged();