将片段中的列表传递给班级

时间:2017-04-04 03:12:20

标签: android list listview android-fragments android-fragmentactivity

我有一个向用户提供列表的片段,所以我在类里面扩展了片段,2个类:一个用于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
   } 
}

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

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

  • 使用访问列表的方法在片段内创建一个新类(我仍然无法访问该类)
  • 在类片段中创建一个公共变量,以便我可以从另一个类访问:不,不起作用。
  • 在现有类中创建“getlist”方法:无法访问方法。

最后的想法是让应用程序中显示的列表与Widget中的片段完全相同。

所以在HolderI内部试图将列表放在bundle中,用intent发送它并创建一个getList函数: `public class NoteAdapter扩展了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;
    }

以下是我想要访问的类中的代码:

 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

0 个答案:

没有答案