如何在expandableListView中的特定位置设置值

时间:2016-04-02 07:42:00

标签: android adapter expandablelistview

我已经创建了一个ExpandableListView.But当我在所有项目上设置特定项目的值相同时我有一个问题,我无法理解如何解决这个问题。

这是我的代码。

direct_referal.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutfaq"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="25dp" >

    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:cacheColorHint="#00000000"/>
</LinearLayout>

这是我的主要Xml文件。

DirectReferal.java

public class DirectReferalFragment extends Fragment 
{

    ExpandableDirectReferalListAdapter listAdapter;
    ExpandableListView expListView;
    private List<String> userID;
    private List<String> joiningDate;
    private HashMap<String, List<String>> slNo;
    private HashMap<String, List<String>> name;
    private HashMap<String, List<String>> city;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.direct_referal, container, false);

        // get the listview
        expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
        expListView.setGroupIndicator(null);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableDirectReferalListAdapter(this.getActivity(), userID, joiningDate, slNo, name, city);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {

                return false;
            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                // TODO Auto-generated method stub
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                // TODO Auto-generated method stub
            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                return false;
            }
        });
        return view;
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        userID = new ArrayList<String>();
        joiningDate = new ArrayList<String>();
        slNo = new HashMap<String, List<String>>();
        name = new HashMap<String, List<String>>();
        city = new HashMap<String, List<String>>();

        // Adding USER ID
        userID.add("User ID : GLI000014");
        userID.add("User ID : GLI000015");
        userID.add("User ID : GLI000016");
        userID.add("User ID : GLI000017");
        userID.add("User ID : GLI000018");

        // Adding JOINING DATE
        joiningDate.add("Joining Date : 9 Nov,2015");
        joiningDate.add("Joining Date : 9 Dec,2015");
        joiningDate.add("Joining Date : 9 Jan,2016");
        joiningDate.add("Joining Date : 9 Fab,2016");
        joiningDate.add("Joining Date : 9 Mar,2016");

        // Adding SL NO
        List<String> no1 = new ArrayList<String>();
        no1.add("SL No. : 1");
        List<String> no2 = new ArrayList<String>();
        no2.add("SL No. : 2");
        List<String> no3 = new ArrayList<String>();
        no3.add("SL No. : 3");
        List<String> no4 = new ArrayList<String>();
        no4.add("SL No. : 4");
        List<String> no5 = new ArrayList<String>();
        no5.add("SL No. : 5");

        slNo.put(userID.get(0), no1); // Header, Child data
        slNo.put(userID.get(1), no2);
        slNo.put(userID.get(2), no3);
        slNo.put(userID.get(3), no4);
        slNo.put(userID.get(4), no5);

        // Adding NAME
        List<String> name1 = new ArrayList<String>();
        name1.add("Name : Prabal Saxena");
        List<String> name2 = new ArrayList<String>();
        name2.add("Name : Chirag Mehta");
        List<String> name3 = new ArrayList<String>();
        name3.add("Name : Tarun Mehta");
        List<String> name4 = new ArrayList<String>();
        name4.add("Name : Prashant Gupta");
        List<String> name5 = new ArrayList<String>();
        name5.add("Name : Chotu");

        name.put(userID.get(0), name1); // Header, Child data
        name.put(userID.get(1), name2);
        name.put(userID.get(2), name3);
        name.put(userID.get(3), name4);
        name.put(userID.get(4), name5);

        // Adding CITY
        List<String> city1 = new ArrayList<String>();
        city1.add("City : Noida");
        List<String> city2 = new ArrayList<String>();
        city2.add("City : Gurgaun");
        List<String> city3 = new ArrayList<String>();
        city3.add("City : Delhi");
        List<String> city4 = new ArrayList<String>();
        city4.add("City : Pune");
        List<String> city5 = new ArrayList<String>();
        city5.add("SL No. : Delhi");

        city.put(userID.get(0), city1); // Header, Child data
        city.put(userID.get(1), city2);
        city.put(userID.get(2), city3);
        city.put(userID.get(3), city4);
        city.put(userID.get(4), city5);
    }
}

