Java 在Android Studio中显示函数数据而不闪烁
最近,我正在进行Android项目,使用Volley进行注册,为了进一步操作,我可以创建用于插入的函数,另一个用于检索数据。当插入按钮点击“插入”时,调用了“插入”函数,数据已通过截取插入到数据库中,同时还调用了检索函数。但当用户单击按钮并调用函数时,数据显示(数据库插入的数据)会闪烁,看起来像是加载 我想消除这种影响。我希望平滑地显示数据,而不产生任何闪烁效果。我确实在搜索,但找不到任何解决方案。请建议我的解决方案,我是一个新手,所以亲切的短和高效率的要求Java 在Android Studio中显示函数数据而不闪烁,java,android,android-volley,Java,Android,Android Volley,最近,我正在进行Android项目,使用Volley进行注册,为了进一步操作,我可以创建用于插入的函数,另一个用于检索数据。当插入按钮点击“插入”时,调用了“插入”函数,数据已通过截取插入到数据库中,同时还调用了检索函数。但当用户单击按钮并调用函数时,数据显示(数据库插入的数据)会闪烁,看起来像是加载 我想消除这种影响。我希望平滑地显示数据,而不产生任何闪烁效果。我确实在搜索,但找不到任何解决方案。请建议我的解决方案,我是一个新手,所以亲切的短和高效率的要求 package com.darkco
package com.darkcoderz.parsejson;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
//private CoordinatorLayout mCLayout;
private TextView mTextView;
private String mJSONURLString = "http://192.168.10.4/volley/api.php";
String url = "http://192.168.10.4/volley/register.php";
private EditText sms;
private Button sendsms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context
//mContext = getApplicationContext();
//mActivity = MainActivity.this;
// Get the widget reference from XML layout
//mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
mTextView = (TextView) findViewById(R.id.tv);
sms = (EditText) findViewById(R.id.sms);
sendsms = (Button) findViewById(R.id.sendsms);
final Handler firesms = new Handler();
firesms.post(new Runnable() {
@Override
public void run() {
getdata();
firesms.postDelayed(this, 100);
}
});
sendsms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
reg();
}
});
getdata();
}
// insert
public void reg()
{
final String msg = sms.getText().toString();
StringRequest stringreq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("success"))
{
Toast.makeText(MainActivity.this, "Registration Successfull!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Username Already Exist!", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Great Error "+error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("sms",msg);
return params;
}
};
RequestQueue reqest = Volley.newRequestQueue(MainActivity.this);
reqest.add(stringreq);
package com.darkcoderz.parsejson;
公共类MainActivity扩展了AppCompatActivity{
私有上下文;
私人活动能力;
//私人协调者布局图;
私有文本视图mTextView;
私有字符串mJSONURLString=”http://192.168.10.4/volley/api.php";
字符串url=”http://192.168.10.4/volley/register.php";
私人短信;
私人按钮发送短信;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取应用程序上下文
//mContext=getApplicationContext();
//mActivity=MainActivity.this;
//从XML布局获取小部件引用
//mCLayout=(协调人布局)findViewById(R.id.协调人布局);
mTextView=(TextView)findviewbyd(R.id.tv);
sms=(EditText)findViewById(R.id.sms);
sendsms=(按钮)findViewById(R.id.sendsms);
最终处理程序firesms=新处理程序();
firesms.post(新的Runnable(){
@凌驾
公开募捐{
getdata();
firesms.postDelayed(这个,100);
}
});
sendsms.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
reg();
}
});
getdata();
}
//插入
公开作废登记()
{
最后一个字符串msg=sms.getText().toString();
StringRequest stringreq=new StringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
if(response.equals(“success”))
{
Toast.makeText(MainActivity.this,“注册成功!”,Toast.LENGTH_SHORT.show();
}
其他的
{
Toast.makeText(MainActivity.this,“用户名已经存在!”,Toast.LENGTH_SHORT.show();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(MainActivity.this,“大错误”+Error.toString(),Toast.LENGTH\u LONG.show();
}
})
{
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“sms”,msg);
返回参数;
}
};
RequestQueue reqest=Volley.newRequestQueue(MainActivity.this);
请求添加(stringreq);
}
private void getdata(){
//清空文本视图
mTextView.setText(“”);
//初始化新的RequestQueue实例
RequestQueue RequestQueue=Volley.newRequestQueue(MainActivity.this);
//初始化新的JsonArrayRequest实例
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Request.Method.GET,mJSONURLString,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
//做些有反应的事情
//mTextView.setText(response.toString());
//处理JSON
试一试{
//循环遍历数组元素
对于(int i=0;iprivate void getdata(){
//清空文本视图
mTextView.setText(“”);
//初始化新的RequestQueue实例
RequestQueue RequestQueue=Volley.newRequestQueue(MainActivity.this);
//初始化新的JsonArrayRequest实例
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Request.Method.GET,mJSONURLString,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
//在解析之前,请检查您的响应是否为JSONArray格式或JSONObject格式
//处理JSON
试一试{
}捕获(JSONException e){
e、 printStackTrace();
//在此处打印以了解JSONException(如果存在)
Toast.makeText(mContext,“Exception”+e.toString(),Toast.LENGTH_SHORT.show();
}
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//发生错误时做些什么
Toast.makeText(mContext,“出错”,Toast.LENGTH_SHORT.show();
}
}
);
//将JsonArrayRequest添加到请求队列
add(jsonArrayRequest);
}
private void getdata(){
//清空文本视图
mTextView.setText(“”);
//初始化新的RequestQueue实例
RequestQueue RequestQueue=Volley.newRequestQueue(MainActivity.this);
//初始化新的JsonArrayRequest实例
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Request.Method.GET,mJSONURLString,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
//在解析之前,请检查您的响应是否为JSONArray格式或JSONObject格式
//处理JSON
试一试{
}捕获(JSONException e){
e、 printStac
private void getdata() {
// Empty the TextView
mTextView.setText("");
// Initialize a new RequestQueue instance
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Initialize a new JsonArrayRequest instance
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, mJSONURLString, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Do something with response
//mTextView.setText(response.toString());
// Process the JSON
try{
// Loop through the array elements
for(int i=0;i<response.length();i++){
// Get current json object
JSONObject student = response.getJSONObject(i);
// Get the current student (json object) data
// String firstName = student.getString("fname");
// String lastName = student.getString("lname");
String age = student.getString("email");
// Display the formatted json data in text view
mTextView.append("SMS : " + age);
mTextView.append("\n\n");
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
Toast.makeText(mContext, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
}
);
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
}
private void getdata() {
// Empty the TextView
mTextView.setText("");
// Initialize a new RequestQueue instance
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Initialize a new JsonArrayRequest instance
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, mJSONURLString, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//before parsing check your response is in JSONArray Format or JSONObject format
// Process the JSON
try{
}catch (JSONException e){
e.printStackTrace();
//print here to know JSONException if exists
Toast.makeText(mContext, "Exception"+e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
Toast.makeText(mContext, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
}
);
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
}