Представление Recycler не работает или не отображаетсяJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Представление Recycler не работает или не отображается

Сообщение Anonymous »


I am trying to take data from API and save it in Array List which is used in Recycler View adapter. But it's not showing. As user searches, on every text change an API call gets generated and we get data from API that I am trying to add that data into the same array List that has been used to create Recycler View adapter, and Recycler view is under the search bar so as it shows up as suggestion returned from API search like autocompleteTextView.

Here is the code:

MainActivity:

public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private EditText et_DataInput; RecyclerView recyclerView; ArrayList stockSuggestionList; // There we are adding Data in Array public void addElement(stockSuggestionModel element){ stockSuggestionList.add(element); } stockSuggestAdapter suggestAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_DataInput = (EditText) findViewById(R.id.et_inputData); // ArrayList stockSuggestionList = new ArrayList(); // recycler view recyclerView = findViewById(R.id.searchSuggestion); // Adapter redefining; suggestAdapter = new stockSuggestAdapter(MainActivity.this, stockSuggestionList); // It is used to tell in which form our layout will be. recyclerView.setLayoutManager(new LinearLayoutManager(this)); // set Adapter recyclerView.setAdapter(suggestAdapter); //To make the search facility which captures (one by one variable). et_DataInput.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) { String quearyString = et_DataInput.getText().toString(); // if else condition for blank Log.i(TAG, "Search: " + quearyString); // There we have created // There we are adding data to the ArrayList to Show-Up FetchStock fetchStock = new FetchStock(); fetchStock.execute(quearyString); } @Override public void afterTextChanged(Editable s) { } }); } } FetchStock:

JSONObject jsonObject = new JSONObject(s); // There can be a problem as there is Two Data name variable. JSONArray data = jsonObject.getJSONArray("data"); for (int i=0; i < data.length(); i++){ // Instance of Model Class stockSuggestionModel StockSuggestionModel = new stockSuggestionModel(); // JsonObject to really get Data JSONObject itemsArray = data.getJSONObject(i); // variable to transmit data String symbol = null; String instrument_name = null; String exchange = null; try { symbol = itemsArray.getString("symbol"); instrument_name = itemsArray.getString("instrument_name"); exchange = itemsArray.getString("exchange"); } catch (Exception e){ e.printStackTrace(); } if(symbol != null && instrument_name != null){ StockSuggestionModel.setStockName(instrument_name); StockSuggestionModel.setStockSymbol(symbol); String instrument = StockSuggestionModel.getStockName(); Log.i(TAG, "Data Has adding into the array used by adapter"); Log.i(TAG,""+instrument); // adding data into the arrayList MainActivity mainActivity = new MainActivity(); mainActivity.addElement(StockSuggestionModel); } } stockSuggestionModel.java:

public class stockSuggestionModel { String stockName; String stockSymbol; public stockSuggestionModel(String stockName, String stockSymbol) { this.stockName = stockName; this.stockSymbol = stockSymbol; } public stockSuggestionModel() { } public String getStockName() { return stockName; } public void setStockName(String stockName) { this.stockName = stockName; } public String getStockSymbol() { return stockSymbol; } public void setStockSymbol(String stockSymbol) { this.stockSymbol = stockSymbol; } } stockSuggestAdapter.java

public class stockSuggestAdapter extends RecyclerView.Adapter { Context context; ArrayList stockSuggestionArrayList; public stockSuggestAdapter(Context context, ArrayList stockSuggestionArrayList) { this.context = context; this.stockSuggestionArrayList = stockSuggestionArrayList; } @NonNull @Override public stockSuggestAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(context); View view = layoutInflater.inflate(R.layout.suggest_items, parent, false); stockSuggestAdapter.viewHolder StockSuggestAdapter = new stockSuggestAdapter.viewHolder(view); return StockSuggestAdapter; } @Override public void onBindViewHolder(@NonNull stockSuggestAdapter.viewHolder holder, int position) { stockSuggestionModel StockSuggestion = stockSuggestionArrayList.get(position); holder.stockName.setText(String.valueOf(StockSuggestion.stockName)); holder.stockSymbol.setText(String.valueOf(StockSuggestion.stockSymbol)); // We also need a Func when User Click on our List. (userAdapter of Java Messenger has implementation of this) } @Override public int getItemCount() { return stockSuggestionArrayList.size(); } public class viewHolder extends RecyclerView.ViewHolder { TextView stockName; TextView stockSymbol; public viewHolder(@NonNull View itemView) { super(itemView); stockName = itemView.findViewById(R.id.stockName); stockSymbol= itemView.findViewById(R.id.stockSymbol); } } } If needed anything else, please ask and Thanks in advance.

Trying to take data from API and add it into the Array List used by Recycler View in order to show recycler view under the search bar as autoCompleteTextView Effect.


Источник: https://stackoverflow.com/questions/780 ... showing-up
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»