注册 登录
编程论坛 Android开发

动态更新UI的问题

情.难言ゝ 发布于 2015-04-29 16:40, 3615 次点击
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录


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();
    }
2 回复
#2
q2152362132015-05-10 20:55
在MainActivity 中 使用  startActivityForResult 方法启动AddActivity,同时在MainActivity中,重写 onActivityResult方法,在AddActivity 的按钮点击事件中使用 setResult方法,这样把AddActivity中的数据传回到MainActivity中,在onActivityResult方法中,处理返回的数据就可以了
#3
情.难言ゝ2015-05-11 14:59
我解决了,用的Intent传List
1