Pass a list from a Fragment to a Class

你离开我真会死。 提交于 2019-12-13 07:07:33

问题


I have a fragment which present a list to the user, so I have inside the class extends fragment, 2 classes : one for the Holder and other for the Adapter.

public class ListFragment extends Fragment {
List<>mylist;
         //some stuff
   public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{
      //some Stuff
      }
//Another class
public class NoteAdapter extends RecyclerView.Adapter<NoteHolder>{
//I put my list inside the adapter
   } 
}

What I am trying to do is to create a Widget with the same list. So, I checked this github which present a ListView in a Widget: https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

Now I would like to pass the list that I have in my fragment to a class (LoremViewFactory). I can't access the list within the fragment from the class ! From the class LoremViewFactory I can acces to the Holder and adapter class but not the ones I create. So far I tried :

  • Create a new class inside fragment with the method to access the list (I still can't acces the class)
  • Create a public variable inside the class fragment so I can acces from another class : no, didn't work.
  • Create a "getlist" method inside the existing classes : can't access the methods.

The final idea is to have the list presented in the app with the fragment exactly the same inside the Widget.

So inside the HolderI tried to put the list inside the bundle, to send it with an intent and to create a getList function : `public class NoteAdapter extends RecyclerView.Adapter { public List mNotes;

    public NoteAdapter(List<Note> notes) {
        mNotes = notes;
    }

    @Override
    public NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater.inflate(R.layout.list_item_note, parent, false);

        Intent in = new Intent(getContext(), WidgetProvider.class);//Send data to
        in.putExtra("List", (Serializable) mNotes);

        Bundle list = new Bundle();
        list.putSerializable("List", (Serializable) mNotes);

        return new NoteHolder(view);
    }

    @Override
    public void onBindViewHolder(NoteHolder holder, int position) {
        Note note = mNotes.get(position);
        holder.bindNote(note);
    }
    public int getItemCount() {
       return mNotes.size();
    }
    public List<Note>  getList() {
        return mNotes;
    }

Here is the code inside the class from which I want to access :

 public class LoremViewsFactory implements RemoteViewsService.RemoteViewsFactory {
   //This array was created by the author,  want to create my own from the data
     private static final String[] items={"lorem", "ipsum", "dolor",
                                    "sit", "amet", "consectetuer",
                                    "adipiscing", "elit", "morbi",
                                    "vel", "ligula", "vitae",
                                    "arcu", "aliquet", "mollis",
                                    "etiam", "vel", "erat",
                                    "placerat", "ante",
                                    "porttitor", "sodales",
                                    "pellentesque", "augue",
                                    "purus"};
private Context ctxt=null;
private int appWidgetId;

 List<Note> mylist;
 //What I am doing, create a new "fragment class"
  NoteListFragment test = new NoteListFragment();

  NoteListFragment.NoteAdapter inner = new NoteListFragment().new 
 NoteAdapter(mylist);
 //Here I am trying to access the list with :
 inner.getList(); //THIS DOES NOT WORK, I can't acces methods

回答1:


I found the solution: Use the databse SQLite. Once you've created and implemented it you need to call it from the service of the widget (inside RemoteViewFactory) records is an Arraylist of string:

NoteBaseHelper db = new NoteBaseHelper(this.mContext);
    NoteBaseHelper mDbHelper = new NoteBaseHelper(this.mContext);

    NoteHolder nh = new NoteHolder(this.mContext);

    myListTitle=nh.getNotes();
    records=new ArrayList<String>();

    for (Note e: myListTitle){
        records.add(e.getTitle().toString());
    }

Then you can set up your views in the method getViewAt :

public RemoteViews getViewAt(int position) {

    // position will always range from 0 to getCount() - 1.
    // Construct a RemoteViews item based on the app widget item XML file, 
    and set the
    // text based on the position.
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), 
R.layout.widget_item);
    // feed row
    String data=records.get(position);
    String title = myListTitle.get(position).getTitle();
    String details = myListTitle.get(position).getDetails();
    //Put data in the widget list
    rv.setTextViewText(R.id.title, data);
    rv.setTextViewText(R.id.details, details);


来源:https://stackoverflow.com/questions/43197609/pass-a-list-from-a-fragment-to-a-class

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