将列表从片段传递到类 [英] Pass a list from a Fragment to a Class

查看:98
本文介绍了将列表从片段传递到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,向用户显示一个列表,所以我在类中扩展了片段,分为2个类:一个用于Holder,另一个用于Adapter.

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

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

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

现在,我想将片段中的列表传递给类(LoremViewFactory). 我无法访问该类片段中的列表! 从LoremViewFactory类,我可以访问Holder和adapter类,但不能访问我创建的类. 到目前为止,我已经尝试过:

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 :

  • 在片段中使用访问列表的方法创建一个新类(我仍然无法访问该类)
  • 在类片段中创建一个公共变量,以便我可以从另一个类访问:不,没有用.
  • 在现有类中创建一个"getlist"方法:无法访问这些方法.

最终的想法是使列表显示在应用程序中,其片段与Widget内部完全相同.

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

因此,在Holder内部,我试图将列表放入包中,以有意发送它,并创建一个getList函数: `公共类NoteAdapter扩展了RecyclerView.Adapter { 公开列表mNotes;

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

推荐答案

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

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

然后,您可以在getViewAt方法中设置视图:

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

这篇关于将列表从片段传递到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