[英]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.