将列表从 Fragment 传递到 Class

Pass a list from a Fragment to a Class

我有一个向用户显示列表的片段,所以我在 class 扩展片段中有 2 个 classes:一个用于 Holder,另一个用于 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
   } 
}

我想做的是创建一个具有相同列表的小部件。 所以,我检查了这个 github,它在小部件中显示了一个 ListView:https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

现在我想将片段中的列表传递给 class (LoremViewFactory)。 我无法从 class 访问片段中的列表! 从 class LoremViewFactory 我可以访问 Holder 和适配器 class 但不能访问我创建的那些。 到目前为止我试过了:

最终的想法是让应用程序中呈现的列表与 Widget 中的片段完全相同。

所以在 Holder 内部,我尝试将列表放入包中,以意图发送它并创建一个 getList 函数: `public class NoteAdapter 扩展 RecyclerView.Adapter { public 列出 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;
    }

这是我要访问的 class 中的代码:

 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

我找到了解决方案: 使用数据库 SQLite。 创建并实现它后,您需要从小部件的服务(在 RemoteViewFactory 内)调用它,记录是一个字符串数组列表:

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

然后您可以在方法 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);