在ExpandableListView

时间:2015-10-19 09:44:20

标签: android expandablelistview

我有一个ExpandableListView,其中包含2个TextView和一个EditText,用于为每个孩子输入金额,并允许内联编辑。

当孩子的一个金额发生变化时,我需要立即更改该组的金额值。

我已经这样做但问题是,当我调用notifyDataSetChanged()方法显示组中的更改时,我失去了当前edittext的焦点。也就是说,如果我更改了组的第一个子项中的金额字段,然后单击第二个子项的金额字段,则由于notifyDataSetChanged(),焦点仍然在第一个子项上。

我该如何解决这个问题?

我的代码如下。

ExpandableListAdapter.java:

package com.example.empandroid.view;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import com.fort.empandroid.R;
import com.example.empandroid.model.Employee;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private final Context context;
    private List<Employee> listDataHeader;
    private HashMap<Integer, List<Employee>> listDataChild;
    DecimalFormat df = new DecimalFormat("#.00");
    int s;
    Float value = 0.0f, change = 0.0f;

    public ExpandableListAdapter(Context context, List<Employee> listDataHeader, HashMap<Integer, List<Employee>> listChildData) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
        //this.context=mainContxt.getApplicationContext();
    }

    public static class ListHolder {
        Employee expCoa;
        TextView txtName;
        TextView txtAmount;
        TextView txtMonthTotal;
        EditText edtAmount;
    }

    @Override
    public int getGroupCount() {
        return this.listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int childcount = this.listDataChild.get(this.listDataHeader.get(groupPosition).getiD()).size();
        return childcount;
    }

    @Override
    public Employee getGroup(int groupPosition) {
        return this.listDataHeader.get(groupPosition);
    }

    @Override
    public Employee getChild(int groupPosition, int childPosition) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition).getiD()).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        df.setMinimumIntegerDigits(1);
        ListHolder expHolder = new ListHolder();
        expHolder.expCoa = getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        expHolder.txtName = (TextView) convertView.findViewById(R.id.txtAccount);
        expHolder.txtAmount = (TextView) convertView.findViewById(R.id.txtAmount);
        expHolder.txtMonthTotal = (TextView) convertView.findViewById(R.id.txtMonthTotal);
        convertView.setTag(expHolder);
        expHolder.txtName.setTypeface(null, Typeface.BOLD);
        expHolder.txtAmount.setTypeface(null, Typeface.BOLD);
        expHolder.txtMonthTotal.setTypeface(null, Typeface.BOLD);
        setupItem(expHolder);

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        df.setMinimumIntegerDigits(1);
        final ListHolder expHolder = new ListHolder();
        expHolder.expCoa = getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        expHolder.txtName = (TextView) convertView.findViewById(R.id.txtAccount);
        expHolder.edtAmount = (EditText) convertView.findViewById(R.id.edtAmount);
        expHolder.txtMonthTotal = (TextView) convertView.findViewById(R.id.txtMonthTotal);
        convertView.setTag(expHolder);
        setupChildItem(expHolder);
        //expHolder.edtAmount.requestFocus();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        //AlertDialog.Builder alert = new AlertDialog.Builder(mainContxt);
        expHolder.edtAmount.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    onChange(v, groupPosition, childPosition, expHolder.expCoa);

                } else {
                    //Toast.makeText(context, " else "+s, 68000).show();
                    onFocus(v);
                }
            }
        });
        return convertView;
    }

    private void onFocus(View v) {
        final EditText Caption = (EditText) v;
        value = Float.parseFloat(Caption.getText().toString());
        Toast.makeText(context, "onFocus value " + value + " change " + change, 68000).show();
    }

    public void onChange(View views, int groupPosition, int childPosition, Employee expCoaChd) {
        final int position = views.getId();
        final EditText Caption = (EditText) views;
        change = Float.parseFloat(Caption.getText().toString());
        if (value != change) { //use database type for this loop condition
            //perform change
            ListHolder expHolders = new ListHolder();
            expHolders.expCoa = getGroup(groupPosition);
            expHolders.expCoa.setAmount(expHolders.expCoa.getAmount() - expCoaChd.getAmount() + change);
            expCoaChd.setAmount(change);
            notifyDataSetChanged();
            //Toast.makeText(context, "onChange value "+value+" change "+change, 68000).show();
            change = 0.0f;
            value = 0.0f;
        }
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private void setupChildItem(ListHolder expHolder) {
        expHolder.txtName.setText(expHolder.expCoa.getStrName());
        if (expHolder.expCoa.getiD() == -998 || expHolder.expCoa.getiD() == -999) {
            expHolder.edtAmount.setText("");
            expHolder.txtMonthTotal.setText("");
        } else {
            expHolder.edtAmount.setText(df.format(expHolder.expCoa.getAmount()));
            expHolder.txtMonthTotal.setText(df.format(expHolder.expCoa.getExpenseMTD()));
        }
    }

    private void setupItem(ListHolder expHolder) {
        expHolder.txtName.setText(expHolder.expCoa.getStrName());
        if (expHolder.expCoa.getiD() == -998 || expHolder.expCoa.getiD() == -999) {
            expHolder.txtAmount.setText("");
            expHolder.txtMonthTotal.setText("");
        } else {
            expHolder.txtAmount.setText(df.format(expHolder.expCoa.getAmount()));
            expHolder.txtMonthTotal.setText(df.format(expHolder.expCoa.getExpenseMTD()));
        }
    }

}

list_group.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
>
<TextView
    android:id="@+id/txtAccount"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:textSize="16sp"
    android:gravity="center_vertical"
    android:layout_weight="3"/>

<TextView
    android:id="@+id/txtAmount"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:gravity="end"
    android:textSize="15sp"
    android:textAppearance="?android:attr/textAppearanceSmall"/>

<TextView
    android:id="@+id/txtMonthTotal"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:gravity="end"
    android:textSize="15sp"
    android:textAppearance="?android:attr/textAppearanceSmall"/>

 </LinearLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
>
<TextView
    android:id="@+id/txtAccount"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:textSize="15sp"
    android:gravity="center_vertical"
    android:layout_weight="3"/>

<EditText
    android:id="@+id/edtAmount"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:gravity="end"
    android:textSize="15sp"
    android:textAppearance="?android:attr/textAppearanceSmall"/>

<TextView
    android:id="@+id/txtMonthTotal"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:gravity="end"
    android:textSize="15sp"
    android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>

0 个答案:

没有答案