zoukankan      html  css  js  c++  java
  • Android 远程连接数据库。。。。。

    本来是 6.0.6  换成mysql 5.1.14 驱动ok。。。。

    将方法 放在 new Thread() 解决。。。。。

    只能在主线程绘制ui。。。。

    解决办法。。。
    子Thread 获取数据后,将绘制ui代码放到Handler.post中执行

    代码----
    
    package com.example.administrator.demo1;
    
    import android.os.Handler;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    import android.widget.TabHost;
    import android.widget.TabWidget;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import org.whm.db.DbUtils;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.jar.Attributes;
    
    public class MainActivity extends AppCompatActivity {
    
        //钩子线程
        Handler handler = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Example of a call to a native method
            //TextView tv = (TextView) findViewById(R.id.tabhost);
            // tv.setText(stringFromJNI());
            TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
            tabHost.setup();
    
    
    //        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tabcontent);
    //        int childCount = frameLayout.getChildCount();
            LinearLayout l1 = (LinearLayout) findViewById(R.id.tab1);
            LinearLayout l2 = (LinearLayout) findViewById(R.id.tab2);
    
            handler = new Handler();
    
            //在lambda中尽量不要抛出异常。。。。
            new Thread(() -> {
                List<Map<String, Object>> list = getData(l1);
                handler.post(new Thread(() -> {
                    darwView(list, l1);
                }));
            }).start();
    
    
            TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("tabSpec1").setIndicator("视频").setContent(R.id.tab1);
            TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("tabSpec2").setIndicator("直播").setContent(R.id.tab2);
    
    
            tabHost.addTab(tabSpec1);
            tabHost.addTab(tabSpec2);
    
    
        }
    
        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();
    
    
        //从远程服务器获取数据
        private List<Map<String, Object>> getData(LinearLayout l) {
            List<Map<String, Object>> list = null;
            try {
                list = DbUtils.execQuery("select * from video", null);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "发生了错误", Toast.LENGTH_LONG).show();
            }
            return list;
    
        }
    
    
        //绘制ui
        private void darwView(List<Map<String, Object>> list, LinearLayout view) {
            list.forEach((map) -> {
                TextView tv = new TextView(getApplicationContext());
                tv.setText((String) map.get("title"));
                view.addView(tv);
            });
        }
    
    
        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");
        }
    }
  • 相关阅读:
    Prometheus—告警altermanger
    Prometheus监控Kafka
    get与post(转)
    js typeof
    设置SQL脚本大小敏感
    max Count Group by
    统计当年登陆次数
    IOC
    ORM
    [转载]C#实现获取浏览器信息
  • 原文地址:https://www.cnblogs.com/whm-blog/p/7042617.html
Copyright © 2011-2022 走看看