【问题标题】:listview is not refreshing in fragment after dismiss custom dialog box?关闭自定义对话框后,listview 没有在片段中刷新?
【发布时间】:2017-09-16 08:36:09
【问题描述】:

任何人都可以解决我的 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;
}
}

【问题讨论】:

标签: android listview android-fragments


【解决方案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】:

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

    reviewratinglist = dataBaseHandler.GetAllData();
    myReviewAdapter.setReviewRatings(reviewRatingList); 
    

    在您的onClick() 中应该可以解决问题。

    【讨论】:

      【解决方案3】:

      在您的对话框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();
                      }
                  });
      

      【讨论】:

      • 如果答案帮助您解决了问题,请将其标记为已接受的答案。它会帮助别人。 @palashgour
      猜你喜欢
      • 2020-06-03
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多