2016-08-29 72 views
1

如何刷新此片段中的ListView? 我有一個活性和一個片段,我想用SwipeRefreshLayout和FloatingActionButton刷新ListView。 這裏是我的片段:片段中的Android刷新列表視圖

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

只要調用notifyDataSetChanged()如果有任何新的或刪除的項目,或者只是創建一個新的適配器,並再次將其設置爲列表視圖。 –

+0

這將在onCreateView中工作,但我想在onViewCreated中進行刷新。 –

+0

它也可以在onViewCreated中使用。 –

回答

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(); 
    }}); 
    }