问题
I currently have an expandable list view that is populating just fine. However when the child view is filling it creates individuals views and rows for each entry. I would like to have one view and multiple textviews in the view as well as a button that I will be adding. The data is being populated from a HASHMAP. I have tried many things and cannot get multiple textviews to populate correctly in the listview. All data comes from this hashmap:
private HashMap<String, List<String>> _deviceDetails;
This is the get child view method of my custom adapter.
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
This is an image of what I have now with what I wish to do.
I need the data highlighted inside of one row so that I can add buttons etc without a bunch of rows. I genuinely cannot figure out how to do this properly.
回答1:
I'm an idiot. I figured it out. Thank you @Krupal you pointed me in the right direction. If you place anything as an answer i'll select you because you got me thinking the right way.
So what I ended up doing here was I created a custom List Adapter. I won't add the entire class to keep it short. The key was to use set the adapter to only return one child. Then in the child that was returned (XML below) I had all of my needed fields added in. So now when I expanded the list only one child would be visible. Also in my getChildView() I defined all needed entries for the layout for the children. Don't know if this is the perfect way to do it but worked nicely for my needs.
This is where I actually configured the child with my data.
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, 0);
final String childTextSecond = (String) getChild(groupPosition, 1);
final String childTextThird = (String) getChild(groupPosition, 2);
final String deviceName = (String) getChild(groupPosition, 3);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
TextView txtListChildTwo = (TextView) convertView
.findViewById(R.id.lblListItemTwo);
TextView txtListChildThree = (TextView) convertView
.findViewById(R.id.lblListItemThree);
//rest of logic!
}
Setting the children count to 1
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
XML Layout for the child elements
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<RelativeLayout
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|right"
android:background="#f9fcff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:background="#4d86ff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:id="@+id/lblListItem"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView5"
android:layout_toRightOf="@+id/textView5"
android:layout_toEndOf="@+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Device ID: "
android:id="@+id/textView5"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Tags"
android:id="@+id/btnTags"
android:background="#ffffff"
android:singleLine="true"
android:layout_alignTop="@+id/lblListItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Status: "
android:id="@+id/textView6"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Large Text"
android:id="@+id/lblListItemTwo"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView6"
android:layout_toRightOf="@+id/textView6"
android:layout_toEndOf="@+id/textView6" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"
android:textColor="#ffffff"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Description: "
android:id="@+id/textView7"
android:layout_marginStart="15dp"
android:layout_alignTop="@+id/lblListItemThree"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:layout_marginRight="13dp"
android:layout_marginBottom="10dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Large Text"
android:id="@+id/lblListItemThree"
android:textColor="#ffffff"
android:layout_marginStart="39dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView7"
android:layout_toEndOf="@+id/textView7" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
来源:https://stackoverflow.com/questions/32768040/multiple-textviews-in-expandablelistview-android