我想在我的Android应用程序的ListView
中添加每个项目的子项。为此我写了这段代码(如下所示)。但这不起作用。我是Android的新手,我已经看到很多解决这个问题的例子,但他们没有帮助。因此,请告诉我如何在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>
答案 0 :(得分:0)
您需要为此制作自定义适配器。这是您应该使用的代码。
/** class to act as list adapter for rows List */
private static class TwoTextListAdapter extends ArrayAdapter<MyDataClass> {
/** To cache views of item */
private static class ViewHolder {
private TextView label;
private TextView subLabel;
/**
* General constructor
*/
ViewHolder() {
// nothing to do here
}
}
/** Inflater for list items */
private final LayoutInflater inflater;
/**
* General constructor
*
* @param context
* @param resource
* @param textViewResourceId
* @param objects
*/
public TwoTextListAdapter(final Context context,
final int resource,
final int textViewResourceId,
final List<User> objects) {
super(context, resource, textViewResourceId, objects);
this.inflater = LayoutInflater.from(context);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View itemView = convertView;
ViewHolder holder = null;
final MyDataClass item = getItem(position);
if(null == itemView) {
itemView = this.inflater.inflate(R.layout.activity_listviewpar, parent, false);
holder = new ViewHolder();
holder.label = (TextView)itemView.findViewById(R.id.label);
holder.subLabel = (TextView)itemView.findViewById(R.id.subLabel);
itemView.setTag(holder);
} else {
holder = (ViewHolder)itemView.getTag();
}
holder.label.setText(item.getlabel());
holder.subLabel.setText(item.getsubLabel());
return itemView;
}
}
activity_listview.xml应该如下所示
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/subLabel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
</LinearLayout>
最后在ListDisplay中执行此操作
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;
public List<Item> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_display);
for(int i=0;i<items.length;i++){
list.add(new Item(items[i],subItems[i]))
}
listView = (ListView) findViewById(R.id.listViewItems);
TwoTextListAdapter adapter = new TwoTextListAdapter(context, R.layout.activity_listview, null, list);
ListDisplay.listView.setAdapter(adapter);
}
public class Item {
public String a,b;
public Item(String a, String b) {
this.a = a;
this.b = b;
}
}
}