android - 如何在 ListView 中显示不同的 json 字符串?

标签 android json android-listview

我在我的应用程序中使用 json 解析,我收到以下响应,我想显示我对 listiview 的响应中的名称,但是我的应用程序崩溃了,谁能告诉我这是什么错误

[
        {
            "friend_id": "1",
            "user_spoc_dob": "1993-11-11",
            "status": "true",
            "spouse_name": "lily",
            "occ": "spoc"
        },
        {
            "friend_id": "1",
            "user_mother_dob": "1974-12-12",
            "status": "true",
            "mother_name": "sita",
            "message": "Address details Not available",
            "occ": "mom"
        },
        {
            "friend_id": "1",
            "user_father_dob": "1972-11-19",
            "status": "true",
            "father_name": "ram",
            "message": "Address details Not available",
            "occ": "dad"
        },
        {
            "friend_id": "1",
            "user_dob": "1994-02-20",
            "status": "true",
            "user_fname": "Su",
            "occ": "spl"
        }
    ]

主要 Activity .java

class LoadAllStates extends
        AsyncTask<String, String, ArrayList<String>> {
    private ProgressDialog pDialog;
    private String test;
    private ArrayList<String> testlist;
    private String oocs;

    private String ids;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(TestFamilyocaasion.this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected ArrayList<String> doInBackground(
            String... args) {
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        statedata = new ArrayList<String>();
        testlist=new ArrayList<String>();
        String jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                jsonObj = new JSONArray(jsonStr);
                for (int i = 0; i < jsonObj.length(); i++) {
                    JSONObject c = jsonObj.getJSONObject(i);
                    String wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null;
                    wifename= (c.has("spouse_name")) ? c.getString("spouse_name") : null;
                    String mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null;
                    mothersname= (c.has("mother_name")) ? c.getString("mother_name") : null;
                    String fatherbday= (c.has("user_father_dob")) ? c.getString("user_father_dob") : null;
                    fathername= (c.has("father_name")) ? c.getString("father_name") : null;
                    String ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null;
                    ownname= (c.has("user_fname")) ? c.getString("user_fname") : null;
                    occs= (c.has("occ")) ? c.getString("occ") : null;
                    ids= (c.has("friend_id")) ? c.getString("friend_id") : null;
                    System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);
                    // if test, to avoid adding null values
                    if(wifename!=null) statedata.add(wifename);
                    if(mothersbday!=null) statedata.add(mothersname);
                    if(fatherbday!=null) statedata.add(fathername);
                    if(ownbbday!=null) statedata.add(ownname);
                    if(occs!=null) testlist.add(occs);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return statedata;
    }
    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);

        if (pDialog.isShowing())
            pDialog.dismiss();

       aList = new ArrayList<String>();
        aList.addAll(result);
        adapter = new CustomAdapterGiftsharealert(TestFamilyocaasion.this, result);
        listviw.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

}

适配器

private class CustomAdapterGiftsharealert extends BaseAdapter {
    //  String [] result;
    Context context;
    // int [] imageId;
    private ArrayList<String> listData;
    private LayoutInflater inflater=null;
    public CustomAdapterGiftsharealert(Context mainActivity, ArrayList<String> listData) {
        // TODO Auto-generated constructor stub
        context=mainActivity;
        this.listData=listData;

        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
        TextView message;
        TextView points;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.list_item_frndsfmly, null);
        holder.tv=(TextView) rowView.findViewById(R.id.frndsfamly_name);



        return rowView;
    }

}

最佳答案

listiview nothing is displaying

因为当前代码中存在以下问题:

1. 在 ArrayList 中添加项目之前未对其进行初始化。这样做:

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

2. 因为问题是 getView 方法:

@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;
    }

关于android - 如何在 ListView 中显示不同的 json 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914873/

相关文章:

java - Thrift - 从简单的 JSON 转换

sql - 如何将具有树结构的表聚合到单个嵌套 JSON 对象?

Android Listview Item-左右滑动得到一个TimePicker和DatePicker

java - 从gradle的任何进程中排除一些文件夹和文件

android - 以编程方式在 View 上添加填充

android - 无法解析目标 'Google Inc.:Google APIs:15'

php - 如何使用 nl2br 删除\n和添加中断

android - Xamarin Forms 主详细信息页面将返回按钮更改为汉堡菜单

android - 在 ListView Android 中隐藏和显示 View

java - 如何将单击的 ListView 项目的文本发送到我的新 Activity ?