【发布时间】:2021-09-23 10:30:03
【问题描述】:
我想在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
【问题讨论】:
标签: android json android-recyclerview