将值从mainactivity传递到listview

时间:2015-05-21 12:23:59

标签: android android-intent

大家好我正在开发一个Android应用程序,我在代码中有一些混乱,我有一个主要的活动,我存储一些数组值。 我有两个xml文件(一个mainactivity.xml,另一个是listview.xml)

在main activity.xml中有四个开关,当我点击任何特定的开关时,它应该带我到listview.xml,列表视图适配器在列表视图上显示相应的数组。代码如下

public class MainActivity extends ActionBarActivity {


ListView l;

 Button chem = (Button) findViewById(R.id.button4);
 public String[] contentc = {
            "Abundance",
            "Anxiety",
            "Bruxism",
            "Discipline",
            "Drug Addiction"
        };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    l= (ListView) findViewById(R.id.listview);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_element, contentp);
    chem.setOnClickListener(new OnClickListener() {
        public void onClick(View v)
        {
            //DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
            setContentView(R.layout.list);

            l.setAdapter(adapter);  



        } 
    });






}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
 }

1 个答案:

答案 0 :(得分:0)

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

在activity1中,在onCreate()中,您可以通过检索Bundle(包含调用活动发送的所有消息)来获取String消息,并在其上调用getString():

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message")