简体   繁体   English

如何从ListAdapter更改片段的Lis​​tView中的数据?

[英]How do I change data in a ListView of a Fragment from a ListAdapter?

I have a Fragment which contains a few ListViews. 我有一个片段,其中包含一些ListViews。 I have my ListAdapter, which customizes the ListViews so that only one group will show a button and add an OnClickListener to that button. 我有我的ListAdapter,它可以自定义ListView,以便只有一个组将显示一个按钮,并向该按钮添加OnClickListener。 I am trying to make the OnClickListener add an item to one of my lists in my Fragment, but the problem is that I cannot find out how to interact with any of the methods in my Fragment. 我试图使OnClickListener将一个项目添加到我的Fragment中的一个列表中,但是问题是我无法找出如何与Fragment中的任何方法进行交互。 Perhaps, there is also a better way to do this than directly through the ListAdapter. 也许,还有比直接通过ListAdapter更好的方法。

SlidingMenuFragment.java - the fragment SlidingMenuFragment.java-片段

/**
 * Adds a child to favorite locations section list
 * @param v the view
 * @param location the location to add
 */
public void addFavoriteLocation(View v, String location){
    mFavoriteLocationsSection.addSectionItem(99, "test location", "slidingmenu_clear");//TODO change dummy values
    sectionListAdapter.notifyDataSetChanged();
}

SectionListAdapter.java - the list adapter 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());

    //Sets the group "Favorite Locations" to have the only add_button as VISIBLE, and other groups to GONE
    //Set an onClickListener to add button as well
    if( sections.get(groupPosition).getTitle().equalsIgnoreCase("Favorite Locations") ){
        convertView.findViewById(R.id.favoritelocations_addbutton).setVisibility(View.VISIBLE);//works
        convertView.findViewById(R.id.favoritelocations_addbutton).setOnClickListener(new OnClickListener() {//doesnt

            @Override
            public void onClick(View v) {
                //I am trying to call addFavoriteLocation(...) here, but can't figure out how to do so
            }
        });
        }else{
            convertView.findViewById(R.id.favoritelocations_addbutton).setVisibility(View.GONE);
        }

    return convertView;
}

I finally figured it out...inside my OnClickListener 我终于想通了...在我的OnClickListener里面

MainActivity mainAct = (MainActivity) context;
mainAct.addFavoriteLocation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从片段创建新的listadapter。 知道如何通过活动而不是片段来做到这一点 - Create new listadapter from a fragment. Know how to do it from an activity but not from a fragment 如何从片段本身中更改片段的文本视图? - How do I change the textview of a Fragment from WITHIN the fragment itself? 如何为片段设置listadapter - how to set listadapter for fragment 我正试图将数据从Firebase数据库显示到ListView,但是在创建Listadapter时会出错 - I am tring to display data from firebase database to listview but getting eror when I create Listadapter 如何在片段中更新ListView? - How do I update a ListView in a Fragment? 如何让notifyDatasetChanged()与ListAdapter一起使用? - How do I get notifyDatasetChanged() to work with a ListAdapter? 如何在片段中使用ListView? - How do ListView in a Fragment? 使用ListAdapter时如何使ListView中的新数据无效 - How to invalidate ListView for new data when using ListAdapter 如何单击simpleAdapter列表视图以更改片段并获取数据? - How to click on the simpleAdapter listview to change the fragment and get the data? 如何从drawable创建imges的ArrayList并在片段ListView中显示它? - How do I create an ArrayList of imges from drawable and display it in fragment ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM