通过Intent启动时更改活动内容

时间:2017-11-13 11:34:26

标签: android android-intent android-activity

我有一个包含10个项目的listView和一个包含textView的活动。我想在listView ItemClick上启动活动,但其中包含不同的textView。我可以通过意图启动活动但是如何更改textView中的值。有没有办法做到这一点?

      lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id){

                    switch (position){
                        case 0:
                            Intent myIntent = new Intent(ListActivity.this, Districts.class);
                            ListActivity.this.startActivity(myIntent);
//I want textView to display 'abc'

                        case 1:
                            Intent myIntent = new Intent(ListActivity.this, Districts.class);
                            ListActivity.this.startActivity(myIntent);
//I want textView to display 'xyz'

                    }
                }
            });

1 个答案:

答案 0 :(得分:2)

将数据传递给意图

Intent myIntent = new Intent(ListActivity.this, Districts.class);
myIntent.putExtra("key","value");
ListActivity.this.startActivity(myIntent);

并在下一个活动中获取

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
    textView.setText(value);
}