0

我有一个片段向用户提供一个列表,所以我在类内部扩展了片段,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。 所以,我检查了这个提供了一个Widget列表视图的github:https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

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

  • 创建内部片段的新类来访问列表的方法(我仍然无法存取权限类)
  • 创建类片段中一个公共变量,这样我可以ACCES来自另一班:不,没有工作。
  • 在现有类中创建一个“getlist”方法:无法访问方法。

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

所以HolderI内试图把列表中的束里面,有意向把它和创造的GetList功能: `公共类NoteAdapter扩展RecyclerView.Adapter { 公开名单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

如何试图从班级访问列表?你可以发布你的代码吗? –

+0

@ Saeed-rz我刚刚编辑了更多代码的问题。如果你理解了这个问题并且有一个更好的主意。 –

+0

我需要得到片段中呈现的完全相同的列表,所以我可以将它发送到Lorem类,以便使小部件呈现相同的列表。 –

回答

0

我找到了解决办法: 使用SQLite的DATABSE。 一旦你创建并实现它,你需要从控件的服务调用它(里面RemoteViewFactory)记录为字符串的ArrayList:

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); 
相关问题