0

各アイテムに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現在のアイテムのテキストを表示するには、何を書く必要がありますか?

4

1 に答える 1

1

これを行う最も簡単な方法は、クリックしたビューから直接取得することです。行の XML を表示していないため、次のコードでは、行として TextView を含む LinearLayout があると想定しています。

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();の場合は、渡された View v が必要な TextView になるため、 直接移動できます。

編集

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
}
于 2012-06-13T15:13:38.173 に答える