gpt4 book ai didi

java - 如何解决问题: "java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)"

转载 作者:行者123 更新时间:2023-12-01 10:50:23 25 4
gpt4 key购买 nike

我在我的 fragment 类上使用 ExpandableListView,运行时出现以下错误,

 `java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)` on below lines

在下面的代码中,useroccasion 是我的适配器,occasionview 是我的可扩展 ListView

 userOccasions = new UserOccasions(getActivity(), R.layout.invitation, eventMOs);

occasionView.setAdapter(userOccasions);

我的可扩展 ListView 的布局代码如下,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<ExpandableListView
android:id="@+id/invitation_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbarStyle="outsideOverlay" />

</LinearLayout>

我的 fragment 代码如下,

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
this.inflater = inflater;
this.container = container;
this.savedInstanceState = savedInstanceState;
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
final View view = inflater.inflate(R.layout.invitation_tab, container, false);
context = getActivity().getApplicationContext();
prgDialog = new ProgressDialog(getActivity());
eventId.clear();
eventName.clear();
eventPlace.clear();
prgDialog.setMessage("Please wait...");
prgDialog.setCancelable(false);

dbHelper = new DatabaseHelper(context);
userMO = dbHelper.getUserData(1);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
return eventDelegates.getAllInvites(userMO, context);
}

@Override
protected void onPostExecute(String eventLists) {
if (eventLists != "null") {
eventMOs = gson.fromJson(eventLists, new TypeToken<List<EventMO>>() {
}.getType());
Toast.makeText(context, "total items of eventMo" + eventMOs.size(), Toast.LENGTH_LONG).show();

for (EventMO eventMO : eventMOs) {
eventName.add(eventMO.getText());
eventId.add(eventMO.getEventId());
eventPlace.add(eventMO.getPlace());
}

prgDialog.dismiss();
occasionView = (ExpandableListView) view.findViewById(R.id.invitation_list_view);
occasionView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {

if (groupPosition != lastExpandedGroupPosition) {
occasionView.collapseGroup(lastExpandedGroupPosition);

}
lastExpandedGroupPosition = groupPosition;

}
});
userOccasions = new UserOccasions(getActivity(), R.layout.invitation, eventMOs);
occasionView.setAdapter(userOccasions);
occasionView.setItemsCanFocus(false);
occasionView.setTextFilterEnabled(true);
occasionView.setOnItemClickListener(occasionView.getOnItemClickListener());
}
}
}.execute(null, null, null);

return view;
}
private class UserOccasions extends ArrayAdapter<EventMO> {
private ArrayList<EventMO> eventMOs;
LayoutInflater mInflater;

UserOccasions(Context context, int textViewResourceId,
ArrayList<EventMO> eventMOs) {
super(context, textViewResourceId, eventMOs);
this.eventMOs = new ArrayList<EventMO>();
this.eventMOs.addAll(eventMOs);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

private class ViewHolder {
TextView eventNameTxtV;
TextView eventPlaceTxtV;
CheckBox cb;
}

@Override
public int getCount() {
return eventMOs.size();
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (null == convertView) {
convertView = mInflater.inflate(R.layout.invitation, null);
holder = new ViewHolder();
holder.eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
holder.eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationPlace);
holder.cb = (CheckBox) convertView.findViewById(R.id.invitationCheckBox);

holder.cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
EventMO eventMO = (EventMO) checkBox.getTag();
eventMO.setSelected(checkBox.isChecked());

}
});
convertView.setTag(holder);
holder.cb.setTag(eventMOs.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.eventNameTxtV.setText(eventMOs.get(position).getText());
holder.eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
holder.cb.setChecked(eventMOs.get(position).isSelected());

View v = convertView.findViewById(R.id.invitation_single);

final LinearLayout eventLayout = (LinearLayout) convertView.findViewById(R.id.invitation_single);
//invitationCheckBox = (CheckBox) convertView.findViewById(R.id.invitationCheckBox);

final Button yesBtn = (Button) convertView.findViewById(R.id.yesButton);
final Button noBtn = (Button) convertView.findViewById(R.id.noButton);
final Button maybeBtn = (Button) convertView.findViewById(R.id.buttonMayBe);

final LinearLayout responseLayout = (LinearLayout) convertView.findViewById(R.id.hidden);
eventLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
responseLayout.setVisibility(View.VISIBLE);
break;
}
}


});

return convertView;

}

最佳答案

使用BaseExpandableListAdapter相反 ArrayAdapter

更改:private class UserOccasions extends ArrayAdapter<EventMO>

致:private class UserOccasions extends BaseExpandableListAdapter,并实现方法

教程:

http://examples.javacodegeeks.com/android/core/ui/expandablelistview/android-expandablelistview-example/

http://www.vogella.com/tutorials/AndroidListView/article.html

也许你想使用常用的 RecyclerView?

关于java - 如何解决问题: "java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33954803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com