可扩展ListView显示子组视图而不是组视图

时间:2015-04-19 07:44:09

标签: android android-listview

我在我的应用中实现了ExpandableList。 当我完成它完全错误。
列表只有一行,并在其中显示行标题视图中的子数据,
这是我的代码:

我的列表adpater:

public class JoinUsAdapter extends BaseExpandableListAdapter {

    public static final String DESCRIPTION_TG = "description";
    public static final String TITLE_TG = "title";
    private Context mContext;
    private List<Map<String, String>> mData;
    private LayoutInflater inflater;
    private ViewHolder vholder;

    public JoinUsAdapter(Context context, List<Map<String, String>> data) {
        this.mContext = context;
        this.mData = data;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Map<String , String> getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).get(DESCRIPTION_TG);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_title_join_us, null);
            vholder = new ViewHolder();
            vholder.txtTitle = (TextView) convertView.findViewById(R.id.txtNewsTitle);
            vholder.txtShortDesc = (TextView) convertView.findViewById(R.id.txtNewsShortDesc);
            convertView.setTag(vholder);
        }
        else {
            vholder = (ViewHolder) convertView.getTag();
        }
        Map item = getGroup(groupPosition);
        vholder.txtTitle.setText(item.get(TITLE_TG).toString());
        vholder.txtTitle.setText(truncate(item.get(DESCRIPTION_TG).toString(),35)+" ...");
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_detail_join_us, null);
        }
        TextView txtLongDesc =(TextView) convertView.findViewById(R.id.txtLongDescription);
        txtLongDesc.setText(getChild(groupPosition,childPosition));
        return convertView;
    }

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

这是我的布局: title(作为列表行视图):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:gravity="center"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:id="@+id/txtNewsTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TITLE"
            android:textStyle="bold"
            android:textColor="@color/white"/>
    <TextView
            android:id="@+id/txtNewsShortDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="news description news description news description news description"
            android:textStyle="bold"
            android:textColor="@color/white"/>

</LinearLayout> 

细节:(作为行的子项)

<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/txtLongDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"
            android:text="long description long description long description long description long description long description "/>

</LinearLayout>

我无法找到我的错误:/

更新:
我创建虚假数据的方法:

public void getData() {
        dataList = new ArrayList<Map<String, String>>();
        Map<String, String> item = new HashMap<String, String>();
        String fake = "We asked respondents how satisfied they are with their current job or jobs. 76% of developers report being at least satisfied with their job, and 36% love their job. Developers are generally more fulfilled by work than most employees.\n" +
                "\n" +
                "And developers in Iran are more satisfied with their jobs than developers anywhere else. Stack Overflow Careers may not have any jobs available in Iran, but you can still move there and apply for one of our many available remote jobs.";
        for(int i=0; i<10; i++) {
            item.put(JoinUsAdapter.TITLE_TG,"Title "+i);
            item.put(JoinUsAdapter.DESCRIPTION_TG,fake);
            dataList.add(item);
        }

0 个答案:

没有答案