2

Hello to all android folks over there!! I want to get list of objects from web service and want to display them in list view.Now i am able to fetch those values and collected them in arraylist.But i am facing problem to display them in list view.below is my code.

Using everyones suggestion ,i solved my problem.Thats the spirit of android buddies.I am pasting my answer in UPDATED block.Hope it will be helpful in future.

UPDATED

     public class TabFragment2 extends android.support.v4.app.Fragment {
    ListView FacultyList;
    View rootView;
    LinearLayout courseEmptyLayout;

    FacultyListAdapter facultyListAdapter;

    String feedbackresult,programtype,programname;
    Boolean FeedBackResponse;

    String FacultiesList[];
   public ArrayList<Faculty> facultylist = new ArrayList<Faculty>();

    SharedPreferences pref;
    FacultyListAdapter adapter;
    SessionSetting session;

    public TabFragment2(){
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getActivity().getSharedPreferences("prefbook", getActivity().MODE_PRIVATE);
        programtype = pref.getString("programtype", "NOTHINGpref");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        rootView = inflater.inflate(R.layout.activity_studenttab2, container, false);
        session = new SessionSetting(getActivity());

        new FacultySyncerBg().execute("");

        courseEmptyLayout = (LinearLayout) rootView.findViewById(R.id.feedback_empty_layout);

        FacultyList = (ListView) rootView.findViewById(R.id.feedback_list);


        facultyListAdapter = new FacultyListAdapter(getActivity());
       FacultyList.setEmptyView(rootView.findViewById(R.id.feedback_list));
        FacultyList.setAdapter(facultyListAdapter);

        return rootView;
    }


    public class FacultyListAdapter extends BaseAdapter {
       private final Context context;

        public FacultyListAdapter(Context context) {

            this.context = context;

            if (!facultylist.isEmpty())
                courseEmptyLayout.setVisibility(LinearLayout.GONE);
        }


        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            final ViewHolder TabviewHolder;


            if (convertView == null) {

                TabviewHolder = new ViewHolder();


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

                convertView = inflater.inflate(R.layout.list_item_feedback,
                        parent, false);

                TabviewHolder.FacultyName = (TextView) convertView.findViewById(R.id.FacultyName);//facultyname

                TabviewHolder.rating = (RatingBar) convertView.findViewById(R.id.rating);//rating starts
                TabviewHolder.Submit = (Button) convertView.findViewById(R.id.btnSubmit);

                // Save the holder with the view
                convertView.setTag(TabviewHolder);
            } else {


                TabviewHolder = (ViewHolder) convertView.getTag();
            }


            final Faculty mFac = facultylist.get(position);//*****************************NOTICE
            TabviewHolder.FacultyName.setText(mFac.getEmployeename());
         //   TabviewHolder.ModuleName.setText(mFac.getSubject());


            TabviewHolder.rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating,
                                            boolean fromUser) {

                    feedbackresult =String.valueOf(rating);

                }
            });


            return convertView;
        }

        @Override
        public int getCount() {
            return facultylist.size();
        }

        @Override
        public Object getItem(int position) {return facultylist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    static class ViewHolder {
        TextView FacultyName;
        RatingBar rating;
        Button Submit;
    }

    private class FacultySyncerBg extends AsyncTask<String, Integer, Void> {
        ProgressDialog progressDialog;
        @Override
        protected void onPreExecute() {
            progressDialog= ProgressDialog.show(getActivity(), "Faculty Feedback!","Fetching Faculty List", true);

        }

        @Override
        protected Void doInBackground(String... params) {

            //CALLING WEBSERVICE
            Faculty(programtype);


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            /*if (FacultyList.getAdapter() != null) {
                if (FacultyList.getAdapter().getCount() == 0) {

                    FacultyList.setAdapter(facultyListAdapter);
                } else
                {
                    facultyListAdapter.notifyDataSetChanged();
                }
            } else {
                FacultyList.setAdapter(facultyListAdapter);
            }


            progressDialog.dismiss();*/

            if (!facultylist.isEmpty()) {
              //  FacultyList.setVisibiltity(View.VISIBLE) ;
                courseEmptyLayout.setVisibility(LinearLayout.GONE);

                if (FacultyList.getAdapter() != null)
                {
                    if (FacultyList.getAdapter().getCount() == 0)
                    {
                        FacultyList.setAdapter(facultyListAdapter);
                    }
                    else
                    {
                        facultyListAdapter.notifyDataSetChanged();
                    }
                }
                else
                {
                    FacultyList.setAdapter(facultyListAdapter);
                }
            }else
            {
                courseEmptyLayout.setVisibility(LinearLayout.VISIBLE);
               // FacultyList.setVisibiltity(View.GONE) ;

            }



            progressDialog.dismiss();

        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isResumed()) {

            new FacultySyncerBg().execute("");
        }
    }//end*


    //**************************WEBSERVICE CODE***********************************

    public void Faculty(String programtype)

    {

        String URL ="http://detelearning.cloudapp.net/det_skill_webservice/service.php?wsdl";
        String METHOD_NAMEFACULTY = "getUserInfo";
        String NAMESPACEFAC="http://localhost", SOAPACTIONFAC="http://detelearning.cloudapp.net/det_skill_webservice/service.php/getUserInfo";


        String faculty[]=new String[4];//changeit

        String webprogramtype="flag";

        String programname="DESHPANDE SUSANDHI ELECTRICIAN FELLOWSHIP";
        // Create request
        SoapObject request = new SoapObject(NAMESPACEFAC, METHOD_NAMEFACULTY);


        request.addProperty("fellowshipname", programname);
        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        // Set output SOAP object
        envelope.setOutputSoapObject(request);

        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {

            //my code Calling Soap Action
            androidHttpTransport.call(SOAPACTIONFAC, envelope);

          //  ArrayList<Faculty> facultylist = new ArrayList<Faculty>();

            java.util.Vector<SoapObject> rs = (java.util.Vector<SoapObject>) envelope.getResponse();

            if (rs != null)
            {
                for (SoapObject cs : rs)
                {
                    Faculty rp = new Faculty();



                    rp.setEmployeename(cs.getProperty(0).toString());//program name
                    rp.setEmployeeid(cs.getProperty(1).toString());//employee name


                    facultylist.add(rp);
                }
            }


        }
        catch (Exception e) {


            e.printStackTrace();
        }


    }

}
2
  • 1
    Why ArrayList<Faculty> facultylist = new ArrayList<Faculty>(); line in getView method. use same facultylist object in which adding items in Faculty Commented Dec 22, 2016 at 6:08
  • yaeh i just added it for safer side.i will remove it. Commented Dec 22, 2016 at 6:14

