在ExpandableListView中编辑子项



我正在尝试使用ExpandableListView来显示带有其子项的项目列表。我希望每个孩子都有两个单选按钮(是和否(。单击"否"按钮时,将出现一个EditText,用户应在其中输入一些文本。

当用户单击保存按钮时,我希望保存数据。

经过一些研究,我认为最好在子对象发生变化时更新它们,而不是点击按钮一次检索所有对象。因此,我的代码试图在实际操作中显示这一点。如果这不是最好的方法,请告诉我。

我的问题是,当我编辑一个时,UNEXPECTED儿童会得到更新。

我看了以下链接(在许多链接中!(,但没有成功。

https://www.digitalocean.com/community/tutorials/android-expandablelistview-example-tutorial如何在可扩展列表视图的子视图中从多个编辑文本中提取数据?https://www.geeksforgeeks.org/baseexpandablelistadapter-in-android-with-example/

可扩展列表适配器

public class MyExpandableListAdapter extends BaseExpandableListAdapter{
private Context context;
private List<Parent> parents;
private HashMap<Parent, List<Child>> children;
public MyExpandableListAdapter(Context context, List<Parent> parents, HashMap<Parent, List<Child>> children){
this.context = context;
this.parents = parents;
this.children = children;
}
@Override
public Child getChild(int listPosition, int expandedListPosition){
return this.children.get(this.parents.get(listPosition)).get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition){
return this.getChild(listPosition, expandedListPosition).getId();
}
@Override
public View getChildView(final int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, parent, false);
}
TextView text = (TextView) convertView.findViewById(R.id.child_text);
RadioButton yesButton = (RadioButton)convertView.findViewById(R.id.yes_button);
RadioButton noButton = (RadioButton)convertView.findViewById(R.id.no_button);
EditText negativeResponse = (EditText)convertView.findViewById(R.id.no_response);
Child child = getChild(listPosition, expandedListPosition);
child.setResponse(true);
if (child != null)
{
text.setText(child.getText());
}
negativeResponse.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
String response = negativeResponse.getText().toString();
child.setNegativeResponse(response);
child.setResponse(response == null || response.isEmpty());
}
}
});
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeResponse.setVisibility(View.GONE);
child.setResponse(true);
child.setNegativeResponse(null);
}
});
noButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeResponse.setVisibility(View.VISIBLE);
child.setResponse(false);
}
});
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.children.get(this.parents.get(listPosition)).size();
}
@Override
public Parent getGroup(int listPosition) {
return this.parents.get(listPosition);
}
@Override
public int getGroupCount() {
return this.parents.size();
}
@Override
public long getGroupId(int listPosition) {
return this.getGroup(listPosition).getOid();
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Parent parent = getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.parent_layout, null);
}
TextView parentCode = (TextView) convertView.findViewById(R.id.code);
parentCode.setText(parent.getCode());
TextView parentDescription = (TextView) convertView.findViewById(R.id.description);
parentDescription.setText(parent.getDescription());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}

父级

public class Parent {
private int id;
private String code;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

public class Child {
private int id;
private String text;
private boolean response;
private String negativeResponse;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return questionText;
}
public void setText(String text) {
this.text = text;
}

public boolean isAffirmativeResponse() {
return response;
}
public void setResponse(boolean response) {
this.response = response;
}
public String getNegativeResponse() {
return negativeResponse;
}
public void setNegativeResponse(String negativeResponse) {
this.negativeResponse = negativeResponse;
}

活动

public class MyActivity {
private MyExpandableListAdapter listAdapter;
private ExpandableListView dataView;
@Override
public void onResume() {
dataView = (ExpandableListView) findViewById(R.id.dataView);
this.listAdapter = new MyExpandableListAdapter(this.getContext(), this.getParents(), this.getChildren());
dataView.setAdapter(this.listAdapter);
}

private List<Parent> getParents() {
// Gets the list of parents

}

private List<Child> getChildren() {
// Gets the list of children
}
}

父布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textStyle="bold" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textStyle="bold" />
</RelativeLayout>

子布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingRight="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/child_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
<RadioGroup
android:id="@+id/radio_group"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/child_text">
<RadioButton
android:id="@+id/no_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no"/>
<RadioButton
android:id="@+id/yes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:checked="true"
android:text="@string/yes"/>
</RadioGroup>
</RelativeLayout>
<EditText
android:id="@+id/negative_response"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:visibility="gone"/>
</RelativeLayout>

假设我有格式的数据

  • 父级1
    • 儿童1a
    • 儿童1b
    • 儿童1c
    • 儿童1d
  • 家长2
    • 孩子2a
    • 儿童2b
    • 儿童2c
    • 儿童2d
    • 儿童2e

如果我编辑Child 1a,Child 2c将反映我所做的更改。

如果我编辑Child 1b,Child 2c将反映我所做的更改。

如果我编辑Child 1c,Child 2d将反映我所做的更改。

如果我编辑Child 1d,Child 2e将反映我所做的更改。

我觉得我犯了一个非常简单的错误,但我看不出来。我如何编辑正确的子项,并且只编辑正确的孩子,以便保存数据?

这取决于我选择显示哪个Child以及它们是如何显示的。固定getChildView低于

@Override
public View getChildView(final int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, parent, false);
}
TextView text = (TextView) convertView.findViewById(R.id.child_text);
final RadioButton yesButton = (RadioButton) convertView.findViewById(R.id.yes_button);
final RadioButton noButton = (RadioButton) convertView.findViewById(R.id.no_button);
final EditText negativeResponse = (EditText) convertView.findViewById(R.id.no_response);
final Child child = getChild(listPosition, expandedListPosition);

if (child != null) {
text.setText(child.getText());
}
negativeResponse.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
response.setNegativeResponse(negativeResponse.getText().toString());
}
}
});
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeResponse.setVisibility(View.GONE);
response.setPositiveQuestionResponse(true);
response.setNegativeResponse("");
}
});
noButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeResponse.setVisibility(View.VISIBLE);
response.setPositiveQuestionResponse(false);
}
});
return convertView;
}

相关内容

  • 没有找到相关文章

最新更新