这是我的主要Java文件。在这里我们设置所有子元素和父元素的值,并通过构造函数发送值。

list_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:background="@drawable/direct_referal_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblListItemslno"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:gravity="left"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/lblListItemname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:gravity="left"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/lblListItemcity"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:gravity="left"
        android:textColor="#000000" />

</LinearLayout>

list_group.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp" 
    android:background="#333333">

    <TextView
        android:id="@+id/lblListHeaderuserid"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="User ID : GLI000014"
        android:textSize="12sp"
        android:gravity="center"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/lblListHeaderjoindate"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Joining Date : 9 Nov, 2015"
        android:textSize="12sp"
        android:gravity="center"
        android:textColor="#FFFFFF" />

</LinearLayout>

ExpandableDirectReferalListAdapter.java

public class ExpandableDirectReferalListAdapter extends BaseExpandableListAdapter {

    private Context _context;

    List<String> _userID;
    List<String> _joiningDate;
    HashMap<String, List<String>> _slNo;
    HashMap<String, List<String>> _name;
    HashMap<String, List<String>> _city;

    public ExpandableDirectReferalListAdapter(Context context, List<String> userID,List<String> joiningDate,
                                              HashMap<String, List<String>> slNo,HashMap<String, List<String>> name,
                                              HashMap<String, List<String>> city) {
        this._context = context;
        this._userID = userID;
        this._joiningDate = joiningDate;
        this._slNo = slNo;
        this._name = name;
        this._city = city;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._slNo.get(this._userID.get(groupPosition))
                .get(childPosititon);
    }

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

    @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 txtListChildslno = (TextView) convertView.findViewById(R.id.lblListItemslno);
        TextView txtListChildname = (TextView) convertView.findViewById(R.id.lblListItemname);
        TextView txtListChildcity = (TextView) convertView.findViewById(R.id.lblListItemcity);

        txtListChildslno.setText(childText);
        txtListChildname.setText(childText);
        txtListChildcity.setText(childText);


        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._slNo.get(this._userID.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._userID.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater=(LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeaderuserid = (TextView) convertView.findViewById(R.id.lblListHeaderuserid);
        TextView lblListHeaderjoindate = (TextView) convertView.findViewById(R.id.lblListHeaderjoindate);

        lblListHeaderuserid.setText(headerTitle);
        lblListHeaderjoindate.setText(headerTitle);

        return convertView;
    }

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

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

}

这是我的Adapter类。在此我们要设置所有子字段和父字段的不同值,但在所有字段上设置相同的值。 你可以在这张图片中看到输出.. enter image description here

我怎么能解决这个问题,我无法理解。 请帮忙。

1 个答案:

答案 0 :(得分:1)

请更正getChildView方法gand etGroupView方法。代码如下。

public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        String slNo = _slNo.get(_userID.get(groupPosition)).get(0);
        String childName = _name.get(_userID.get(groupPosition)).get(0);
        String childCity = _city.get(_userID.get(groupPosition)).get(0);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChildslno = (TextView) convertView.findViewById(R.id.lblListItemslno);
        TextView txtListChildname = (TextView) convertView.findViewById(R.id.lblListItemname);
        TextView txtListChildcity = (TextView) convertView.findViewById(R.id.lblListItemcity);

        txtListChildslno.setText(slNo);
        txtListChildname.setText(childName);
        txtListChildcity.setText(childCity);

        return convertView;
    }




@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater=(LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeaderuserid = (TextView) convertView.findViewById(R.id.lblListHeaderuserid);
    TextView lblListHeaderjoindate = (TextView) convertView.findViewById(R.id.lblListHeaderjoindate);

    lblListHeaderuserid.setText(headerTitle);
    lblListHeaderjoindate.setText(_joiningDate.get(groupPosition));

    return convertView;
}

希望你能理解你的错误是什么...... 我测试了代码,它会工作。