Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ImageButton设置为仅对其中一个列表组在ExpandableListView中可见_Java_Android_Android Fragments_Expandablelistview - Fatal编程技术网

Java 将ImageButton设置为仅对其中一个列表组在ExpandableListView中可见

Java 将ImageButton设置为仅对其中一个列表组在ExpandableListView中可见,java,android,android-fragments,expandablelistview,Java,Android,Android Fragments,Expandablelistview,我有一个ExpandableListView,其中我只希望其中一个组有一个ImageButton。我目前正在隐藏它的可见性:在组的布局中,并计划使其仅对其中一个组可见,其中部分组的标题是“最喜爱的位置”。在第行创建特定组: Section oGeneralSection = new Section("Favorite Locations"); 我当前在SectionListAdapter.java中的尝试 已编辑-下面的代码现在可以工作了 @Override public View g

我有一个ExpandableListView,其中我只希望其中一个组有一个ImageButton。我目前正在隐藏它的可见性:在组的布局中,并计划使其仅对其中一个组可见,其中部分组的标题是“最喜爱的位置”。在第行创建特定组:

Section oGeneralSection = new Section("Favorite Locations");
我当前在SectionListAdapter.java中的尝试

已编辑-下面的代码现在可以工作了

    @Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.slidingmenu_sectionview,
                parent, false);
    }

    TextView textView = (TextView) convertView
            .findViewById(R.id.slidingmenu_section_title);
    textView.setText(((Section) getGroup(groupPosition)).getTitle());

    //TODO make this set the add_image_button to VISIBLE
    if( sections.get(groupPosition).getTitle().equalsIgnoreCase("Favorite Locations") ){
        convertView.findViewById(R.id.favoritelocations_addbutton).setVisibility(View.VISIBLE);

        }else{
            convertView.findViewById(R.id.favoritelocations_addbutton).setVisibility(View.GONE);
        }

    return convertView;
}
程序正在抛出NullPointerException

09-17 22:18:06.566: W/dalvikvm(24531): threadid=1: thread exiting with uncaught exception (group=0x40cf8438)
09-17 22:18:06.636: E/AndroidRuntime(24531): FATAL EXCEPTION: main
09-17 22:18:06.636: E/AndroidRuntime(24531): java.lang.NullPointerException
09-17 22:18:06.636: E/AndroidRuntime(24531):    at com.planner.SectionListAdapter.getGroupView(SectionListAdapter.java:85)
09-17 22:18:06.636: E/AndroidRuntime(24531):    at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
09-17 22:18:06.636: E/AndroidRuntime(24531):    at android.widget.AbsListView.obtainView(AbsListView.java:2271)
09-17 22:18:06.636: E/AndroidRuntime(24531):    at android.widget.ListView.makeAndAddView(ListView.java:1769)
SlidingMenuFragment.java-包含并创建“收藏夹位置”

公共类SlidingMenuFragment扩展片段实现ExpandableListView.OnChildClickListener{
私有可扩展ListView部分ListView;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
List sectionList=createMenu();
视图=充气机。充气(R.layout.slidingmenu\u片段,容器,false);
this.sectionListView=(ExpandableListView)view.findViewById(R.id.slidingmenu\u视图);
this.sectionListView.setGroupIndicator(null);
SectionListAdapter SectionListAdapter=新的SectionListAdapter(this.getActivity(),sectionList);
这个.sectionListView.setAdapter(sectionListAdapter);
this.sectionListView.setOnGroupClickListener(新的ExpandableListView.OnGroupClickListener(){
@凌驾
公共布尔onGroupClick(ExpandableListView父视图、视图v、int-groupPosition、长id){
返回true;
}
});
this.sectionListView.setOnChildClickListener(this);
int count=sectionListAdapter.getGroupCount();
用于(int位置=0;位置<计数;位置++){
此.sectionListView.expandGroup(位置);
}
返回视图;
}
/**
*通过将所有项目添加到节列表中,创建滑动菜单的菜单
*@return section列出包含节项的节列表
*/
私有列表创建菜单(){
List sectionList=新建ArrayList();
//TODO在mStartSection和mEndSection上实现设置当前值
截面mStartSection=新截面(“起始位置”);
mStartSection.addSectionItem(sID_设置_和提示_PERM_开始_LOC,“设置永久”、“滑动菜单_设置永久”);
mStartSection.addSectionItem(sID_CLEAR_PERM_START_LOC,“永久清除”、“滑动菜单清除”);
//mStartSection.addSectionItem(sID_SET_CURRENT_AS_START,“SET CURRENT”,“slidingmenu\u friends”);
截面=新截面(“端部位置”);
mEndSection.addSectionItem(sID_设置_和_提示_PERM_END_LOC,“设置永久性”、“滑动菜单_设置永久性”);
mEndSection.addSectionItem(sID_CLEAR_PERM_END_LOC,“永久清除”、“滑动菜单清除”);
//mEndSection.addSectionItem(sID_SET_CURRENT_AS_END,“SET CURRENT”,“sliding menu_friends”);
截面=新截面(“最喜欢的位置”);
章节列表。添加(mStartSection);
章节列表。添加(章节);
章节列表。添加(章节);
返回分区列表;
}
...
}
slidingmenu_sectionview.xml-其中ImageButton“favoritelocations_addbutton”设置为不存在

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
       android:id="@+id/slidingmenu_section_title"
       style="?android:attr/listSeparatorTextViewStyle"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Section"
       android:textSize="22sp"
       android:paddingTop="7dp"
       android:paddingBottom="7dp"
       android:background="@android:color/transparent"
       android:textColor="#FFFFFF"
       android:textStyle="bold|italic" />

    <ImageButton 
        android:id="@+id/favoritelocations_addbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_action_add"
        android:visibility="gone" />

</RelativeLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/white"/>

</LinearLayout>

将图像按钮的可视性设置为在部分列表适配器的getView()中消失/可见。像这样的

if( sectionList.get(position).get(Something).equalsIgnoreCase("Favorite Locations") ){

findViewById(R.id.ImageButton).setVisibilty(View.Visible)

}else{

    findViewById(R.id.ImageButton).setVisibilty(View.Gone)
}


请注意,根据检查将visibility切换为visibility is Visible and gone非常重要,因为如果您只检查匹配的案例,并将visibility切换为visibility,而忘记将visibility更改为gone,则其他场景中的每个项目的imageButton都将可见。

MySectionListAdapter只有getChildView()和getParentView(),而没有getView()。我会将您的代码粘贴到getParentView()中吗?您可以尝试一下。我发布了我的尝试,结果是NullPointerException。也许你可以看一看?你正在搜索一个可绘制的,而不是一个视图。您必须找到convertView.findViewById(R.id.your_ImageButton_id)使用的ImageButton。R.drawable.ic_action_add是drawable文件夹中的一个资源,或者更确切地说是一个图像。这不是一种观点。
if( sectionList.get(position).get(Something).equalsIgnoreCase("Favorite Locations") ){

findViewById(R.id.ImageButton).setVisibilty(View.Visible)

}else{

    findViewById(R.id.ImageButton).setVisibilty(View.Gone)