代码----
packagecom.example.administrator.demo1;importandroid.os.Handler;importandroid.support.v7.app.AlertDialog;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.util.AttributeSet;importandroid.view.View;importandroid.widget.FrameLayout;importandroid.widget.LinearLayout;importandroid.widget.TabHost;importandroid.widget.TabWidget;importandroid.widget.TextView;importandroid.widget.Toast;importorg.whm.db.DbUtils;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.Objects;importjava.util.jar.Attributes;public class MainActivity extendsAppCompatActivity {//钩子线程
Handler handler = null;
@Overrideprotected voidonCreate(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= newHandler();//在lambda中尽量不要抛出异常。。。。
new Thread(() ->{
List> 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 nativeString stringFromJNI();//从远程服务器获取数据
private List>getData(LinearLayout l) {
List> list = null;try{
list= DbUtils.execQuery("select * from video", null);
}catch(Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"发生了错误", Toast.LENGTH_LONG).show();
}returnlist;
}//绘制ui
private void darwView(List>list, LinearLayout view) {
list.forEach((map)->{
TextView tv= newTextView(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");
}
}