-2

Hello all I am new to android, am trying to display a list of json data in listview my class is extended with SherlockFragment fetched the data succesfully but unable to append the data in a list. for displaying of data I created a class which extending with baseadapter class. i have found that value is returning null at View group

Can any one help in fixing the issue thanks in advance

public class AboutMeFragment extends Fragment { 

ListViewAdapter la;
String add_users;
View rootView;
ArrayList<HashMap<String, String>> contactList;
static String idStr,getMaxAnsLengthStr,userIdStr,userRankStr,de;

static String getQuestionsStr = "GetQuestion";

ListView list;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_aboutme,
            container, false);

    la = new ListViewAdapter(getActivity(), contactList);

    list = (ListView)rootView.findViewById(R.id.listView1);
    contactList = new ArrayList<HashMap<String, String>>();


    new CheckLoginAboutmen().execute();     
    return rootView;
}

private class CheckLoginAboutmen extends AsyncTask<Void,Void,Void>{ 

    String json;
    String outputData = null;
    JSONArray jsonArray;
    @Override
    protected Void doInBackground(Void... args) {
        // TODO Auto-generated method stub

        List<NameValuePair> params = new ArrayList<NameValuePair>(1);

        //      params.add(new BasicNameValuePair("LoginName", loginStr));
        params.add(new BasicNameValuePair("UserID", "195"));
        ServiceHandler sh = new ServiceHandler();

        add_users = getString(R.string.about_me);
        json = sh.makeServiceCall(add_users, ServiceHandler.POST,params);
        Log.d("Create Prediction Request: ", "> " + json);

        System.out.println("The returned JSON Value is..."+json);

        try{
            jsonArray = new JSONArray(json);
            if(json != null){
                HashMap<String, String> contact = null;
                System.out.println("The Length of JSON Array is..."+jsonArray.length());
                for(int i =0;i<jsonArray.length();i++){
                    contact = new HashMap<String, String>();
                    JSONObject c = jsonArray.getJSONObject(i);
                    getQuestionsStr = c.getString("GetQuestion");
                    idStr = c.getString("_id");
                    getMaxAnsLengthStr = c.getString("MaxAnswerLength");
                    userIdStr = c.getString("UserId");
                    userRankStr = c.getString("UserRank");

                    System.out.println("The Fetched Id is...."+idStr+"\n"+getQuestionsStr+"\n"+getMaxAnsLengthStr+"\n"+userIdStr+"\n"+userRankStr);

                    contact.put("getQuestionsStr", getQuestionsStr);

                }
                contactList.add(contact);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

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

        //  ListViewAdapter la = new ListViewAdapter(getSherlockActivity().getBaseContext(),contactList);
        //  ListViewAdapter la = new ListViewAdapter(getActivity().getBaseContext(),contactList);

        //list.setAdapter(la);


        list.setAdapter(new ListViewAdapter(getActivity().getBaseContext(),contactList));
    }

}}

And my Base adapter class follows here.....

public class ListViewAdapter extends BaseAdapter{

int tempI =0;
int tempII = 7;

Context context;
ArrayList<HashMap<String, String>> data;
LayoutInflater inflater;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context baseContext,
        ArrayList<HashMap<String, String>> contactList) {
    // TODO Auto-generated constructor stub

    this.context = baseContext;
    data = contactList;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    System.out.println("The Item Value is..."+data.get(position));
    return 0;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub


    View itemView = null;
    TextView textView1_lv_123;


    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       

    itemView = inflater.inflate(R.layout.list_item_view, parent,false);     
    resultp = data.get(position);       

    textView1_lv_123 = (TextView)itemView.findViewById(R.id.textView1_lv_123);      

    // facing issue from here, data is retuning null unable to append the value to text view 
    String s1 = resultp.get("GetQuestion");
    System.out.println("The demo String is....1234 "+s1);

    textView1_lv_123.setText(resultp.get("GetQuestion"));

     // if I am printing only resultp value it is returning the value/result which is an last value of array 

    System.out.println("The Result value is  "+resultp);


    return itemView;
}

}

  • 1
    Is it crashing? Just not able to add data? Your question isn't real clear. What `ViewGroup` are you talking about that is `null`? – codeMagic Mar 25 '15 at 12:56

