在实现FastAndroidNetwork的lambda中的notifyDataSetChanged

时间:2019-04-05 12:23:35

标签: android android-recyclerview android-cardview

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

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

我在名为Dictionnary.java的Activity类中对此进行管理

某些代码:

public class Dictionnary 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_dictionnary);
    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放入已经运行的活动中。

这是词典布局:

<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=".Dictionnary">

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

有人可以解释使用recyclerView的逻辑以及如何解决此问题吗?

1 个答案:

答案 0 :(得分:1)

请参阅this answer,并阅读有关用新列表替换旧列表的说明。 不应使用从json适配器创建的对象覆盖设置到适配器的List对象resultSearch,而应为JSON响应创建新列表,清除旧列表并将新列表添加到旧列表。然后调用notifyDataSetChanged()。 希望对您有所帮助。