我正在嘗試使用ListView
組件,並且我被卡住了。 我只想在我的活動中查看列表。我在網上搜索了一個解決方案, 但我不知道什麼是錯在我的代碼ListView未在活動中顯示
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.a05042.listviewproject.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="@+id/ListViewMain"
android:background="@android:color/holo_blue_bright" />
</RelativeLayout>
activity_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textAlignment="center" />
</LinearLayout>
JAVA:
public class MainActivity extends AppCompatActivity {
String[] myItems = {"Sony","Toshiba","Samsung","y","daniel","sold","sail"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MakeListView();
}
public void MakeListView() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_list,myItems);
ListView lv = (ListView)findViewById(R.id.ListViewMain);
lv.setAdapter(adapter);
}
}
是什麼問題? –
應用程序錯誤你不能看到列表,當應用程序啓動它的近期進展... –