在其他活动中将数据从一个活动发送到动态列表

时间:2015-04-04 13:09:11

标签: android android-intent

我尝试过使用意图功能,但只要我尝试进入仪表板屏幕,应用就会崩溃

public class Dashboard extends Activity {

    Button add1;
    private ListView lv;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    Intent intent = getIntent();
    String message = intent.getExtras().getString("txtData","");     

    lv = (ListView) findViewById(R.id.listView1);

        // Instantiating an array list (you don't need to do this, 
        // you already have yours).
    List<String> your_array_list = new ArrayList<String>();
    your_array_list.add(message);


        // This is the array adapter, it takes the context of the activity as a 
        // first parameter, the type of list view as a second parameter and your 
        // array as a third parameter.
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this,  R.layout.list, your_array_list );

    lv.setAdapter(arrayAdapter); 



    add1 = (Button)findViewById(R.id.button1); 
    add1.setOnClickListener(new View.OnClickListener() {

            @Override
    public void onClick(View v) {
                // TODO Auto-generated method stub
    Intent i = new Intent(getBaseContext(),AddDevice.class);
    startActivity(i);
            }

         });
     }

}

我正在尝试将数据从AddDevice.java发送到Dashbaord,并将该数据输入到应该在dashbaord上可见的列表中,这是我的AddDevice.java

public class AddDevice extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);

        getActionBar().setDisplayHomeAsUpEnabled(true);   

        Intent intent = new Intent(getBaseContext(),Dashboard.class);

        EditText editText = (EditText) findViewById(R.id.editText2);
        String message = editText.getText().toString();
        intent.putExtra("txtData", message);
    }
}

1 个答案:

答案 0 :(得分:0)

为什么在onClick on listener中使用getBaseContext()?请将其更改为getApplicationContext()或Dashboard.this

如果你检查logcat,你会发现错误:

  

&#34; Intent类型中的构造函数Intent(Context,Class)不适用于参数(new    View.OnClickListener(){},Class)&#34;