关闭自定义对话框后,listview在片段中不刷新?

时间:2017-09-16 08:36:10

标签: android listview android-fragments

解雇对话框后,有人能解决我的ListView没有刷新吗?

这是我的片段。我设置了适配器和ListView。

public class About extends Fragment {

MyRatingAdapter myRatingAdapter;
ListView listView;
List<ReviewRatingBean> reviewratinglist = new ArrayList<>();
Button buttonsubmit;
EditText editText_name, editText_text;
RatingBar ratingBar;
DataBaseHandler dataBaseHandler;
Button btn_add_reviews;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
    //returning our layout file
    //change R.layout.yourlayoutfilename for each of your fragments
    View view = inflater.inflate(R.layout.about_fragment, container, false);
    //  ratingbar1=(RatingBar)view.findViewById(R.id.ratingBar1);
    // button=(Button)view.findViewById(R.id.button1);
    dataBaseHandler = new DataBaseHandler(getActivity());
    listView = (ListView) view.findViewById(R.id.list_item);
    reviewratinglist = dataBaseHandler.GetAllData();
    myRatingAdapter = new MyRatingAdapter(getActivity(),  reviewratinglist);

    listView.setAdapter(myRatingAdapter);

    btn_add_reviews = (Button)view.findViewById(R.id.btn_addreviews);
    btn_add_reviews.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public void onClick(View view) {
            LayoutInflater inflater = getLayoutInflater(savedInstanceState);
            View dialoglayout = inflater.inflate(R.layout.activity_review_rating, null);
            AlertDialog.Builder  builder = new AlertDialog.Builder(getActivity());
            builder.setView(dialoglayout);
            final AlertDialog alertDialog = builder.create();
            buttonsubmit = (Button)dialoglayout.findViewById(R.id.btn_submit);
            editText_name = (EditText)dialoglayout.findViewById(R.id.et_name);
            editText_text = (EditText)dialoglayout.findViewById(R.id.et_text);
            ratingBar = (RatingBar)dialoglayout.findViewById(R.id.rating_bar);
            ratingBar.setIsIndicator(false);

            buttonsubmit.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    ReviewRatingBean reviewRatingBean = new ReviewRatingBean();
                    String name = editText_name.getText().toString();
                    float rate = ratingBar.getRating();
                    String text = editText_text.getText().toString();

                    reviewRatingBean.setName(name);
                    reviewRatingBean.setRating(rate);
                    reviewRatingBean.setTextreview(text);

                    dataBaseHandler.addData(reviewRatingBean);

                    Toast.makeText(getContext(), "Thanks For Your FeedBack", Toast.LENGTH_SHORT).show();

                    myRatingAdapter.notifyDataSetChanged(); here i have try to notify the base adpater to refreshing a list view
                    listView.setAdapter(myRatingAdapter);

                    alertDialog.dismiss();
                }
            });

           alertDialog.show();
        }

    });

     return view;
}
}

3 个答案:

答案 0 :(得分:1)

您永远不会向适配器提供任何新数据。 像

这样的东西
reviewratinglist = dataBaseHandler.GetAllData();
myReviewAdapter.setReviewRatings(reviewRatingList); 
onClick()中的

应该可以解决问题。

答案 1 :(得分:1)

不要再使用适配器类只需添加以下

@Override
            public void onClick(View view) {

                ReviewRatingBean reviewRatingBean = new ReviewRatingBean();
                String name = editText_name.getText().toString();
                float rate = ratingBar.getRating();
                String text = editText_text.getText().toString();

                reviewRatingBean.setName(name);
                reviewRatingBean.setRating(rate);
                reviewRatingBean.setTextreview(text);

                dataBaseHandler.addData(reviewRatingBean);

                Toast.makeText(getContext(), "Thanks For Your FeedBack", Toast.LENGTH_SHORT).show();


                dataBaseHandler.GetAllData();  // Add this line


                myRatingAdapter.notifyDataSetChanged(); // just use notifiydataset only

                alertDialog.dismiss();
            }
        });

答案 2 :(得分:0)

在对话框onClick()中,您刚刚将更改保存到数据库,但未使用适配器数据进行更新。

您的对话框onClick()应该是这样的:

                @Override
                public void onClick(View view) {

                    ReviewRatingBean reviewRatingBean = new ReviewRatingBean();
                    String name = editText_name.getText().toString();
                    float rate = ratingBar.getRating();
                    String text = editText_text.getText().toString();

                    reviewRatingBean.setName(name);
                    reviewRatingBean.setRating(rate);
                    reviewRatingBean.setTextreview(text);

                    dataBaseHandler.addData(reviewRatingBean);

                    Toast.makeText(getContext(), "Thanks For Your FeedBack", Toast.LENGTH_SHORT).show();

                    reviewratinglist.add(reviewRatingBean)  // add this line

                    myRatingAdapter.notifyDataSetChanged(); here i have try to notify the base adpater to refreshing a list view
                    listView.setAdapter(myRatingAdapter);

                    alertDialog.dismiss();
                }
            });