如何刷新此片段中的ListView? 我有一个活动和一个片段,我想用SwipeRefreshLayout和一个FloatingActionButton刷新ListView。 这是我的片段:
public class NotesBottomFragment extends Fragment {
public final Object getInt(int num)
{
DatabaseNote dbn = new DatabaseNote();
SQLiteDatabase notes = getActivity().openOrCreateDatabase("ENAPLO", getActivity().MODE_PRIVATE, null);
String value2 = "novalue";
int value = 0;
Bundle extras = getActivity().getIntent().getExtras();
if(extras != null) {
value = dbn.getIdBySubject(notes, extras.getString(SubjectBottomFragment.SUB_CODE));
value2 = extras.getString(SubjectBottomFragment.SUB_NAME);
}
if (num == 1){
return value;
}
else {
return value2;
}
}
public NotesBottomFragment(){}
public static final String TAG = "NotesBottomFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView tv = (TextView)getActivity().findViewById(R.id.tvHead);
tv.setText(getInt(2) + " notes");
View rootView = inflater.inflate(R.layout.fragment_notes, container, false);
final ArrayList<ListviewNotes> listContact = GetlistContact();
final ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
ListviewNotesAdapter adapter = new ListviewNotesAdapter(getActivity(), listContact);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
ListviewNotes selectedListviewSubject = (ListviewNotes) listContact.get(position);
Toast.makeText(getActivity(), selectedListviewSubject.getDescription(), Toast.LENGTH_LONG).show();
}
});
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final DatabaseNote dbn = new DatabaseNote();
final SQLiteDatabase notes = getActivity().openOrCreateDatabase("ENAPLO", getActivity().MODE_PRIVATE, null);
final SwipeRefreshLayout swr = (SwipeRefreshLayout) getActivity().findViewById(R.id.swiperefresh);
final FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
swr.setColorSchemeResources(R.color.green, R.color.orange, R.color.blue);
swr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
fab.setVisibility(View.INVISIBLE);
dbn.refreshDatabase(notes, getContext(), getActivity().findViewById(android.R.id.content));
//here want I do the refresh
fab.setVisibility(View.VISIBLE);
}
}
);
}
private ArrayList<ListviewNotes> GetlistContact(){
ArrayList<ListviewNotes> contactlist = new ArrayList<ListviewNotes>();
ListviewNotes contact;
SQLiteDatabase notes = getActivity().openOrCreateDatabase("ENAPLO", getActivity().MODE_PRIVATE, null);
DatabaseNote dbn =new DatabaseNote();
dbn.Create(notes);
String tvin = "";
for (int i = 1; i <= dbn.getCount(notes, (Integer) getInt(1)); i++) {
String[] arr = dbn.getArray(notes, i, (Integer) getInt(1));
tvin = tvin + arr[0] + arr[1] + arr[2] + arr[3] + arr[4]+ "\n";
Log.d("ASD", "in");
contact = new ListviewNotes(dbn.getSubjectById(notes, Integer.parseInt(arr[2])), arr[1], arr[3], arr[4]);
contactlist.add(contact);
}
return contactlist;
}}
我不想重新启动活动,因为它看起来像是在打开一个新活动。 这是我的活动:
public class Notes extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
TextView tv = (TextView) findViewById(R.id.tvHead);
final DatabaseNote dbn = new DatabaseNote();
final FragmentManager fmTop = getSupportFragmentManager();
final FragmentTransaction ftTop = fmTop.beginTransaction();
final Fragment fragm = new NotesBottomFragment();
ftTop.replace(R.id.layout_content_notes, fragm,
SubjectBottomFragment.TAG);
ftTop.commit();
final SQLiteDatabase notes = openOrCreateDatabase("ENAPLO", MODE_PRIVATE, null);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fab.setVisibility(View.INVISIBLE);
dbn.refreshDatabase(notes, Notes.this, findViewById(android.R.id.content));
//Here want I do the same refresh
fab.setVisibility(View.VISIBLE);
}
});
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openOptionsMenu();
}
});
}}
答案 0 :(得分:0)
在onCreateView()之外声明适配器,以便它对onCreateView()和onViewCreated()方法都可见。
ListviewNotesAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
adapter = new ListviewNotesAdapter(getActivity(), listContact);
lv.setAdapter(adapter);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final SwipeRefreshLayout swr = (SwipeRefreshLayout)getActivity().findViewById(R.id.swiperefresh);
swr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
adapter.notifyDataSetChanged();
}});
}