从ExpandableListView.OnChildClickListener获取值

我想要获取此列表中被点击的子项的值,并将其作为键值对。但这个执行后:从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);

回答:

从您的日志似乎placowkaGrupaAdapter没有任何孩子的意见都没有。 (无效的索引5,大小为0)

我建议确保适配器的所有初始化都在此发生之前执行。

另外,在我看来,从这小部分代码中,您可以简单地使用View视图函数参数。

如果您要提供更多代码或详细说明此代码的用途,我很乐意提供帮助。

但是,这些是我可以根据此代码给出的建议。

以上是 从ExpandableListView.OnChildClickListener获取值 的全部内容, 来源链接: utcz.com/qa/260631.html

回到顶部