2

I've looked at some answers but nothing looks like it could solve this problem.

I'm trying to do a dictionary that shows the definitions inside a cardView. I have a RelativeLayout that contains a linearLayout (with the search bar and button) and the recyclerView.

I'm managing this in the Activity class called Dictionary.java

Some code:

public class Dictionary extends AppCompatActivity {
String URL = "https://jisho.org/api/v1/search/words";

RecyclerView recyclerView;
DicoAdapter dicoAdapter;
Button search;
EditText wordToSearch;
List<WordDico> resultSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dictionary);
    AndroidNetworking.initialize(getApplicationContext());
    search = findViewById(R.id.searchbutton);
    wordToSearch = findViewById(R.id.wordToSearch);

    recyclerView = findViewById(R.id.dicoview);
    resultSearch = new ArrayList<>();
    dicoAdapter = new DicoAdapter(getApplicationContext(), resultSearch);
    recyclerView.setAdapter(dicoAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    search.setOnClickListener(event ->{
        if(wordToSearch.getText().toString() != null && wordToSearch.getText().toString().equals("")){
            // todo
            // handle error
        }else {
            Log.d("word sent", wordToSearch.getText().toString());

            AndroidNetworking.get(URL).
                    setPriority(Priority.LOW).addQueryParameter("keyword", wordToSearch.getText().toString()).
                    build().getAsJSONObject(new JSONObjectRequestListener(){
                @Override
                public void onResponse(JSONObject response) {
                    if (response != null) {
                        //build the list
                        Log.d("Request returned", response.toString());
                        Moshi moshi = new Moshi.Builder().build();
                        Type listMyData = Types.newParameterizedType(List.class, WordDico.class);
                        JsonAdapter<List<WordDico>> adapter = moshi.adapter(listMyData);
                        try {
                            resultSearch = adapter.fromJson(response.getJSONArray("data").toString());
                            dicoAdapter.notifyDataSetChanged();
                            Log.d("Ma liste: ", "" + resultSearch.size());
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        //handle the error
                    }
                }

                @Override
                public void onError(ANError anError) {
                    //handle error in the networking
                    Log.d("Error request", "error code: " + anError.getErrorBody() +"  " +anError.getErrorDetail());
                }
            });
        }
    });
}
}

So I notify the dataset changed but nothing happens.

It's the first time I try to put a recyclerView inside an already running activity.

Here's the dictionary layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu_bg"
tools:context=".Dictionary">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/search"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="50dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/l_s_dic"
    android:fontFamily="@font/font"/>
<EditText
    android:id="@+id/wordToSearch"
    android:layout_width="190dp"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/searchbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/b_s_dic"/>
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dicoview"
    android:layout_below="@+id/search">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Can someone explain the logic behind the use of the recyclerView and how to solve this issue?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Bouji
  • 83
  • 8

1 Answers1

1

See this answer and read the given explanation for Replace old list with new list. Instead of overwriting your List object resultSearch that is set to the adapter with an object created from json adapter you should create a new list for the JSON response, clear the old list and add the new list to the old list. Then call notifyDataSetChanged(). Hope it helps.

Nulldroid
  • 681
  • 4
  • 15
  • Even though I read that solution, I didn't think overwriting could be a problem in this situation. Works well now. Thank you. – Bouji Apr 05 '19 at 13:31