Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓回收器视图在我单击按钮时未更新_Android_Android Recyclerview_Android Adapter_Android Button_Buttonclick - Fatal编程技术网

Android 安卓回收器视图在我单击按钮时未更新

Android 安卓回收器视图在我单击按钮时未更新,android,android-recyclerview,android-adapter,android-button,buttonclick,Android,Android Recyclerview,Android Adapter,Android Button,Buttonclick,我有这个片段,我有和编辑文本,一个按钮和一个回收视图。当我第一次单击按钮时,它具有预期的行为,但如果我更改编辑文本内容并再次单击按钮,它不会更新我的回收器视图。我做错了什么?因为我在重复这个过程 每次单击都要进行Api调用 碎片 public类SearchFragment扩展片段实现View.OnClickListener{ 私人餐厅; 私人回收视图mRecyclerView; 受保护的静态列表restaurantsList; 私人语境; 私有静态OnRestaurantClikedListen

我有这个片段,我有和编辑文本,一个按钮和一个回收视图。当我第一次单击按钮时,它具有预期的行为,但如果我更改编辑文本内容并再次单击按钮,它不会更新我的回收器视图。我做错了什么?因为我在重复这个过程 每次单击都要进行Api调用

碎片
public类SearchFragment扩展片段实现View.OnClickListener{
私人餐厅;
私人回收视图mRecyclerView;
受保护的静态列表restaurantsList;
私人语境;
私有静态OnRestaurantClikedListener侦听器;
私人消防队;
私有编辑文本关键字;
私有FusedLocationProviderClient mFusedLocationClient;
公共搜索片段(){
}
公共静态OnRestaurantClikedListener getListener(){
返回侦听器;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
context=getContext();
mAuth=FirebaseAuth.getInstance();
restaurantsList=newarraylist();
mAdapter=newrestaurantapter(上下文,restaurantsList,getActivity());
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图mContentView=充气机。充气(R.layout.fragment\u搜索,容器,false);
mRecyclerView=mContentView.findviewbyd(R.id.recycler\u视图);
setLayoutManager(新的LinearLayoutManager(mContentView.getContext());
mRecyclerView.setAdapter(mAdapter);
关键字=mContentView.findViewById(R.id.keyword);
关键字.setOnEditorActionListener(新建TextView.OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView TextView、int actionId、KeyEvent KeyEvent){
if((keyEvent!=null&&keyEvent.getKeyCode()==keyEvent.KEYCODE\u ENTER)| |(actionId==EditorInfo.IME\u ACTION\u SEARCH))
getRestaurants();
返回false;
}
});
ImageButton searchButton=mContentView.findViewById(R.id.search);
searchButton.setOnClickListener(这个);
返回mContentView;
}
@凌驾
恢复时公开作废(){
super.onResume();
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
侦听器=(OnRestaurantClickedListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()+“必须实现OnButtonClicked”);
}
}
@SuppressLint(“丢失许可”)
私人食肆({
mFusedLocationClient=LocationServices.getFusedLocationProviderClient(getActivity());
mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(),new OnSuccessListener()){
@凌驾
成功时的公共无效(最终位置){
如果(位置!=null){
SharedReferences mSettings=PreferenceManager.GetDefaultSharedReferences(上下文);
字符串排序=mSettings.getString(“排序”、“评级”);
字符串顺序=mSettings.getString(“顺序”、“描述”);
double radius=double.parseDouble(mSettings.getString(“radius”,“10”));
半径=半径*1000;
更新zomato.getApi().searchByName(关键字.getText().toString(),位置.getLatitude(),位置.get经度(),
20,半径,排序,顺序,getActivity().getResources().getString(R.string.user_key))
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
如果(restaurantsList.size()!=0){
restaurantsList.clear();
mAdapter.notifyDataSetChanged();
}
List restaurants=response.body().getRestaurants();
对于(int i=0;ipublic class SearchFragment extends Fragment implements View.OnClickListener {

    private RestaurantAdapter mAdapter;
    private RecyclerView mRecyclerView;
    protected static List<Restaurant_> restaurantsList;
    private Context context;
    private static OnRestaurantClickedListener listener;
    private FirebaseAuth mAuth;

    private EditText keyword;

    private FusedLocationProviderClient mFusedLocationClient;


    public SearchFragment() {
    }

    public static OnRestaurantClickedListener getListener() {
        return listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
        mAuth = FirebaseAuth.getInstance();
        restaurantsList = new ArrayList<>();
        mAdapter = new RestaurantAdapter(context, restaurantsList, getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mContentView = inflater.inflate(R.layout.fragment_search, container, false);
        mRecyclerView = mContentView.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContentView.getContext()));
        mRecyclerView.setAdapter(mAdapter);

        keyword = mContentView.findViewById(R.id.keyword);
        keyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if ((keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) || (actionId == EditorInfo.IME_ACTION_SEARCH))
                    getRestaurants();
                return false;
            }
        });

        ImageButton searchButton = mContentView.findViewById(R.id.search);
        searchButton.setOnClickListener(this);

        return mContentView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (OnRestaurantClickedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnButtonClicked");
        }
    }

    @SuppressLint("MissingPermission")
    private void getRestaurants() {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
        mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(final Location location) {
                if (location != null) {
                    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(context);
                    String sort = mSettings.getString("sort", "rating");
                    String order = mSettings.getString("order", "desc");
                    double radius = Double.parseDouble(mSettings.getString("radius", "10"));
                    radius = radius * 1000;
                    RetrofitZomato.getApi().searchByName(keyword.getText().toString(), location.getLatitude(), location.getLongitude(),
                            20, radius, sort, order, getActivity().getResources().getString(R.string.user_key))
                            .enqueue(new Callback<SearchResponse>() {
                                @Override
                                public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) {
                                    if (restaurantsList.size() != 0) {
                                        restaurantsList.clear();
                                        mAdapter.notifyDataSetChanged();
                                    }
                                    List<Restaurant> restaurants = response.body().getRestaurants();
                                    for (int i = 0; i < restaurants.size(); i++) {
                                        double distance = calculateDistance(Double.parseDouble(restaurants.get(i).getRestaurant().getLocation().getLatitude()),
                                                Double.parseDouble(restaurants.get(i).getRestaurant().getLocation().getLongitude()),
                                                location.getLatitude(), location.getLongitude());
                                        distance = (double) Math.round(distance * 100d) / 100d;
                                        restaurants.get(i).getRestaurant().setDistance(distance);
                                        restaurantsList.add(restaurants.get(i).getRestaurant());
                                        mAdapter.notifyItemInserted(i);
                                    }
                                }

                                @Override
                                public void onFailure(Call<SearchResponse> call, Throwable t) {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                    builder.setMessage("Couldn't find any nearby restaurants");
                                    AlertDialog mDialog = builder.create();
                                    mDialog.show();
                                }
                            });
                }
            }
        }).addOnFailureListener(getActivity(), new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getActivity(), "It wasn't possible to determine your location", Toast.LENGTH_LONG).show();
            }
        });
    }

    private double calculateDistance(double latRestaurant, double lonRestaurant, double myLat, double myLon) {
        if ((myLat == latRestaurant) && (myLon == lonRestaurant)) {
            return 0;
        } else {
            double theta = myLon - lonRestaurant;
            double dist = Math.sin(Math.toRadians(myLat)) * Math.sin(Math.toRadians(latRestaurant))
                    + Math.cos(Math.toRadians(myLat)) * Math.cos(Math.toRadians(latRestaurant)) * Math.cos(Math.toRadians(theta));
            dist = Math.acos(dist);
            dist = Math.toDegrees(dist);
            dist = dist * 60 * 1.1515;
            dist = dist * 1.609344;
            return dist;
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.search) {
            getRestaurants();
        }
    }
//    @Override
//    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//        searchViewModel = ViewModelProviders.of(this).get(SearchViewModel.class);
//        // TODO: Use the ViewModel
//    }

}
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());