我想要獲取此列表中被點擊的子項的值,並將其作爲鍵值對。但這個執行後:從ExpandableListView.OnChildClickListener獲取值
expandableListView
.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(
ExpandableListView parent, View v,
int groupPosition, int childPosition,
long id) {
Intent i = new Intent(v.getContext(), NumerekActivity.class);
View child_view = placowkaGrupaAdapter.getChildView(groupPosition, childPosition, false, v, parent);
i.putExtra("key", child_view.toString());
我得到了一個錯誤:
我試圖讓這個列表的點擊孩子的價值,並把它作爲鍵值對。但在執行上述操作之後,我得到了一個錯誤。
02-12 23:07:38.869 15940-15940/net.dbcoder.kolejka.android E/AndroidRuntime: FATAL EXCEPTION: main Process: net.dbcoder.kolejka.android, PID: 15940 java.lang.IndexOutOfBoundsException: Invalid index 5, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at net.dbcoder.kolejka.android.adapters.PlacowkaGrupaAdapter.getChild(PlacowkaGrupaAdapter.java:62) at net.dbcoder.kolejka.android.adapters.PlacowkaGrupaAdapter.getChildView(PlacowkaGrupaAdapter.java:95) at net.dbcoder.kolejka.android.listaPlacowek$2.onChildClick(listaPlacowek.java:93)
難道是好方法嗎?如何操作?
適配器類groupPosition,childPosition
public class PlacowkaGrupaAdapter extends BaseExpandableListAdapter {
private List<String> header_titles;
private HashMap<String,List<String>> child_items;
private Context ctx;
public PlacowkaGrupaAdapter(PlacowkaGrupa[] placowkaGrupas, Context ctx) {
List<String> header_titles = new ArrayList<>();
HashMap<String,List<String>> child_items = new HashMap<>();
for (PlacowkaGrupa placowkaGrupa : placowkaGrupas){
header_titles.add(placowkaGrupa.getPlacowka().getNazwa_placowki());
child_items.put(placowkaGrupa.getPlacowka().getNazwa_placowki(),placowkaGrupa.getNazwa_grupy());
}
this.ctx = ctx;
this.header_titles = header_titles;
this.child_items = child_items;
}
@Override
public int getGroupCount() {
return header_titles.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child_items.get(header_titles.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return header_titles.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child_items.get(header_titles.get(groupPosition)).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) {
String title = (String)this.getGroup(groupPosition);
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.headliny,null);
}
TextView textView = (TextView) convertView.findViewById(R.id.heading_item);
textView.setTypeface(null, Typeface.BOLD);
textView.setText(title);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String title = (String) this.getChild(groupPosition,childPosition);
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.chilitemy,null);
}
TextView textView = (TextView) convertView.findViewById(R.id.child_item);
textView.setText(title);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
值合適我通過顯示測試了它的價值在另一個活動,他們符合我的list.So選擇唯一的事情就是如何正確的呼叫轉接方法或獲取以某種方式選擇這些值。
適配器是用空白的對象集聲明的,但是加載器添加它們並列出顯示的內容確實很好。也許這兩件事情是分開的我的意思是展示事物和底層價值......我在這裏很困惑。
placowkaGrupaAdapter = new PlacowkaGrupaAdapter(new PlacowkaGrupa[]{},this);
expandableListView.setAdapter(placowkaGrupaAdapter);
getLoaderManager().initLoader(0, null, this);
我添加了適配器類和註釋。請看一下。 –
我敢肯定,你應該調用方法是: '查看child_view =(查看)placowkaGrupaAdapter.getChild(groupPosition,childPosition);' –
不幸的是,同樣的事情發生了: 'java.lang.IndexOutOfBoundsException:無效指數...' –