当前位置:网站首页>Android Development: the client obtains the latest value in the database in real time and displays it on the interface
Android Development: the client obtains the latest value in the database in real time and displays it on the interface
2022-04-23 18:56:00 【Mug mop fan】
Catalog
summary
I'm writing a program recently , The requirement is that the server-side program will constantly refresh the value of a field in the database , Then you need to write a client program to continuously read the field and display it on the interface . Here the database uses MySQL,Android Data interaction between client and server Volley frame .
Preparation
Volley yes Google Officially launched HTTP Method library , Use Volley The framework needs to be Android Studio Introduce the corresponding package , For details, please refer to my previous articles :
Android Use Volley Frame for data transmission
Main code
The whole implementation logic is that the user clicks “START” after , Start to continuously pull the value of the corresponding field in the database , Also on UI Control . meanwhile , Set flag bit getAgain, Its meaning is as follows :
getAgain = true: The current read operation has completed , Make the next read ;
getAgain = false:Volley The child thread is reading the value of the corresponding field in the database , The read operation is blocked .
The code is as follows :
1. Variable bit declaration :
public static boolean getAgain = true;
2.“START” Button Click event :
mBtnBegin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start a loop refresh
new Thread(new Runnable() {
@Override
public void run() {
// Conduct 1000 Read operations
for(int i = 0; i < 1000; ++i){
// Read the flag bit to judge
if(getAgain){
// Enter the read state
getAgain = false;
// Pull the latest field value from the database
ShowMyCount(getApplicationContext(), LoginActivity.Username);
while (!getAgain){
// Data reading , To block
}
}
}
}
}).start();
}
});
3.“ShowMyCount()” Method :
Use Volley The framework interacts with the server. You can see the above article , For the functions to be realized in this paper , stay “ The query is successful ” Two statements need to be added to the statement block of :
// The query is successful , to update UI Interface
mTvCount.setText(count);
// The data reading is completed , Unblock state
getAgain = true;
Optimize
1. Updated in this article UI The interface is directly in “ShowMyCount()” Method used in setText() Method , but Android In development ,UI The update of the interface usually uses Handler Mechanism .
2. In this paper, the for Cycle... Cycle 1000 Times to roughly realize automatic cyclic reading , But more than 1000 It will not be updated after UI Interface , There are two solutions : Increase the number of cycles or use while loop .
——————————————————————————
Finally, post my official account. : WeChat search “ Tea migration ” Or scan the figure below . Usually update some programming related articles , Welcome to pay attention ~

版权声明
本文为[Mug mop fan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603049963.html
边栏推荐
- Xlslib use
- Practice of Druid SQL and security in meituan review
- 【历史上的今天】4 月 23 日:YouTube 上传第一个视频;网易云音乐正式上线;数字音频播放器的发明者出生
- Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
- [advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
- Using Visual Studio code to develop Arduino
- Sogou cell thesaurus analysis (only extract words and word frequency)
- Ctfshow - web362 (ssti)
- 教你用简单几个步骤快速重命名文件夹名
- c#:泛型反射
猜你喜欢

The fifth bullet of MySQL learning -- detailed explanation of transaction and its operation characteristics

【科普】CRC校验(一)什么是CRC校验?

Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)

STM32: LCD display

Simplified path (force buckle 71)

Query the logistics update quantity according to the express order number

Raspberry pie uses root operation, and the graphical interface uses its own file manager

Teach you to quickly rename folder names in a few simple steps
![[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]](/img/47/521bd7f144b0d6a5759d10067c9bea.png)
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]

Solutions such as unknown or garbled code or certificate problem prompt in Charles's mobile phone packet capture, actual measurement.
随机推荐
Treatment of incomplete display of listview height
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
Use of kotlin collaboration in the project
Summary of actual business optimization scheme - main directory - continuous update
【玩转Lighthouse】腾讯云轻量服务器搭建全平台视频解析视频下载网站
C: generic reflection
ctfshow-web362(SSTI)
22 year flying Book manpower Kit
One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
ctfshow-web362(SSTI)
Screenshot using projectmediamanager
SSDB基础2
从技术体系到商业洞察,中小研发团队架构实践之收尾篇
2022.04.23(LC_714_买卖股票的最佳时机含手续费)
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
Introduction to ROS learning notes (I)
Getting started with vcpkg
How can programmers quickly develop high-quality code?
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?