duandiao3961 2015-03-01 18:22
浏览 70
已采纳

从数据库填充Spinner时的奇怪行为

I am trying to populate a spinner from Mysql Databse through JSON. The thing is that the data are exported fine. But when I click the Spinner to show the dropdown menu the app crashes.

The JSON output from database: Output

The Error:

03-01 18:12:42.966    3214-3214/com.order.app.order E/ArrayAdapter﹕ You must supply a resource ID for a TextView
03-01 18:12:42.966    3214-3214/com.order.app.order D/AndroidRuntime﹕ Shutting down VM
03-01 18:12:42.967    3214-3214/com.order.app.order E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.order.app.order, PID: 3214
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
            at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
            at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)

My Adapter:

public class SpiritsListAdapter extends ArrayAdapter<SpiritList> {

    public SpiritsListAdapter(Context context, int layoutId, List<SpiritList> items) {
        super(context, layoutId, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SpiritHolderItems holder;

        if(convertView == null){
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            convertView = vi.inflate(R.layout.spinner_item, parent, false);

            holder = new SpiritHolderItems();
            holder.spiritName = (TextView)convertView.findViewById(R.id.spirit_name);
            holder.spiritPrice = (TextView)convertView.findViewById(R.id.spirit_price);
            convertView.setTag(holder);
        }else{
            holder = (SpiritHolderItems) convertView.getTag();
        }
            SpiritList current = getItem(position);
            holder.spiritName.setText(current.getName());
            holder.spiritPrice.setText(current.getPrice() + " €");
        return convertView;
    }

    static class SpiritHolderItems {
        TextView spiritName, spiritPrice;
    }
}

My AsyncTask:

public class JsonReadTask extends AsyncTask<String , Void, List<SpiritList>> {
        public JsonReadTask() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Gins.this, ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setIndeterminate(true);
            pDialog.setMessage(getString(R.string.get_stocks));
            pDialog.setCancelable(false);
            pDialog.setInverseBackgroundForced(true);
            pDialog.show();
        }

        @Override
        protected List<SpiritList> doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
                customList = new ArrayList<>();

                JSONObject jsonResponse = new JSONObject(jsonResult);
                JSONArray jsonMainNode = jsonResponse.optJSONArray("gins");
                for (int i = 0; i < jsonMainNode.length(); i++) {
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                    String name = jsonChildNode.optString("name");
                    String price = jsonChildNode.optString("price");
                    customList.add(new SpiritList(name, price));
                }
                return customList;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
            return answer;
        }

        @Override
        protected void onPostExecute(List<SpiritList> customList) {
            if(customList == null){
                Log.d("ERORR", "No result to show.");
                return;
            }
            ListDrawer(customList);
            pDialog.dismiss();
        }
    }// end async task

    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        task.execute(new String[]{url});
    }

    public void ListDrawer(List<SpiritList> customList) {
        adapterGins = new SpiritsListAdapter(getApplicationContext(), R.layout.spinner_item, customList);
        adapterGins.notifyDataSetChanged();
        spDrinks.setAdapter(adapterGins);
        Log.d("Spinner Count", "The Spinner count is " + spDrinks.getCount());
    }

My Layout looks like:

enter image description here

Any ideas why is this happening? I am inflating the custom layout and yet it takes it as an ArrayAdapter<String>.

  • 写回答

2条回答 默认 最新

  • douyeke2695 2015-03-02 15:43
    关注

    You can do the same thing using BaseAdapter as such:

    public class SpiritsListAdapter extends BaseAdapter
    

    Modify your constructor like this:

        private List<SpiritList> objects;
        private Context context;
    
        public SpiritsListAdapter(Context context ,List<SpiritList> objects) {
            this.context = context;
            this.objects = objects;
        }
    
        @Override
        public int getCount() {
            return objects.size();
        }
    
        @Override
        public Object getItem(int position) {
            return objects.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    

    and add the unimplemented methods and you keep your getView method as it is.

    Hope it helps!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法