我发现很多关于实现Android的SwipeRefreshLayout this being the one I followed的文章,他们都说这很简单,但我似乎无法让我的工作。我做错了什么?
package myPackage;
public class MyClass extends Fragment {
private View rootView;
private DatabaseHelper myDBHelper;
private Cursor dataCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private ListView rowOfDataListView;
private SwipeRefreshLayout mySwipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_awesome_layout, container, false);
rowOfDataListView = (ListView) rootView.findViewById(R.id.row_of_data);
mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_container);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState){
super.onViewCreated(rootView, savedInstanceState);
if(getArguments() != null) {
idNumber = getArguments().getString("id");
}
myDBHelper = new DatabaseHelper(getActivity());
dataCursor = myDBHelper.getDataCursor(idNumber);
String[] fromColumns = {"column1", "column2", "column3", "column4", "column5", "column6"};
int[] toViews = {R.id.column1_textview, R.id.column2_textview, R.id.column3_textview, R.id.column4_textview, R.id.column5_textview, R.id.column6_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.row_of_data_layout, dataCursor, fromColumns, toViews, 0);
rowOfDataListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mySwipeRefreshLayout.setRefreshing(false);
}
});
}
}
我的数据最初显示正确,但刷新不会刷新。
答案 0 :(得分:1)
我看不到你在哪里更新数据onRefresh。我认为你应该将构建View的代码分成一个新方法,在你的onViewCreated方法中调用它,然后在onRefresh中再次调用它。否则,onRefresh会被触发,但它不会“做”任何事情。
package myPackage;
public class MyClass extends Fragment {
private View rootView;
private DatabaseHelper myDBHelper;
private Cursor dataCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private ListView rowOfDataListView;
private SwipeRefreshLayout mySwipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_awesome_layout, container, false);
rowOfDataListView = (ListView) rootView.findViewById(R.id.row_of_data);
mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_container);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState){
super.onViewCreated(rootView, savedInstanceState);
methodToBuildTheData();
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mySwipeRefreshLayout.setRefreshing(false);
methodToBuildTheData();
}
});
}
private void methodToBuildTheData() {
if(getArguments() != null) {
idNumber = getArguments().getString("id");
}
myDBHelper = new DatabaseHelper(getActivity());
dataCursor = myDBHelper.getDataCursor(idNumber);
String[] fromColumns = {"column1", "column2", "column3", "column4", "column5", "column6"};
int[] toViews = {R.id.column1_textview, R.id.column2_textview, R.id.column3_textview, R.id.column4_textview, R.id.column5_textview, R.id.column6_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.row_of_data_layout, dataCursor, fromColumns, toViews, 0);
rowOfDataListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
}