NotifyDataSetChanged 在实现 FastAndroidNetwork 的 lambda 中

NotifyDataSetChanged inside a lambda that implements FastAndroidNetwork

我看了一些答案,但似乎没有什么可以解决这个问题。

我正在尝试制作一本字典,在 cardView 中显示定义。我有一个包含 linearLayout(带有搜索栏和按钮)和 recyclerView 的 RelativeLayout。

我在名为 Dictionary.java

的 Activity class 中管理这个

部分代码:

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());
                }
            });
        }
    });
}
}

所以我通知数据集已更改但没有任何反应。

这是我第一次尝试将 recyclerView 放入已经 运行 activity 中。

这是字典布局:

<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>

谁能解释一下使用 recyclerView 背后的逻辑以及如何解决这个问题?

请参阅 并阅读 用新列表替换旧列表 的给定解释。 不要使用从 json 适配器创建的对象覆盖设置为适配器的列表对象 resultSearch,您应该为 JSON 响应创建一个新列表,清除旧列表并将新列表添加到旧列表。然后调用 notifyDataSetChanged()。 希望对你有帮助。