ExpandableListActivity get Text



我在每个项目中都有ExpandableListActivity Text。如何获取点击列表项的文本?

这是我的做法:

        groupData = application.getFirstLayer();
        String groupFrom[] = new String[] {"groupName"};
        int groupTo[] = new int[] {android.R.id.text1};
        childData = application.getSecondLayer();
        String childFrom[] = new String[] {"levelTwoCat"};
        int childTo[] = new int[] {android.R.id.text1};
        adapter = new SimpleExpandableListAdapter(
            this,
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            groupFrom,
            groupTo,
            childData,
            android.R.layout.simple_list_item_1,
            childFrom,
            childTo);

public boolean onChildClick(android.widget.ExpandableListView parent,
            View v, int groupPosition, int childPosition, long id) {}

我必须用onChildClick写什么才能看到当前项目的文本?

最简单的方法是直接从您单击的视图中获取它。 您尚未显示行 XML,因此以下代码将假定您有一个 LinearLayout,其中包含一个 TextView 作为您的行。

public boolean onChildClick(android.widget.ExpandableListView parent, 
        View v, int groupPosition, int childPosition, long id) {
            TextView exptv = (TextView)v.findViewById(R.id.yourtextview); //  Get the textview holding the text
            String yourText = exptv.getText().toString();  // Get the text from the view and put it in a string
            // use string as you need to
}

如果布局只是一个文本视图,您可以直接转到String yourText = v.getText().toString();因为传入的视图 v 将是您需要的文本视图。

编辑

正如 Jason Robinson 在他的评论中指出的那样,您正在将android.R.layout.simple_list_item_1用于子布局,因为这只是一个 TextView,它简化了您需要的代码:

public boolean onChildClick(android.widget.ExpandableListView parent, 
        View v, int groupPosition, int childPosition, long id) {
            String yourText = v.getText().toString();  // Get the text from the view and put it in a string
            // use string as you need to
}

最新更新