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.
You have 2 separate instances of
listModels. One inMainActivityand one inFetchStockApi. This is why making changes to the instance inFetchStockApihas no effect on the instance inMainActivity.If you want to share the same instance, then you need to pass a reference of the instance you create in
MainActivitytoFetchStockApiso that it can manipulate that instance.You can pass the
listModelsreference toFetchStockApiwhen you instantiate it. To do this, create a constructor forFetchStockApilike this:Also change the declaration of
listModelsinFetchStockApito this:It will get initialized in the constructor.
Now you need to pass a reference to
listModelsfromMainActivitytoFetchStockApiwhen it gets constructed. ChangeMainActivityto instantiateFetchStockApilike this:Now
FetchStockApican manipulate the same instance oflistModelsthatMainActivityis using and thatMainActivityhas passed to theListAdapter.The only problem you now have is that after
FetchStockApihas made changes tolistModels, you need to somehow inform theListViewthat the underlying data has changed, so that it can redraw theListView. You could create an interface and haveMainActivityimplement this interface and haveFetchStockApicall a method on that interface after it makes changes tolistModels, but it is easier (although not as elegant) to just declare a public method inMainActivitylike this:then add a member variable that will hold a reference to
MainActivitytoFetchStockApilike this:then add a reference to
MainActivityin theFetchStockApiconstructor, like this:then pass a reference to
MainActivitytoFetchStockApiin the constructor, like this:and have
FetchStockApicall this method after it updateslistModels, like this: