如何从可扩展的可回收的编辑文本中获得价值

时间:2016-10-13 06:19:10

标签: android

如何从Android中的可扩展可回收编辑文本中获得价值,我使用了" com.thoughtbot:expandablerecyclerview:1.0" 库安卓工作室。 在可扩展的可回收利用中无法获得适当的价值

public class MasterAdapter extends ExpandableRecyclerViewAdapter<ParentView, ChildView> {

    private List<? extends ExpandableGroup> list;
    private Activity activity;
    private Button button;

    public MasterAdapter(Activity activity, List<? extends ExpandableGroup> list) {
        super(list);
        this.activity = activity;
        this.list = list;
        button = (Button) activity.findViewById(R.id.btnOk);
    }

    @Override
    public ParentView onCreateGroupViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parent_view, parent, false);
        ParentView parentView = new ParentView(view);
        return parentView;
    }

    @Override
    public ChildView onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_view, parent, false);
        ChildView childView = new ChildView(view);
        return childView;
    }

    @Override
    public void onBindChildViewHolder(final ChildView holder, int flatPosition, ExpandableGroup group, int childIndex) {
        DataValue dataValue = ((MainData) group).getItems().get(childIndex);
        holder.Bind(dataValue);
//
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String editTextData = ((EditText) view).getText().toString().trim();
                String editTextData = holder.editText.getText().toString().trim();

                if (holder.editText!=null) {
//                    Toast.makeText(activity, editTextData, Toast.LENGTH_LONG).show();
                    Toast.makeText(activity, editTextData, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public void onBindGroupViewHolder(ParentView holder, int flatPosition, ExpandableGroup group) {
        holder.getTitle(group);
    }
}

ParentView.class

 public class ParentView extends GroupViewHolder {

    TextView textView;

    public ParentView(View view) {
        super(view);
        textView = (TextView) view.findViewById(R.id.parentName);
    }

    public void getTitle(final ExpandableGroup group) {
        textView.setText(group.getTitle());
    }
}

ChildView.class

public class ChildView extends ChildViewHolder {

    EditText editText;
    String string;

    public ChildView(View view) {
        super(view);
        editText = (EditText) view.findViewById(R.id.childtName);

    }
    public void Bind(DataValue dataValue) {
        editText.setText(dataValue.getName());

    }

}

DataValue.class

public class DataValue implements Parcelable {

    private String title;
    private String name;

    public DataValue(String title, String name) {
        this.title = title;
        this.name = name;
    }

    public DataValue(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {

    }

MainData

public class MainData extends ExpandableGroup<DataValue> {

    public MainData(String title, List<DataValue> items) {
        super(title, items);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您可以在用户点击按钮并获取输入文本时使用界面来处理。

步骤1:定义界面

group.id

步骤2:使用Adapter类上的接口:

public interface GetInputListener{

 public void getInput(String message);

}

步骤3:为此适配器使用适配器和输入侦听器。在回调方法getInput()中,您可以获得edittext的输入。

public class MasterAdapter extends ExpandableRecyclerViewAdapter<ParentView, ChildView> {

    private List<? extends ExpandableGroup> list;
    private Activity activity;
    private Button button;
    private GetInputListener getInputListener;

    public MasterAdapter(Activity activity, List<? extends ExpandableGroup> list, GetInputListener listener) {
        super(list);
        this.activity = activity;
        this.list = list;
        button = (Button) activity.findViewById(R.id.btnOk);
        this.getInputListener = listener;
    }

    @Override
    public ParentView onCreateGroupViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parent_view, parent, false);
        ParentView parentView = new ParentView(view);
        return parentView;
    }

    @Override
    public ChildView onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_view, parent, false);
        ChildView childView = new ChildView(view);
        return childView;
    }

    @Override
    public void onBindChildViewHolder(final ChildView holder, int flatPosition, ExpandableGroup group, int childIndex) {
        DataValue dataValue = ((MainData) group).getItems().get(childIndex);
        holder.Bind(dataValue);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //String editTextData = ((EditText) view).getText().toString().trim();
                String editTextData = holder.editText.getText().toString().trim();

                if (holder.editText!=null) {
                    listener.getInput(editTextData);
                }
            }
        });
    }

    @Override
    public void onBindGroupViewHolder(ParentView holder, int flatPosition, ExpandableGroup group) {
        holder.getTitle(group);
    }
}

我认为它会对你有所帮助。