2 Answers2

0

You should change when you create your ListAdapter , you should create in AsyncTask onPostExecute. Try this:

AboutMeFragment.class:

    public class AboutMeFragment extends Fragment { 

    ListViewAdapter la;
    String add_users;
    View rootView;
    ArrayList<HashMap<String, String>> contactList;
    static String idStr,getMaxAnsLengthStr,userIdStr,userRankStr,de;

    static String getQuestionsStr = "GetQuestion";

    ListView list;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_aboutme,
                container, false);



        list = (ListView)rootView.findViewById(R.id.listView1);
        contactList = new ArrayList<HashMap<String, String>>();


        new CheckLoginAboutmen().execute();     
        return rootView;
    }

    private class CheckLoginAboutmen extends AsyncTask<Void,Void,Void>{ 

        String json;
        String outputData = null;
        JSONArray jsonArray;
        @Override
        protected Void doInBackground(Void... args) {
            // TODO Auto-generated method stub

            List<NameValuePair> params = new ArrayList<NameValuePair>(1);

            //      params.add(new BasicNameValuePair("LoginName", loginStr));
            params.add(new BasicNameValuePair("UserID", "195"));
            ServiceHandler sh = new ServiceHandler();

            add_users = getString(R.string.about_me);
            json = sh.makeServiceCall(add_users, ServiceHandler.POST,params);
            Log.d("Create Prediction Request: ", "> " + json);

            System.out.println("The returned JSON Value is..."+json);

            try{
                jsonArray = new JSONArray(json);
                if(json != null){
                    HashMap<String, String> contact = null;
                    System.out.println("The Length of JSON Array is..."+jsonArray.length());
                    for(int i =0;i<jsonArray.length();i++){
                        contact = new HashMap<String, String>();
                        JSONObject c = jsonArray.getJSONObject(i);
                        getQuestionsStr = c.getString("GetQuestion");
                        idStr = c.getString("_id");
                        getMaxAnsLengthStr = c.getString("MaxAnswerLength");
                        userIdStr = c.getString("UserId");
                        userRankStr = c.getString("UserRank");

                        System.out.println("The Fetched Id is...."+idStr+"\n"+getQuestionsStr+"\n"+getMaxAnsLengthStr+"\n"+userIdStr+"\n"+userRankStr);

                        contact.put("getQuestionsStr", getQuestionsStr);

                    }
                    contactList.add(contact);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) 
        {
            super.onPostExecute(result);

            //  ListViewAdapter la = new ListViewAdapter(getSherlockActivity().getBaseContext(),contactList);
            //  ListViewAdapter la = new ListViewAdapter(getActivity().getBaseContext(),contactList);

            //list.setAdapter(la);
            if(contactList!=null && contactList.size()>0{

                 la =new ListViewAdapter(getActivity().getBaseContext(),contactList));
                 list.setAdapter(la);

            }else{
              // handle no data Example:show toast "contactList have no data"
            }

}}

And you should change in adapter the method getItem(). Try this:

BaseAdapter.class:

public class ListViewAdapter extends BaseAdapter{

int tempI =0;
int tempII = 7;

Context context;
ArrayList<HashMap<String, String>> data;
LayoutInflater inflater;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context baseContext,
        ArrayList<HashMap<String, String>> contactList) {
    // TODO Auto-generated constructor stub

    this.context = baseContext;
    data = contactList;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    System.out.println("The Item Value is..."+data.get(position));
    return data.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub


    View itemView = null;
    TextView textView1_lv_123;


    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       

    itemView = inflater.inflate(R.layout.list_item_view, parent,false);     
    resultp = data.get(position);       

    textView1_lv_123 = (TextView)itemView.findViewById(R.id.textView1_lv_123);      

    // facing issue from here, data is retuning null unable to append the value to text view 
    String s1 = resultp.get("GetQuestion");
    System.out.println("The demo String is....1234 "+s1);

    textView1_lv_123.setText(resultp.get("GetQuestion"));

     // if I am printing only resultp value it is returning the value/result which is an last value of array 

    System.out.println("The Result value is  "+resultp);


    return itemView;
}
encastellano
  • 435
  • 3
  • 5
0

Hi Aditya check the string declaration in AboutmeFragment page I hope they are overlapping your code