0
我想爲我的Android應用程序的ListView
中的每個項目添加子項目。爲此我寫了這段代碼(下面給出)。但是這不起作用。我是Android新手,我看到很多解決這個問題的例子,但他們沒有幫助。所以請告訴我如何在我的ListView
中添加每件商品的子商品。在ListView中沒有添加子項目
ListDisplay.java
public class ListDisplay extends AppCompatActivity {
String[] items = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
String[] subItems = {"lollipop","4s","8.1","a1","os","ubnt","7","air"};
public static ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_display);
listView = (ListView) findViewById(R.id.listViewItems);
ArrayAdapter adapter = new ArrayAdapter<String>(context, R.layout.activity_listview, items, subItems);
ListDisplay.listView.setAdapter(adapter);
}
}
activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
content_list_display.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.abc.project1.ListDisplay"
tools:showIn="@layout/activity_list_display">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewItems"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
請看看我的答案,如果告訴我你有它的工作! – varunkr