我是Android编码的新手,我试图将数据从Array传递到ListView,我可以在App的第一个Activity上使用它,但是第二个Activity没有显示数据,任何人都可以发现我做错了吗?
我已经尝试了6个或7个来自网络搜索的示例,但没有任何效果,使用示例工作创建了一个新应用,唯一的区别是我在第二个活动上实现了Array
公共类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,
dataList); 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>
第一个活动是“ StartPage”类,“这是我的message1”和“这是我的message1.1”位于Logcat中,分配的按钮按下到实现数组的Activity2“ BuildingSiteInfoActivity”类,尝试将String数组化为“ animalList”和“ dataList”,但是我得到一个空的ListView且日志中没有其他“这是我的消息”出现
答案 0 :(得分:0)
嘿,欢迎来到Android开发人员小组
此代码是错误的!它只是加载您的布局;
//call window2
setContentView(R.layout.building_site_info);
//this is wrong
如果您想加载活动,可以使用以下方法:
//call window2
startActivity(new Intent(this , BuildingSiteInfoActivity.class));
//this is correct
您的代码中的第二项活动未运行! 试试吧,我希望它能起作用:)