我已经尝试了一段时间(同时在这里搜索),当我在其他片段中按下按钮时,我可以帮助我在MainFragment中刷新listView。 sommeone可以解释我如何使它工作?
这里有MainFragment功能(我排除了其他功能,因为我不知道他们是如何贡献的):
public class MainFragment extends Fragment {
public void otherList() {
SQLite db = new SQLite(getActivity());
db.open();
Calendar sCalendar = Calendar.getInstance();
String day = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK,
Calendar.LONG, Locale.ENGLISH);
Cursor c = db.getAllRecords(day);
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
arrayOfBars.clear();
if (c.moveToFirst()) {
do {
String country = c.getString(0);
String city = c.getString(1);
String bar = c.getString(2);
String timeStart = c.getString(3);
String[] h = timeStart.split(":");
int times = Integer.parseInt(h[0]);
String timeEnd = c.getString(4);
String[] h2 = timeEnd.split(":");
int timee = Integer.parseInt(h2[0]);
String placeLaLo = c.getString(5);
String description = c.getString(6);
String likes = c.getString(7);
String offer = c.getString(8);
if (hour < (times + 1) || hour < (timee)) {
getPrefs = PreferenceManager
.getDefaultSharedPreferences(MainFragment.this
.getActivity());
boolean lay = getPrefs.getBoolean("boolean", false);
if (lay == true) {
while (times < timee) {
timeStart = times + ":" + h[1];
times++;
timeEnd = times + ":" + h2[1];
arrayOfBars.add(new BarObject(country, city, bar,
timeStart, timeEnd, placeLaLo, description,
likes, offer, bar));
}
} else {
arrayOfBars.add(new BarObject(country, city, bar,
timeStart, timeEnd, placeLaLo, description,
likes, offer, bar));
}
}
} while (c.moveToNext());
Collections.sort(arrayOfBars, new BarObject.CompST());
lv2.setAdapter(null);
adapter = new BarAdapter(getActivity(), arrayOfBars);
lv2.setAdapter(adapter);
} else {
Toast.makeText(getActivity(), "No database found",
Toast.LENGTH_LONG).show();
}
db.close();
}
}
这是另一个片段:
public class SocialMedia extends Fragment {
ImageButton facebook, twitter, layoutS;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.social_media, container, false);
layoutS = (ImageButton) view.findViewById(R.id.ibLayout);
layoutS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(SocialMedia.this
.getActivity());
boolean lay = getPrefs.getBoolean("boolean", false);
SharedPreferences.Editor e = getPrefs.edit();
lay = !lay;
e.putBoolean("boolean", lay);
e.commit();
//Here I want to call otherList() from MainFragment
}
});
return view;
}
}
答案 0 :(得分:8)
FragmentManager fm = getFragmentManager();
MainFragment fragm = (MainFragment)fm.findFragmentById(R.id.main_fragment);
fragm.otherList();
此代码最适合我。而且看起来很容易
答案 1 :(得分:5)
在MainFragment类中,您可以执行以下代码:
private static MainFragment instance = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
public static MainFragment getInstance() {
return instance;
}
在SocialMedia类中,您可以按以下方式调用方法:
MainFragment.getInstance().otherList();
答案 2 :(得分:3)
你应该使用interface
。在interface
课程中定义OtherFragment
并在MainActivity
中实施,并在public
中定义MainFragment
方法,以便刷新ListView
并致电来自MainActivity
的方法。这是一个例子:
public Class OtherFragment extends Fragment implements OnClickListener {
private Communicator communicator;
...
public void setCommunicator(Communicator communicator) {
this.communicator = communicator;
}
@Override
public void onClick(View v) {
communicator.clicked();
}
public interface Communicator {
public void clicked();
}
}
和您的MainActivity
:
public class MainActivity extends Activity implements OtherFragment.Communicator {
MainFragment mainFragment;
@Override
protected void onCreate(Bundle b) {
...
OtherFragment otherFragment = new OtherFragment();
otherFragment.setCommunicator(this);
...
}
...
@Override
public void clicked() {
mainFragment.updateList();
}
}
和您的MainFragment
:
public class MainFragment extends Fragment {
...
public void updateList() {
// update list
}
}
答案 3 :(得分:0)
要从另一个对象调用方法(Fragment是一个对象),您需要具有该对象的引用。因此,如果您想从MainFragment#otherList()
个实例致电SocialMedia
,SocialMedia
需要引用MainFragment
。
例如:
public class SocialMedia {
private MainFragment mainFragment = null;
public void setMainFragment(final MainFragment mainFragment) {
this.mainFragment = mainFragment
}
}
然后您可以使用this.mainFragment.otherList()
。
答案 4 :(得分:0)
sftpserver