2 Answers 2

2
if (lstView.getAdapter() != null) {
    if (lstView.getAdapter().getCount() == 0) {
        lstView.setAdapter(finalAdapter);
    } else {
        finalAdapter.notifyDataSetChanged();
    }
} else {
    lstView.setAdapter(finalAdapter);
}

and setVisibiltity(View.VISIBLE)for listview

Put this code here

 @Override
 protected void onPostExecute(Void result) {
if (!facultylist.isEmpty()) {
 FacultyList.setVisibiltity(View.VISIBLE) ;
courseEmptyLayout.setVisibility(LinearLayout.GONE);
    if (FacultyList.getAdapter() != null) {
                    if (FacultyList.getAdapter().getCount() == 0) {
                        FacultyList.setAdapter(facultyListAdapter);
                    } else {
                        facultyListAdapter.notifyDataSetChanged();
                    }
                } else {
                    FacultyList.setAdapter(facultyListAdapter);
                }
}else{
 courseEmptyLayout.setVisibility(LinearLayout.VISIBLE);
FacultyList.setVisibiltity(View.GONE) ;

}



    progressDialog.dismiss();

}
Sign up to request clarification or add additional context in comments.

10 Comments

should i edit your suggestion in my getView() method?.I got confused can u explain me little!
I modified my answer, and commented where you need to modify your code.
Dkb,setAdapter and getAdapter showing error?what are they??
They are listview methods, call them using ListView object which is in your case ListView FacultyList, so use FacultyList.getadapter() & FacultyList.setadapter()
But still my listview is not displaying.Is ther ll be other way?? ;-(
|
0

you can try this:

this is the adapter class code.

public class CustomTaskHistory extends ArrayAdapter<String> {

    private Activity context;
    ArrayList<String> listTasks = new ArrayList<String>();

    String fetchRefID;
    StringBuilder responseOutput;
    ProgressDialog progress;
    String resultOutput;


    public String getFetchRefID() {
        return fetchRefID;
    }

    public void setFetchRefID(String fetchRefID) {
        this.fetchRefID = fetchRefID;
    }


    public CustomTaskHistory(Activity context, ArrayList<String> listTasks) {
        super(context, R.layout.content_main, listTasks);
        this.context = context;
        this.listTasks = listTasks;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_task_history, null, true);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        LinearLayout linearLayout = (LinearLayout) listViewItem.findViewById(R.id.firstLayout);


        //System.out.println("client_id" + _clientID);
        //TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.textViewDesc);
        //ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
        if (position % 2 != 0) {
            linearLayout.setBackgroundResource(R.color.sky_blue);


        } else {
            linearLayout.setBackgroundResource(R.color.white);

        }

        textViewName.setText(listTasks.get(position));


        return listViewItem;
        }
    }

and now in the parent class you must have already added a list view in your xml file so now display code for it is below:

CustomTaskHistory customList = new CustomTaskHistory(TaskHistory.this, task_history_name);

            listView = (ListView) findViewById(R.id.listView);
            listView.setAdapter(customList);

you can also perform any action on clicking cells of listview.If needed code for it is below add just below the above code:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent nextScreen2 = new Intent(getApplicationContext(), SubscribeProgrammes.class);
                nextScreen2.putExtra("CLIENT_ID", _clientID);
                nextScreen2.putExtra("REFERENCE_ID", reference_IDs.get(i));
                startActivity(nextScreen2);

                Toast.makeText(getApplicationContext(), "You Clicked " + task_list.get(i), Toast.LENGTH_SHORT).show();
            }
        });

3 Comments

Thanks for reply @Apoorv Malhotra.I am looking into your code.I am little confused.Can u suggest me what mistake i am doing in my code?!1!
what is the actual problem you are facing ?
my listview not displaying Apoorv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.