动态更新UI的问题
main.xml add.xml
想在AddActivity中点击添加,MainActivity中线性布局中栏目(新闻,头条等)可以添加
MainActivity.java
程序代码:
LinearLayout ll_home_menu; Vector<View> home_menu = new Vector<View>(); int num = 10; //显示新闻的条数 private ListView lv_news; TextView tv_retrieve; TextView tv_add; TextView tv_setting; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); updateUI(); } public void updateUI() { // 更新界面 ll_home_menu = (LinearLayout) findViewById(R.id.ll_home_menu); ll_home_menu.removeAllViews(); android.util.Log.v("success",""+home_menu.size()); for(int j=0; j<home_menu.size(); j++) { ll_home_menu.addView(home_menu.get(j)); home_menu.get(j).setOnClickListener(clicklistener); } } private void init() { // 初始化界面 String[] m = {" 军事 "," 体育 "," 天气 "," 软通动力 "," 头条 "," 本地 "," 娱乐 "}; TextView tv; tv_retrieve = (TextView) findViewById(R.id.tv_home_retrieve); tv_add = (TextView) findViewById(R.id.tv_home_add); tv_setting = (TextView) findViewById(R.id.tv_home_log); tv_retrieve.setOnClickListener(clicklistener); tv_add.setOnClickListener(clicklistener); tv_setting.setOnClickListener(clicklistener); for(int i=0; i<m.length; i++) { // 初始化栏目 tv = new TextView(this); tv.setId(i); tv.setTextSize(20); tv.setText(" "+m[i]+" "); home_menu.add(tv); } }
AddActivity.java
程序代码:
private OnClickListener clicklistener // 点击事件(添加、确定) = new OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.tv_keyword_add: et_keyword = (EditText) findViewById(R.id.et_add_keyword); addColumn(et_keyword.getText().toString()); finish(); break; case R.id.tv_area_determine: sp_area = (Spinner) findViewById(R.id.sp_area_area); addColumn(sp_area.getSelectedItem().toString()); finish(); break; default: break; } }}; public void addColumn(String column) { // 添加栏目 MainActivity m = new MainActivity(); TextView tv = new TextView(getBaseContext()); tv.setText(" "+column+" "); tv.setTextSize(20); m.home_menu.addElement(tv); Thread t = new Thread(); t.start(); }