Android refresh listview in fragment

血红的双手。 提交于 2020-01-05 04:52:14

问题


How can i refresh the ListView in this Fragment? I have an acivity and a fragment, and i want to refresh the ListView with a SwipeRefreshLayout and with a FloatingActionButton. Here is my Fragment:

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

I don't want to restart activity, because it looks like opening a new activity. Here is my activity:

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


    }}

回答1:


Declare the adapter outside of the onCreateView(), so that it will visible to both the methods onCreateView() and 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();
    }});
  }


来源:https://stackoverflow.com/questions/39211456/android-refresh-listview-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!