本文介绍了如何显示listiview不同的JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSON解析在我的应用程序,我得到如下回应,我想从我的回应listiview显示的名字,但我的应用程序崩溃了,任何一个可以告诉我什么是错误

  [
        {
            friend_id:1,
            user_spoc_dob:1993年11月11日,
            状态:真正的,
            spouse_name:百合,
            OCC:SPOC
        },
        {
            friend_id:1,
            user_mother_dob:1974年12月12日,
            状态:真正的,
            mother_name:SITA,
            消息:详细地址不可用,
            OCC:妈妈
        },
        {
            friend_id:1,
            user_father_dob:1972年11月19日,
            状态:真正的,
            father_name:公羊,
            消息:详细地址不可用,
            OCC:爸爸
        },
        {
            friend_id:1,
            user_dob:1994年2月20日,
            状态:真正的,
            user_fname:苏,
            OCC:杀破狼
        }
    ]

Mainactiviyt.java

 类LoadAllStates扩展
        AsyncTask的<字符串,字符串的ArrayList<串GT;> {
    私人ProgressDialog pDialog;
    私人字符串试验;
    私人的ArrayList<串GT; testlist;
    私人字符串光正交码;    私人字符串ID;    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(TestFamilyocaasion.this);
        pDialog.setMessage(请稍候...);
        pDialog.setIndeterminate(真);
        pDialog.setCancelable(真);
        pDialog.show();
    }    保护的ArrayList<串GT; doInBackground(
            字符串参数... args){
        SH的ServiceHandler =新的ServiceHandler();
        //制作到URL的请求和响应得到
        statedata =新的ArrayList<串GT;();
        testlist =新的ArrayList<串GT;();
        字符串jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL,ServiceHandler.GET);        Log.d(回应:,>中+ jsonStr);        Log.d(回应:,>中+ jsonStr);
        如果(jsonStr!= NULL){
            尝试{
                jsonObj =新JSONArray(jsonStr);
                的for(int i = 0; I< jsonObj.length();我++){
                    JSONObject的C = jsonObj.getJSONObject(I)
                    串wifebday =(c.has(user_spoc_dob))? c.getString(user_spoc_dob):空;
                    wifename =(c.has(spouse_name))? c.getString(spouse_name):空;
                    串mothersbday =(c.has(user_mother_dob))? c.getString(user_mother_dob):空;
                    mothersname =(c.has(mother_name))? c.getString(mother_name):空;
                    串fatherbday =(c.has(user_father_dob))? c.getString(user_father_dob):空;
                    fathername =(c.has(father_name))? c.getString(father_name):空;
                    串ownbbday =(c.has(user_dob))? c.getString(user_dob):空;
                    ownname =(c.has(user_fname))? c.getString(user_fname):空;
                    OCCS =(c.has(OCC))? c.getString(OCC):空;
                    IDS =(c.has(friend_id))? c.getString(friend_id):空;
                    的System.out.println(BDAY+ wifebday + mothersbday + fatherbday + ownbbday);
                    // if测试,以避免增加空值
                    如果(wifename!= NULL)statedata.add(wifename);
                    如果(mothersbday!= NULL)statedata.add(mothersname);
                    如果(fatherbday!= NULL)statedata.add(fathername);
                    如果(ownbbday!= NULL)statedata.add(ownname);
                    如果(!OCCS = NULL)testlist.add(OCCS);
                }            }赶上(JSONException E){
                e.printStackTrace();
            }
        }其他{
            Log.e(的ServiceHandler,无法从URL得到任何数据);
        }
        返回statedata;
    }
    保护无效onPostExecute(ArrayList的<串GT;的结果){
        super.onPostExecute(结果);        如果(pDialog.isShowing())
            pDialog.dismiss();       ALIST =新的ArrayList<串GT;();
        aList.addAll(结果);
        适配器=新CustomAdapterGiftsharealert(TestFamilyocaasion.this,结果);
        listviw.setAdapter(适配器);
        adapter.notifyDataSetChanged();    }}

适配器

 私有类CustomAdapterGiftsharealert延伸BaseAdapter {
    //的String []的结果;
    上下文语境;
    // INT [] imageId;
    私人的ArrayList<串GT;的ListData;
    私人LayoutInflater吹气= NULL;
    公共CustomAdapterGiftsharealert(上下文mainActivity,ArrayList的<串GT;的ListData){
        // TODO自动生成构造函数存根
        上下文= mainActivity;
        this.listData =的ListData;        充气=(LayoutInflater)上下文。
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回listData.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    公共类持有人
    {
        TextView的电视;
        ImageView的IMG;
        TextView的消息;
        TextView的点;
    }
    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        // TODO自动生成方法存根
        最终持有人持有人=新的持有人();
        查看rowView;
        rowView = inflater.inflate(R.layout.list_item_frndsfmly,NULL);
        holder.tv =(TextView中)rowView.findViewById(R.id.frndsfamly_name);        返回rowView;
    }}


解决方案

Because have following issues in current code:

1. Not initializing ArrayList before adding items in it. do it as:

private ArrayList<String> statedata=new ArrayList<String>();

2. Because problem is getView method :

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ...
        holder.tv=(TextView) rowView.findViewById(R.id.frndsfamly_name);
        //set text to TextView from listData
        holder.tv.setText(listData.get(position));
        return rowView;
    }

这篇关于如何显示listiview不同的JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:07