有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAlang.ClassCastException:无法将CustomAdapter转换为安卓。小装置。java中的ArrayAdapter

我试图将json数据加载到一个可滚动的微调器中。但是我得到一个错误,CustomAdapter无法转换到ArrayAdapter中。你能帮我吗

提供食物。xml:这是微调器所在的文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="@drawable/grdnt"
    tools:context=".Provide_food">

    <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        安卓:id="@+id/select_food"
        安卓:layout_width="120dp"
        安卓:layout_height="50dp"
        安卓:layout_marginTop="250dp"
        安卓:background="@color/bgcolor"
        app:hintText="Select Item"
        />

    <Button
        安卓:id="@+id/select_food_button"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_toRightOf="@+id/select_food"
        安卓:layout_marginTop="250dp"
        安卓:layout_marginLeft="40dp"
        安卓:text="Add Item"/>

</RelativeLayout>

提供食物。java:用于将json数据加载到微调器的java类

package com.example.helping_hands_individual;

import 安卓x.annotation.NonNull;
import 安卓x.appcompat.app.AppCompatActivity;

import 安卓.content.ContentResolver;
import 安卓.content.Context;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.view.textclassifier.TextLinks;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ImageView;
import 安卓.widget.ListView;
import 安卓.widget.Spinner;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import com.安卓.volley.Request;
import com.安卓.volley.RequestQueue;
import com.安卓.volley.Response;
import com.安卓.volley.VolleyError;
import com.安卓.volley.toolbox.JsonObjectRequest;
import com.安卓.volley.toolbox.StringRequest;
import com.安卓.volley.toolbox.Volley;
import com.google.firebase.database.annotations.Nullable;
import com.google.gson.JsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Provide_food extends AppCompatActivity {

    Spinner spinner;
    String url = "api_link"; //can't mention the link here
    List<Item_Model> list = new ArrayList<>();
    List<String> list1 = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provide_food);

        spinner = (Spinner)findViewById(R.id.select_food);


        new Getdata().execute();
    }

    class Getdata extends AsyncTask<Void,Void,String>{

        String result;
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            CustomAdapter adapter = new CustomAdapter(Provide_food.this, list);
            spinner.setAdapter(adapter);
        }

        @Override
        protected String doInBackground(Void... voids) {

            try {
                URL mainurl = new URL(url);
                HttpURLConnection connection = (HttpURLConnection)mainurl.openConnection();
                InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(streamReader);

                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null){
                    builder.append(line);
                }
                result = builder.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                JSONArray array = new JSONArray(result);
                for (int i=0; i<array.length(); i++){
                    Item_Model item_model = new Item_Model();
                    JSONObject object = array.getJSONObject(i);
                    String itemname = object.getString("Item Name");
                    item_model.setItemname(itemname);
                    list.add(item_model);
                }
            }
             catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }

}

项_模型。java:用于从json api获取项目名称的模型类

package com.example.helping_hands_individual;

public class Item_Model {

    String itemname;

    public String getItemname() {
        return itemname;
    }

    public void setItemname(String itemname) {
        this.itemname = itemname;
    }
}

自定义适配器。java:用于将数据解析到微调器中的自定义适配器

package com.example.helping_hands_individual;

import 安卓.content.Context;
import 安卓.transition.Slide;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseAdapter;
import 安卓.widget.TextView;

import java.util.List;

public class CustomAdapter extends BaseAdapter {

    LayoutInflater inflater;
    Context context;
    List<Item_Model> list;

    public CustomAdapter(Context applicationContext, List<Item_Model> list){
        this.context = applicationContext;
        this.list = list;
        inflater = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflater.inflate(R.layout.item,null);
        TextView itemname = (TextView)view.findViewById(R.id.item_names);

        itemname.setText(list.get(i).itemname);

        return view;
    }
}

项目。xml:示例布局文件,其中仅包含将显示在微调器中的textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:orientation="vertical" 安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <TextView
        安卓:id="@+id/item_names"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginLeft="10dp"
        安卓:layout_marginTop="20dp"
        安卓:textSize="18dp"
        安卓:text="Demo"/>

</LinearLayout>

共 (1) 个答案

  1. # 1 楼答案

    你的CustomAdapter应该扩展ArrayAdapter,所以你可以用这种方式改变你的类:

    public class CustomAdapter extends ArrayAdapter {
    
        LayoutInflater inflater;
        Context context;
        List<Item_Model> list;
    
        public CustomAdapter(Context applicationContext, int resources, List<Item_Model> list) {
            super(applicationContext, resources);
            this.context = applicationContext;
            this.list = list;
            inflater = (LayoutInflater.from(applicationContext));
        }
    
        //...
    
    }
    

    考虑使用参数化{{CD3}}特征