-3

实际上我有两个活动,即 MainActivity 和 ListVIew 。
我想将这个字符串数组“arr”从 MainActivity 传递给 ListVIew ..
Activity ListVIew 将显示“arr”的元素列表

注意: arr 中的数据是由我创建的数据库提供的,它工作正常。

在 Activity ListView 中检索 arr 存在一些问题

主要活动

package com.vivekmishra1991.database;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
public int i;

public String arr[] = new String[100];


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

    // A clickable TextView 

    final TextView textView=(TextView) findViewById(R.id.savedContent);
   textView.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {


            <--ARRAY(arr) RETRIVED FROM DATABASE-->



          Intent intent=new Intent(MainActivity.this,ListVIew.class);
           intent.putExtra("array",arr);
          startActivity(intent);


 }
   });


 }  // onCreate ends here



}

列表显示

  <-imports->

public class ListVIew extends Activity {
ListView listview;
Intent intent=getIntent();
String[] arr=intent.getStringArrayExtra("array");
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_list);






listview.findViewById(R.id.list);
     ArrayAdapter<String> Adapter = new ArrayAdapter<String>        (this,android.R.layout.simple_list_item_1,arr);
    listview.setAdapter(Adapter);
}

}

4

2 回答 2

0

If you want to send data to another activity, you can do it using intent.

Bundle bund = new Bundle();

bund.putString("myKey",name);

Intent intent = new Intent(MainActivity.this, ListView.class);

intent.putExtras(bund);

startActivity(intent);

Now in ListView class, receive this data in onCreate()

    @Override
protected void onCreate(Bundle savedInstanceState) {
      .......

    String nameReceived = getIntent().getExtras().getString("myKey");
}

I have given the example of passing String to another activity however, you can pass boolean, int, double etc to another activity. See the full list on here

于 2013-08-13T20:29:05.817 回答
0

你应该从 oncreate 中获得额外的东西

'getIntent().getStringArrayExtra(字符串名称)'

编辑

我错误地写了 bundle ,这是通过意图传递对象的另一种方法。我编写的方法将其直接存储在意图中。

于 2013-08-12T20:34:00.333 回答