Posts
Q&A
Store๊ฐ null์ด ๋์ต๋๋ค
์ฐพ์๋ค์.. dataํด๋์ค Stores s๊ฐ ๋๋ฌธ์์๋ค์..
- 0
- 3
- 280
Q&A
Store๊ฐ null์ด ๋์ต๋๋ค
class MainViewModel : ViewModel() { val itemLiveData = MutableLiveDataListStore>>() val loadingLiveData = MutableLiveDataBoolean>() private val service : MaskService init { val retrofit = Retrofit.Builder() .baseUrl(MaskService.BASE_URL) .addConverterFactory(MoshiConverterFactory.create()) .build() service = retrofit.create(MaskService::class.java) fetchStoreInfo() } fun fetchStoreInfo() { loadingLiveData.value = true viewModelScope.launch { val storeInfo = service.fetchStoreInfo(37.188078, 127.043002) Log.e("tag", "info "+ storeInfo.Stores) itemLiveData.value = storeInfo.Stores loadingLiveData.value = false } }} ํด๋น๊ณผ๊ฐ์ด Mutable๋ฐLiveData๋ก ์ด๊ธฐํ ํ๊ณ ์๊ณ location์ ๊ฐ์๋๋ก ๊ณ ์ ์๋๊ฒฝ๋ ๋ฃ์ด์ฃผ๊ณ ์์ต๋๋ค (๊ฐ์์์์ฒ๋ผ ์ฝํ๋ฆฐ์์ ํ์ฌ ์๋๊ฒฝ๋ ์์จ์ ๊ฐ์๋๋ก ์์ฐ๊ณ ์ ๋ ๊ฒ ๋ฃ์ด๋จ์ต๋๋ค!) ์ ๊ธฐ info๋ก ๋ก๊ทธ์ฐ์ ๊ณณ์ null์ด๋์ต๋๋ค (itemLiveData๋ null ๋ก ๋ค์ด๊ฐ๊ฒ ๋ค์..) storeInfo.count๋ก ํ๋๊ฒฝ์ฐ์๋ count๊ฐ 222๋ก ์๋์ค๋ฉฐ storeInfo.Stores๋ง ์๊พธ null์ด ๋์ต๋๋ค
- 0
- 3
- 280
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
๋ทฐ๋ชจ๋ธ ์์ฑ์์์ performAction ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ์์ด์ ๊ทธ๋ฌ๋๊ฑด ํ์ธ๋์ ํด๊ฒฐ์ ํ๋๋ฐ if (location != null) { location.setLatitude(37.188078); location.setLongitude(127.043002); viewModel.location = location; viewModel.fetchStoreInfo();} ์ด๋ ๊ฒ setlocationํด๋ ๊ทธ json์ ์๋๊ฑฐ ๊ทธ๋๋ก ํ์ฑ์ด๋๋ค์ ํ์ฌ์์น๋ก ์๋๊ณ .. ์ํ json์ด๋ผ๊ทธ๋ฐ๊ฑด๊ฐ์?
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
๋ต ๊ฐ์ฌํฉ๋๋ค
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
์ฌ๋ ค์ฃผ์ ๊นํ์ main์ด๋ ๋น๊ตํ๊ณ performAction() ๋ฉ์๋๋ถ๋ถ์ ๋ณต๋ถ๊น์งํด๋ดค๋๋ฐ๋ ์ฌ๊ธฐ๋จผ์ ์ํ๊ณ ๋ทฐ๋ชจ๋ธ์ ํ๋ค์.. ์ ์ฌ๋ฆฐ ์ฝ๋ ํ๋ฒ ๋ณต๋ถํด์ ํ์ธํด์ฃผ์ค์์๋์?
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
๋ฆฌํ๋ ์๋ฒํผ ์๋๋ฅด๊ณ ์ฑ ์ง์ ํ์๋ง์ ์ฃฝ์ต๋๋ค ใ ใ ๊ทธ๋์ fecthStoreInfo์์ ๋์ฒดํฌ๋ ํด๋ดค๋๋ฐ ๊ทธ๋ฅ ์๋ฌด๊ฒ๋ ์๋์ค๋๋ผ๊ตฌ์ fail ๋ฆฌ์ค๋๋ ๋ฃ์ด๋ดค์๋๋ฐ success๋ก ํ๋๋ค
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
๊ทธ๋์ ๊ฐ์์ ์๋๊ฒ์ฒ๋ผ setlocation์ผ๋ก ์๋๊ฒฝ๋ ์ค๋ viewmodel์์๋ null์ด ์ฐํ๋๋ค
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
๋ฉ์ธ์ viewmodel.location = location ์ด๋ถ๋ถ์ด๋ service.fetchStoreInfo(location.getLatitude(), location.getLongitude()).enqueue(new Callback() { ์ด๋ถ๋ถ์ ๋ธ๋ ์ดํฌํฌ์ธํธ ์ก๊ณ ๋๋ ค๋ณด๋ฉด service.fetchStoreInfo(location.getLatitude(), location.getLongitude()).enqueue(new Callback() { ์ฌ๊ธฐ๋ถ๋ถ์ด ๋จผ์ ๋๋ฉด์ null์ ์ฐ์ด์ ์ฑ์ด ์ฃฝ๊ณ viewmodel.location = location ์ฌ๊ธฐ๋ ์์ ํ์ง๋ ์๋๋ผ๊ตฌ์.. ๋ ํธ๋กํ ํต์ ๋ถ๋ถ์ ์ฃผ์ํ๋ฉด location ๋ค์ด๊ฐ๊ฑด ํ์ธ๋ฉ๋๋ค
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
public class MainViewModel extends ViewModel { public MutableLiveDataListStore>> itemLiveData = new MutableLiveData(); public Location location; private Retrofit retrofit = new Retrofit.Builder() .baseUrl(MaskService.BASE_URL) .addConverterFactory(MoshiConverterFactory.create()) .build(); private MaskService service = retrofit.create(MaskService.class); public MainViewModel(){ fetchStoreInfo(); } public void fetchStoreInfo() { service.fetchStoreInfo(location.getLatitude(), location.getLongitude()).enqueue(new CallbackStoreInfo>() { @Override public void onResponse(CallStoreInfo> call, ResponseStoreInfo> response) { ListStore> items = response.body().getStores() .stream() .filter(item -> item.getRemainStat() != null) .collect(Collectors.toList()); itemLiveData.postValue(items); } @Override public void onFailure(CallStoreInfo> call, Throwable t) { Log.e("tag", "fail ", t); itemLiveData.postValue(Collections.emptyList()); } }); }}
- 0
- 14
- 702
Q&A
์ค์ ์๋๊ฒฝ๋ ๊ฐ์ ธ์ฌ๋ ๋ํฌ์ธํฐ๋ฉ๋๋ค
์์ง๋ ํด๊ฒฐ์ ์ฐพ์ง ๋ชปํด์ ๋ต๊ธ๋ต๋๋ค ใ ใ ์ ์ฒด์์ค๋ฅผ ์ฌ๋ ค๋ณด๊ฒ ์ต๋๋ค (๋ฉ์ธ, ๋ทฐ๋ชจ๋ธ๋ง์ ์ด๋ํฐ๋ ๋ฐ๋ก ๋ถ๋ฆฌํด์ ๋ฉ์ธ์ ์์ต๋๋ค) public class MainActivity extends AppCompatActivity { private MainViewModel viewModel; private FusedLocationProviderClient fusedLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewModel = new ViewModelProvider(this).get(MainViewModel.class); fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); PermissionListener permissionlistener = new PermissionListener() { @Override public void onPermissionGranted() { performAction(); } @Override public void onPermissionDenied(ListString> deniedPermissions) { } }; TedPermission.with(this) .setPermissionListener(permissionlistener) .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]") .setPermissions(Manifest.permission.ACCESS_FINE_LOCATION) .check(); } @SuppressLint("MissingPermission") private void performAction() { fusedLocationClient.getLastLocation() .addOnSuccessListener(this, location -> { if (location != null) { Log.e("tag", "location" + location.getLatitude()); viewModel.location = location; viewModel.fetchStoreInfo(); } }); RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false)); final StoreAdapter adapter = new StoreAdapter(); recyclerView.setAdapter(adapter); // ui ๋ณ๊ฒฝ ๊ฐ์ง viewModel.itemLiveData.observe(this, stores -> { adapter.updateItems(stores); getSupportActionBar().setTitle("๋ง์คํฌ ์ฌ๊ณ ์๋ ๊ณณ : " + stores.size()); }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.action_refresh: viewModel.fetchStoreInfo(); return true; default: return super.onOptionsItemSelected(item); } }}
- 0
- 14
- 702