#android
#Android
Вопрос:
Я могу извлекать данные из базы данных SQLite (если быть точным, 3 столбца) и отображать их в расширяемом Listview как дочерние элементы. Но я хочу отобразить их как:
Срок выполнения: 23.10.2016
поздний ответ: ДА
Штраф: 10%
Выше, текст с левой стороны, я отображаю из Textview текст (дочерний xml), а текст с правой стороны после двоеточия я извлекаю из базы данных.
Как мне справиться с этим в методе getChildView в моем адаптере?
Вот мой код класса адаптера:
public class AssignmentDisplayAdapter extends BaseExpandableListAdapter{
private Context mContext;
private List<String> expandableListTitle;
private HashMap<String, List<CourseDisplay>> expandableListDetail;
private LayoutInflater mInflater;
public AssignmentDisplayAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<CourseDisplay>> expandableListDetail) {
this.mContext = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_listview_expandable_child, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.assign_duedate_display);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_listview_expandable_parent, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.parent_text);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}//end class
Вот мой родительский XML-код:
<?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">
<TextView
android:id="@ id/parent_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="@color/primary"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>
Вот мой дочерний XML-код:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@ id/assign_duedatetext_display"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Due Date: "
android:layout_weight="3"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
<TextView
android:id="@ id/assign_duedate_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="@id/assign_duedatetext_display"
android:layout_marginLeft="16dp"
android:textColor="@color/primary"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@ id/assign_latesubtext_display"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Late Submission: "
android:layout_weight="3"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
<TextView
android:id="@ id/assign_latesub_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="@id/assign_latesubtext_display"
android:layout_marginLeft="16dp"
android:textColor="@color/primary"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@ id/assign_penaltytext_display"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Penalty: "
android:layout_weight="3"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
<TextView
android:id="@ id/assign_penalty_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="@id/assign_penaltytext_display"
android:layout_marginLeft="16dp"
android:textColor="@color/primary"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</RelativeLayout>
</LinearLayout>
Я считаю, что что-то нужно сделать в методе getChildView, но не уверен, как с этим справиться. Пожалуйста, проведите меня через это.
Комментарии:
1. Самый простой способ добиться этого — представить каждую строку как макет, состоящий из двух вертикально расположенных вложенных макетов. Верхний макет — это «заголовок», нижний макет будет содержать ваши текстовые представления. Нижний макет не отображается, но он будет показан, когда пользователь нажмет на верхний макет. Вы можете сделать это, установив OnClickListener в верхней строке и изменив видимость нижнего представления внутри него. Чтобы изменить видимость, вы можете использовать метод View.setVisibility (int visibility)
2. Я попробую это и посмотрю. Спасибо за комментарий
3. Я опубликовал свой ответ. Я нашел способ динамически обновлять текст в коде. В любом случае, я попробую с вашим предложением. Спасибо за ваше время.
Ответ №1:
Я могу добиться этого динамически, поскольку я знаю, что формат данных в базе данных будет одинаковым для каждого родителя.
Здесь обновлен метод getChildView():
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_listview_expandable_child, null);
}
TextView expandedListTextView1 = (TextView) convertView
.findViewById(R.id.assign_duedate_display);
TextView expandedListTextView2 = (TextView) convertView
.findViewById(R.id.assign_duedatetext_display);
if (expandedListPosition == 0){
expandedListTextView2.setText("Due date: ");
}else if(expandedListPosition == 1){
expandedListTextView2.setText("late submission: ");
}else {
expandedListTextView2.setText("Penalty: ");
}
expandedListTextView1.setText(expandedListText);
return convertView;
}