gpt4 book ai didi

java - Android ExpandableListView 在应用程序中不可见

转载 作者:行者123 更新时间:2023-11-30 11:05:21 24 4
gpt4 key购买 nike

我正在尝试在 Android 中制作一个 ExpandableListView 来显示问题列表。当我运行应用程序时,屏幕上没有显示任何内容。我不确定为什么或如何发生这种情况。谁能帮我解决这个问题。

我不想明确表示我从服务器获取数据,这些数据保存在对象中(也称为问题)。我已经对此进行了很多测试,这段代码没有任何问题。因此要显示的数据可用但不会显示在应用中。

我正在使用以下代码:

PlaceHolderFragment:

public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

// Progress dialog
private ProgressDialog pDialog;

private static String TAG = Problems.class.getSimpleName();

private ArrayList<Problem> myProblems = new ArrayList<Problem>();

private SparseArray<Group> groups = new SparseArray<Group>();

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

//Here comes code that handles data input from server ( which correctly works btw )

for(int i = 0; i < myProblems.size(); i++){
Group group = new Group(myProblems.get(i).getTitle());
group.children.add(myProblems.get(i).domainsToString());
group.children.add(myProblems.get(i).getDescription());
groups.append(i, group);
}

View rootView = inflater.inflate(R.layout.fragment_my_problems, container, false);

mMyProblems = (ExpandableListView) rootView.findViewById(R.id.my_problems);
//mMyProblems.setAdapter(adapterMyP);
MyProblemExpandableListViewAdapter adapter = new MyProblemExpandableListViewAdapter(this.getActivity(), groups);
mMyProblems.setAdapter(adapter);

return rootView;
}

private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}

private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}

}

适配器:

public class MyProblemExpandableListViewAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
public LayoutInflater inflater;
public Activity activity;

public MyProblemExpandableListViewAdapter(Activity act, SparseArray<Group> groups) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(children);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, children,
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}

@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
return groups.size();
}

@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}

@Override
public long getGroupId(int groupPosition) {
return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Group group = (Group) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.string);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

}

和组类:

public class Group {
public String string;
public final List<String> children = new ArrayList<String>();

public Group(String string) {
this.string = string;
}
}

以及相应的 xml 文件:

fragementMyProblems.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wsi.wesupportit.Problems$PlaceholderFragment" >

<ExpandableListView
android:id="@+id/my_problems"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>

</LinearLayout>

listrow_group.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginLeft="8dp"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="14sp"
android:textStyle="bold" />

listrow_details.xml

<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="40dp"
android:clickable="true"android:orientation="vertical"
android:paddingLeft="40dp">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />

</LinearLayout>

提前致谢。

最佳答案

使您的 MyProblemExpandableListViewAdapter final 并将其放在类的顶部。并确保每次更改

的内容时都 通知您的适配器

关于java - Android ExpandableListView 在应用程序中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676127/

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