I am taking data from a JSONOBJECT as response and then storing the "data" array from provided data and then looping through all the variables and assigning them to Name, Symbol, Exchange and Type variables. And then adding data into array list which is used by Adapter. Data Fetch from API is in right condition but Stored data in Recycler View duplicates multiple time. Instead showing one data One View at a time, it duplicates data over & over. You can see in this image The Structure of JSON response from API
Main Activity.java:
@Override
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);
stockModelClass stockModelClasses = new stockModelClass();
stockModelClassesArray.add(stockModelClasses);
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) {
}
});
}
public void loadStockApi() {
String searchQueary = searchBar.getText().toString();
String url = "https://api.twelvedata.com/symbol_search?symbol=" + searchQueary;
stockModelClassesArray.clear();
// Volley JsonRequest
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String sName, sSymbol, sExhange, sType;
stockModelClass StockModelClass = new stockModelClass();
JSONArray result = response.getJSONArray("data");
for (int i = 0; i < result.length(); i++) {
JSONObject results = result.getJSONObject(i);
Log.i("cs50", String.valueOf(results));
Log.i("cs50", "number:"+ i);
sName = results.getString("instrument_name");
sSymbol = results.getString("symbol");
sExhange = results.getString("exchange");
sType = results.getString("instrument_type");
StockModelClass.setName(sName);
StockModelClass.setSymbol(sSymbol);
StockModelClass.setExchange(sExhange);
StockModelClass.setType(sType);
String symbol = StockModelClass.getSymbol();
stockModelClassesArray.add(StockModelClass);
}
suggestAdapter.notifyDataSetChanged();
} catch (JSONException e) {
Log.i("MainActivity", "Json Error", e);
}
}
Adapter.java
public class stockSuggestAdapter extends RecyclerView.Adapter<stockSuggestAdapter.ViewHolder> {
ArrayList<stockModelClass> stockModelClassArrayList;
Context context;
public stockSuggestAdapter(ArrayList<stockModelClass> stockModelClassArrayList, Context context) {
this.stockModelClassArrayList = stockModelClassArrayList;
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView symbol, name, exchange, type;
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);
}
}
@NonNull
@Override
public stockSuggestAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_view_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull stockSuggestAdapter.ViewHolder holder, int position) {
stockModelClass stockData = stockModelClassArrayList.get(position);
holder.name.setText(stockData.getName());
holder.exchange.setText(stockData.getExchange());
holder.symbol.setText(stockData.getSymbol());
holder.type.setText(stockData.getType());
}
@Override
public int getItemCount() {
return stockModelClassArrayList.size();
}
I am trying to get data one by one, instead of duplicating one data over and over in all views. Please help...