ArrayAdapter 和 ListView 不起作用
ArrayAdapter and ListView do not work
目前我有兴趣开发自己的 android 应用程序。
因此我想使用 ListView。
实际上我的测试应用程序在启动后立即崩溃。我希望你能帮助我。
MainActivity.java
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] liste = {"Adidas","Nike","Puma"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,liste);
ListView list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.phxlxp_mxyxr.listview.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scrollbars="vertical" />
请试试这个。
将您的 ListView 的 ID 重命名为 android:id="@android:id/list"
。
答案是 here
<ListView
android:id=android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
目前我有兴趣开发自己的 android 应用程序。
因此我想使用 ListView。
实际上我的测试应用程序在启动后立即崩溃。我希望你能帮助我。
MainActivity.java
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] liste = {"Adidas","Nike","Puma"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,liste);
ListView list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.phxlxp_mxyxr.listview.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scrollbars="vertical" />
请试试这个。
将您的 ListView 的 ID 重命名为 android:id="@android:id/list"
。
答案是 here
<ListView
android:id=android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />