Задачи, тесты и теоретические вопросы по JAVA.
Репозиторий: https://github.com/proghub-official/java-interview
Прислать задачу/вопрос в дар: @cyberJohnny
Сотрудничество: @cyberJohnny
Post #1519
2.4K
JA @javaquiz
Showing posts older than #1521 · Back to latest
@Override
public void onNext(NumbersResponsePojo numbersResponsePojo) {
Numbers numbers = new Numbers();
numbers.facts = numbersResponsePojo.getText();
numbers.number = numbersResponsePojo.getNumber();
numbers.time = System.currentTimeMillis();
setPojoLiveData(numbersResponsePojo);
insert(numbers);
setIsLoading(false);
}
@Override
public void onError(Throwable e) {
setIsLoading(false);
errorMessageLiveData.postValue("Internet is missing");
}
@Override
public void onComplete() {
}
});
}
public LiveData<NumbersResponsePojo> getPojoLiveData() {
return pojoLiveData;
}
public MutableLiveData<String> getErrorMessageLiveData() {
return errorMessageLiveData;
}
public void setErrorMessageLiveData(String errorMessageLiveData) {
this.errorMessageLiveData.setValue(errorMessageLiveData);
}
public LiveData<List<Numbers>> getDataFromDB() {
return numbers;
}
public MutableLiveData<Void> getShowLoadingLiveData() {
return showLoadingLiveData;
}
public MutableLiveData<Void> getHideLoadingLiveData() {
return hideLoadingLiveData;
}
}
Что не так? Спасибоjava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object referenceРугает за строчки с ProgressBar
at com.example.mvvm_test.view.MainActivity$4.onChanged(MainActivity.java:94)
at com.example.mvvm_test.view.MainActivity$4.onChanged(MainActivity.java:91)
mMyViewModel.getShowLoadingLiveData().observe(this, new Observer<Void>() {
@Override
public void onChanged(Void unused) {
progressBar.setVisibility(View.VISIBLE);
}
});
mMyViewModel.getHideLoadingLiveData().observe(this, new Observer<Void>() {
@Override
public void onChanged(Void unused) {
progressBar.setVisibility(View.GONE);
}
});
public class MyViewModel extends AndroidViewModel {
private final MutableLiveData<NumbersResponsePojo> pojoLiveData = new MutableLiveData<>();
private LiveData<List<Numbers>> numbers = new MutableLiveData<>();
private MutableLiveData <String> errorMessageLiveData = new MutableLiveData<>();
private final MutableLiveData<Void> showLoadingLiveData = new MutableLiveData<>();
private final MutableLiveData<Void> hideLoadingLiveData = new MutableLiveData<>();
private final WebRepo numberRepository;
private final DataBaseRepo dataBaseRepo;
public MyViewModel(Application application, WebRepo numberRepository) {
super(application);
dataBaseRepo = new DataBaseRepo(application);
numbers = dataBaseRepo.getAllFacts();
this.numberRepository = numberRepository;
}
void insert(Numbers numbers) {
dataBaseRepo.insert(numbers);
}
public void setPojoLiveData(NumbersResponsePojo pojoLiveData) {
setIsLoading(true);
this.pojoLiveData.setValue(pojoLiveData);
}
private void setIsLoading(boolean loading) {
if (loading) {
showLoadingLiveData.postValue(null);
} else {
hideLoadingLiveData.postValue(null);
}
}
public void getNumbers(String num) {
numberRepository.getNumbers(num)
.subscribe(new Observer<NumbersResponsePojo>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(NumbersResponsePojo numbersResponsePojo) {
Numbers numbers = new Numbers();
numbers.facts = numbersResponsePojo.getText();
numbers.number = numbersResponsePojo.getNumber();
numbers.time = System.currentTimeMillis();
setPojoLiveData(numbersResponsePojo);
insert(numbers);
setIsLoading(false);
//errorMessageLiveData.postValue("There is not items!");
}
@Override
public void onError(Throwable e) {
setIsLoading(false);
errorMessageLiveData.postValue("Internet is missing");
}
@Override
public void onComplete() {
}
});
}
public void getRandomNumbers(String num) {
numberRepository.getRandomNumbers(num)
.subscribe(new Observer<NumbersResponsePojo>() {
@Override
public void onSubscribe(Disposable d) {
}public class SystemManager {
Rout CI106 = new Rout("WizzAir", "Rome", "Berlin", 1, "06:00", "06:40", 25);
Set<Rout> rout = new HashSet<>();
rout.add(CI106);
Выдает ошибку: Cannot resolve symbol 'add'create table manual_securityдоступа к таблице у меня нет, изменить не могу. т.е. например добавить в нее поле типа int и
(
isin_code varchar(12) not null,
emitent_id numeric,
update_date_time timestamp not null,
constraint manual_security_pk primary key (isin_code)
);
create sequence manual_security_security_id_auto_gen increment 100;
я прочел в документации, что SEQUENCE is used for delegating the identifier generation to a database sequence.@Id
@SequenceGenerator(name = "manual_security_security_id_auto_gen", allocationSize = 100, sequenceName = "manual_security_security_id_auto_gen", initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "manual_security_security_id_auto_gen")
private Integer securityId;
вопрос к знатокам hibernate - можно ли как-то включить batch и добиться быстрого батчевого сохранения строк в таблице где ключем выступает string.@Entityсохарнение saveall коллекции из 4к записей занимает так около 10 минут.
@Table(name = "manual_security")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
@EqualsAndHashCode(of = "isinCode")
public class ManualSecurity {
@Id
@NotNull
@Column(name = "isin_code", nullable = false)
private String isinCode;
// остальные поля
}
public interface ManualSecurityRepository extends JpaRepository<ManualSecurity, String> {
}
jpa:Java задачи с собеседований
show-sql: false
open-in-view: false
database: postgresql
properties:
hibernate:
jdbc:
batch_size: 100
import android.annotation.SuppressLintJava задачи с собеседований
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.annotation.RequiresApi
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@SuppressLint("SetJavaScriptEnabled")
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webViewSetup()
}
@RequiresApi(Build.VERSION_CODES.O)
private fun webViewSetup() {
val wb = findViewById<WebView>(R.id.wb_webView)
wb.webViewClient = WebViewClient()
wb.apply {
settings.javaScriptEnabled = true
settings.safeBrowsingEnabled = true
loadUrl("https://www.google.com/?hl=ru")
}
}
override fun onBackPressed() {
val wb = findViewById<WebView>(R.id.wb_webView)
if (wb.canGoBack()) wb.goBack() else super.onBackPressed()
}
}