3

I am using json parsing in my app,i am getting following response,and i want to display name from my response to listiview,but my app got crash,can any one tell me what is the mistake

[
        {
            "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"
        }
    ]

Mainactiviyt.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();

    }

}

Adapter

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

}
albert
  • 251
  • 5
  • 20

2 Answers2

1

listiview nothing is displaying

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;
    }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

In your getView() method

holder.tv.setText(listData.get(position));

is missing. You don't set any text in text view. That is why it is empty

Wasim Reza
  • 136
  • 6