ArrayAdapter未在第二个Activity中显示数据

问题描述 投票:0回答:1

我是Android编码的新手,我正在尝试将数据从Array传递到ListView,我可以让它在App中的第一个Activity上运行,但是第二个Activity没有显示数据,任何人都可以发现我在做什么错误?

我已尝试过6次或7次网络搜索示例,但没有任何效果,使用示例工作创建一个新的应用程序,唯一的区别是我在第二个活动上实现了数组

公共类StartPage扩展AppCompatActivity {

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

    Log.d("myTag", "This is my message1");
   /* public void goToActivity2 (View view){
        Intent intent = new Intent (this, BuildingSiteInfoActivity.class);
        startActivity(intent);*/
    }

    public void openWindow2(View v) {
        //call window2
        setContentView(R.layout.building_site_info);
        Log.d("myTag", "This is my message1.1");
    }
}

公共类BuildingSiteInfoActivity扩展AppCompatActivity {

ArrayAdapter arrayAdapter;


// Array of strings...
ListView simpleList;
String animalList[] = {"Lion","Tiger","Monkey","Elephant","Dog","Cat","Camel"};




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

    List<String> dataList = new ArrayList<String>();
    dataList.add("Java");
    dataList.add("Android");
    dataList.add("JavaEE");
    dataList.add("JSP");
    dataList.add("JDBC");


    Log.d("myTag", "This is my message2");

    simpleList = (ListView) findViewById(R.id.simpleListView);
    Log.d("myTag", "This is my message3");
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_listview, R.id.textView,

数据列表); simpleList.setAdapter(arrayAdapter); Log.d(“myTag”,“这是我的消息4”);

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="391dp"
    android:layout_height="682dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/darkbluex" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="233dp"
    android:layout_height="63dp"
    android:layout_marginTop="36dp"
    android:text="@string/bsText1"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/imageView2" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="345dp"
    android:layout_height="102dp"
    android:layout_marginTop="28dp"
    android:text="@string/bsText2"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />


<Button
    android:id="@+id/homePage1"
    android:layout_width="299dp"
    android:layout_height="97dp"
    android:layout_marginTop="28dp"
    android:background="@drawable/buttonrec1"
    android:text="Back"
    android:textAlignment="center"
    android:textColor="@android:color/white"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/simpleListView" />

<ListView
    android:id="@+id/simpleListView"
    android:layout_width="317dp"
    android:layout_height="261dp"
    android:layout_marginTop="40dp"
    android:background="@android:color/white"
    android:scrollbars="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3" />
<!--app:layout_constraintTop_toBottomOf="@+id/textView3"-->

<TextView
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="64dp"
    android:textColor="@android:color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
     /> </LinearLayout>

第一个Activity是“StartPage”类,“这是我的消息1”和“这是我的消息1.1”都在Logcat中,分配的按钮推送到Activity2“BuildingSiteInfoActivity”类,其中实现了Array我已经尝试了String to数组“animalList”和“dataList”但是我得到一个空的ListView而没有其他“这是我的消息”出现在日志中

enter image description here

android
1个回答
0
投票

嘿欢迎来到Android开发者组

这段代码错了!它只是加载您的布局;

//call window2
setContentView(R.layout.building_site_info);
//this is wrong

如果你想加载你的活动,你可以使用这个:

//call window2
startActivity(new Intent(this , BuildingSiteInfoActivity.class));
//this is correct

在您的代码中第二个活动不运行!尝试一下,我希望它有效:)

© www.soinside.com 2019 - 2024. All rights reserved.