android rating bar in listview:How to save rating of multiple list in object?

568 views Asked by At

I am designing faculty feedback activity.Below is my Fragment activity where i get faculty names from webservice as object and dispaly them in listview.And there is Rating bar infront every faculty.

I want to collect value of rating and faculty name in array of object and want to send feedback of every faculty to webservice to store when i click submit button.How can i do it?

FragmentTab2.java

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

    FacultyListAdapter facultyListAdapter;
Button upButton;
    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);
        upButton = (Button) rootView.findViewById(R.id.btnSubmit);

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

    //FEEDBACK SUBMISSION BUTTON
    @Override
    public void onClick(View v) {

        new SendFeedbackSyncerBg().execute("");

    }


    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.rating.setRating(mFac.getRatingStar());
         //   TabviewHolder.ModuleName.setText(mFac.getSubject());


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

                    feedbackresult =String.valueOf(rating);

                    TabviewHolder.rating.setRating(Float.parseFloat(feedbackresult));
                    Log.d("feedback","feedback is: "+ feedbackresult);

                }
            });


            return convertView;
        }

        /*private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position) {
            return new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                    FacultyName item = getItem(position);
                    item.setRatingStar(v);
                    Log.i("Adapter", "star: " + v);
                }
            };
        }*/

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

        }
    }

And here is my object class

Faculty.java

public class Faculty {
    private float ratingStar;
    String employeeid;
    String employeename;

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

    public Faculty()
    {

    }
    public Faculty(int ratingStar,String employeeid,String employeename)
    {
        this.ratingStar = ratingStar;
        this.employeeid =employeeid;
        this.employeename =employeename;
    }

    public float getRatingStar() {
        return ratingStar;
    }

    public void setRatingStar(float ratingStar) {
        this.ratingStar = ratingStar;
    }

    public  void setEmployeeid(String employeeid)
    {
        this.employeeid = employeeid;
    }

    public String getEmployeeid()
    {
        return this.employeeid;
    }

    public  void setEmployeename(String employeename)
    {
        this.employeename = employeename;
    }

    public String getEmployeename()
    {
        return this.employeename;
    }
}

Any kind of knowledge and information will be thankful

EDIT

My list of ratings look like this.

Image

0

There are 0 answers