【问题标题】:I want to list the information I got from json in my project in the recyclerview我想在recyclerview的项目中列出我从json得到的信息
【发布时间】: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


    【解决方案1】:

    ArrayAdapter 不适用于 RecyclerView。您必须创建自己的自定义适配器。关注this 并构建您自己的自定义适配器。

    【讨论】:

      【解决方案2】:

      ArrayAdapter 不适用于 RecyclerView。您需要实现自己的适配器。请参阅此答案以获取可能的解决方案:https://stackoverflow.com/a/38931340/7703505

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多