android - 我想在recyclerview的项目中列出我从json得到的信息
问题描述
我想在recyclerview的项目中列出我从json获得的信息,但我无法成功。你能帮助我吗?
我的代码:
RecyclerView countries = (RecyclerView) findViewById(R.id.countries);
countries.setLayoutManager(new LinearLayoutManager(this));
CityList cityList = new CityList();
try {
BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.jsondata)));
StringBuilder jsonBuilder = new StringBuilder();
for (String line = null; (line = jsonReader.readLine()) != null; ) {
jsonBuilder.append(line).append("\n");
}
Gson gson = new Gson();
cityList = gson.fromJson(jsonBuilder.toString(), CityList.class);
}
catch (FileNotFoundException e){
Log.e("jsonFile", "file not found.");
}
catch (IOException e){
Log.e("jsonFile","ioerror");
}
List <String> data = new ArrayList<>();
for(int i=0; i<cityList.getList().size(); i++){
data.add(cityList.getList().get(i).getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.country_item_layout, data);
adapter.setDropDownViewResource(R.layout.country_item_layout);
countries.setAdapter(adapter);
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/countries"
tools:listitem="@layout/country_item_layout"
app:layoutManager="LinearLayoutManager"
/>
</FrameLayout>
</LinearLayout>
国家 XML
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="8dp"
>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.ExampleFragment" >
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="7dp"
android:layout_marginHorizontal="15dp">
<TextView
android:id="@+id/countryName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_marginEnd="30dp"
android:textColor="@color/black"
android:text="Türkiye"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
<TextView
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:textColor="@color/black"
android:text="Temperature"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
</RelativeLayout>
</androidx.fragment.app.FragmentContainerView>
</com.google.android.material.card.MaterialCardView>
问题:“androidx.recyclerview.widget.RecyclerView”中的“setAdapter(androidx.recyclerview.widget.RecyclerView.Adapter)”不能应用于“(android.widget.ArrayAdapter<java.lang.String>)”
解决方案
ArrayAdapter 不适用于 RecyclerView。您需要实现自己的适配器。请参阅此答案以获取可能的解决方案:https ://stackoverflow.com/a/38931340/7703505
推荐阅读
- excel - 如何为图表系列中的点按定义事件?
- javascript - 如何在 Thymeleaf 中对变量进行子字符串化并将其与 url 连接起来
- python - 如何从内部函数访问在外部函数中定义的变量
- java - 使用 try-with-resources 重写此代码
- gnuplot - 你能在gnuplot中执行一个字符串吗?
- xamarin - 如何创建带有文本和背景图像的 Xamarin 按钮
- python - 从父子关系表创建嵌套字典
- python - 为什么以下代码会给出错误-“错误:'NoneType' 对象不可调用”?
- azure - Azure 搜索筛选器 search.ismatch 函数不适用于混合特殊字符(如 hiphen 和单引号)
- java - 尽管遵循在线说明等,但 Netbeans servlet 错误仍然存在