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); 
相關問題