How to pass data from fragment to adapter class? - android

i am having value of companyName In Myfragment class I need to pass this companyNamevalue to MyAdapter Class. And without moving from Myfragment to MyAdapter activity. Can someone please tell me what to do? I am getting confused with every match I got on other websites.
This is My Adapter Class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("test",""+deliveryListBeans.size());
LayoutInflater inflater=(LayoutInflater)ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.list_view_for_delivery_list,null);
Button delivered=(Button)v.findViewById(R.id.do_delivered);
delivered.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent deliveryUpdate=new Intent(ct, MyOwn.class);
deliveryUpdate.putExtra("invoiceId",dlb.getInvoiceNo());
ct.startActivity(deliveryUpdate);
}
});
return v;
}
In setOnClickListener I need companyName value from fragment class so that i can use it in URL.
This is My Fragment Class
public class DeliveryList extends Fragment {
private List<DeliveryListBean> dlblistbean= new ArrayList<DeliveryListBean>();
ListView listview;
Button delivered;
String companyName;}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_delivery_list,container,false);
listview=(ListView) rootView.findViewById(R.id.deliveryListView);
companyName=this.getArguments().getString("companyName");
listview.setAdapter(deliveryListAdapter);
return rootView;
}

In your adapter class
public class MyAdapter {
private Context mContext;
private String mCompanyName;
public MyAdapter(Context context, String companyName) {
mContext = context;
mCompanyName = companyName;
}
}

Related

Share data between fragments

I am making a bugtracker app on android where i have a class called Task, which has a field "category" which defines if the task is done, in progress or have to do.
I have a fragment of ideas "Projects" where I added to set the ViewModel
public class ProjectFragment extends Fragment {
private static final String TAG = "ProjectFragment";
private UserViewModel model;
private GridViewAdapter gridViewAdapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_projects, container, false);
model = ViewModelProviders.of(requireActivity()).get(UserViewModel.class);
GridView gridView = view.findViewById(R.id.gridview_project);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Project temp = Objects.requireNonNull(model.getCurrentuser().getValue()).getProjects().get(position);
model.setCurrproject(temp);
Log.i(TAG, "onItemClick: " + temp.getProjectName());
model.setTodoTasks(TaskSorter.getSortedTaskList(temp, "TODO"));
model.setInprogressTasks(TaskSorter.getSortedTaskList(temp, "IN PROGRESS"));
model.setDoneTasks(TaskSorter.getSortedTaskList(temp, "DONE"));
}
});
gridViewAdapter = new GridViewAdapter(requireActivity(), Objects.requireNonNull(model.getCurrentuser().getValue()).getProjects());
gridView.setAdapter(gridViewAdapter);
model.getCurrentuser().observe(this, new Observer<User>() {
#Override
public void onChanged(User user) {
gridViewAdapter.notifyDataSetChanged();
}
});
FloatingActionButton floatingActionButton = view.findViewById(R.id.floatingActionButton);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openProjectDialog();
}
});
return view;
}
I also have 3 fragment for the corresponding categories where I observe the changes.
public class FragmentToDo extends Fragment {
private static final String TAG = "FragmentToDo";
private ListView listView;
private UserViewModel model;
private ListViewAdapter listViewAdaptertodo;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo, container, false);
listView = view.findViewById(R.id.todo_list);
model = ViewModelProviders.of(this).get(UserViewModel.class);
model.gettodoTasks().observe(this, new Observer<ArrayList<Task>>() {
#Override
public void onChanged(ArrayList<Task> tasks) {
listViewAdaptertodo.notifyDataSetChanged();
Log.i(TAG, "onChanged: todo");
}
});
initTodo();
return view;
}
private void initTodo(){
listViewAdaptertodo = new ListViewAdapter(requireActivity(), model.gettodoTasks().getValue());
listView.setAdapter(listViewAdaptertodo);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
openTask();
}
});
}
All the 3 fragments looks like this, with different names of course. Whenever I click on a project some change happen, but not in all of them. The 3 categories are hold by a ViewPager, but im not sure where the problem is. In case heres the code for that too:
public class TaskFragment extends Fragment {
private static SectionsPagerAdapter sectionsPagerAdapter;
private ArrayList<Task> todo;
private ArrayList<Task> inp;
private ArrayList<Task> done;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_task, container, false);
sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
sectionsPagerAdapter.addFragments(new FragmentToDo(), "To Do");
sectionsPagerAdapter.addFragments(new FragmentInProgress(), "In Progress");
sectionsPagerAdapter.addFragments(new FragmentDone(), "Done");
ViewPager viewPager = view.findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = view.findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
return view;
}
}
Maybe the problem is whenever I define context and lifecycle owners, because as I am a novice android developer, i still know few thing about these topics.
There is simple and not extensible way - eventbus, this, for example.
And preferred way- move your app logic from fragments to other components, with data passing only in 1 way. I.e there is component, that exposes some sort of model of your screen, that get's updated - and fragment/view/activity reflect that change. You can check sample app here

How to launch a fragment from a fragment while passing a custom object

Building on a previous question and using fragment based approach, the question now is How to launch a fragment from a fragment while passing a custom data object
public class MainActivity extends AppCompatActivity {
public User user = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, user)
.commit();
}
}
User.java list of users
public class User extends Fragment {
ListView userList;
private ArrayList<UserVO> users;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
userList = view.findViewById(R.id.userList);
users = getUsers(); // ArrayList
userList.setAdapter(new UserAdapter());
userList.setOnItemClickListener((adapterView, view1, i, l) -> {
Log.d("UserList", "onItemClick: " + i);
// how to launch Form.java Fragment while passing users[i]
});
return view;
}
class UserAdapter extends BaseAdapter {
#Override
public int getCount() {
return users.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup,false);
((TextView) view.findViewById(android.R.id.text1)).setText(users.get(i).givenName());
return view;
}
}
}
Form.java which is basically to display user data in a form for update purposes.
public class Form extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.form, container, false);
return view;
}
}
Q1. Within User.java I've a placeholder in the comments, asking for how to instantiate Form.java while passing users[i] (a custom object with fields)
Q2. Please see if there's anything wrong with the approach in MainActivity.java, I've seen the other approach with add method and I'm using replace here, not sure which one is right here.
Attempt
I've added following code and not happy from the result, it's overlapping with the list, I want form to have it's own view and place on the stack. Back button should take the user back to the list.
Form form = new Form();
getFragmentManager().beginTransaction()
.add(R.id.frame_layout, form)
.commit();
Edit 2 For review - based on the answer.
public interface IForm { // 1
void update(UserObject userObject);
}
public class User extends Fragments implements IForm { // 2
private Form form = new Form();
void update(UserObject userObject) {
// update user list with updated object.
}
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
userList = view.findViewById(R.id.userList);
users = getUsers(); // ArrayList
form.delegate = this; // 3
userList.setOnItemClickListener((adapterView, view1, i, l) -> {
Log.d("UserList_archive", "onItemClick: " + i);
form.userData = users[i]; // 4
getFragmentManager().beginTransaction()
.replace(R.id.frame_layout, form)
.addToBackStack(null)
.commit();
});
return view;
}
public class Form {
public IForm delegate; // 5
public UserObject userObject; // see 4 - to display data in the form
}
Here you have to use one CallBack/Interface to plug and play with two fragments.
First, write one interface with a method which will do the job, like below:
public interface ItemClickListener extends Parcelable{
void onItemClick(FormData data);
}
Then initialize this interface in your both Fragments in OnAttach() :
public class UserFragment extends Fragment {
private ItemClickListener onItemClick;
#Override
public void onAttach(Context context) {
super.onAttach(context);
onItemClick= (ItemClickListener) context;
}
Use it inside your onItemClick to pass the value:
userList.setOnItemClickListener((adapterView, view1, i, l) -> {
Log.d("UserList", "onItemClick: " + i);
// how to launch Form.java Fragment while passing users[i]
onItemClick.onItemClick(adapterView.getItemAtPosition(i);
});
}
Then implement this interface in your Activity.
public class MainActivity extends AppCompatActivity implements ItemClickListener{
#override
public void onItemClick(FormData data){
//do your stuff here with form data or whatever your object
//Start Form Fragment and send the data through Bundle
//https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android
}
}

Passing data from recylerview adapter to fragments in navigation drawer

I want to pass data from recycleview Adapter to a fragment and then open the fragment. I tried things mentioned but its not working. Should I implement fragment to fragment or activity to fragment? Please provide a solution. How do I do it?
public class QuotationAdapter extends RecyclerView.Adapter<QuotationAdapter.ViewHolder>implements Filterable {
private Context context;
String id;
private List<ListQuotation> contactList;
private List<ListQuotation> contactListFiltered;
private QuotationAdapter.ContactsAdapterListener listener;
Fragment fragment = new Fragment();
#Override
public void onBindViewHolder(final QuotationAdapter.ViewHolder holder, int position) {
final ListQuotation contact = contactListFiltered.get(position);
holder.item_list_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context = v.getContext();
id = contact.getId();
passData(id);
}
});
}
private void passData(String id) {
Intent intent = new Intent(context,QuotationItemDetails.class);
intent.putExtra("id", ""+id);
context.startActivity(intent);
}
This is my fragment
public class ItemListFragment extends Fragment {
String id;
TableLayout tv;
private static final String url = "";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
final View rootView = inflater.inflate(R.layout.fragment_item_list, container, false);
tv = (TableLayout)rootView.findViewById(R.id.tableexp);
loadItemData();
//get id
id = getArguments().getString("id");
return rootView;
}

Making GridView with Fragment Instances

I want to implement GridView that each cell contains an object of class. for this I implemented fragment class:
public class DayUC extends Fragment {
/*
some variables
*/
// use to create instance of class with initial parameters
public static DayUC newInstance(//initial parameters) {
DayUC dayUC = new DayUC();
/*
set parameters
*/
return dayUC;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_layout, container, false);
initialization() //initial variables and view elemans (textView, ...)
return rootView;
}
}
and also I created GridViewAdapter:
public class GridViewAdapter extends BaseAdapter {
private ArrayList<DayUC> gridList; //list of grid DayUC objects
Context context;
public GridViewAdapter(Context context, ArrayList<DayUC> gridList) {
this.gridList = gridList;
this.context = context;
}
#Override
public int getCount() {
return gridList.size();
}
#Override
public DayUC getItem(int position) {
return gridList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return gridList.get(position).getView();
}
}
but when I create GridView it throws NullPointerException! I found that in gridView elements (DayUC) onCreateView() never called. so gridList.get(position).getView() returns null!!
could anyone help me to solve this problem? how could I force onCreateView to be called??
Edit: MainActivity onCreate method added:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DayUC[] dayUC = new DayUC[42];
monthGridView = (GridView) findViewById(R.id.month_view);
for (int i = 0; i < 42; i++) {
dayUC[i] = DayUC.newInstance(//inpute arguments)
dayUCList.add(dayUC[i]);
monthCal.add(Calendar.DATE, 1);
}
gridViewAdapter = new MonthGridViewAdapter(getActivity(), dayUCList);
monthGridView.setAdapter(gridViewAdapter);
gridViewAdapter.notifyDataSetChanged();
}

Android: FragmentStatePageAdapter not updating ListView

I have android application.
Application minSDK:15
I have NavigationDrawer.
When click on first Navigation drawer menu item, my new fragment appears called MainFragment.
Everything is good, like in android tutorial.
Now in this fragment I need to implement simple ViewPager.
ViewPager will page Fragments.
Here is code:
public class MainFragent extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment_layout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<Books> books;
BooksPageAdapter contentPageAdapter = new BooksPageAdapter(getFragmentManager(), books);
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.viewPager);
pager.setAdapter(contentPageAdapter);
}
private class BooksPageAdapter extends FragmentStatePagerAdapter {
private ArrayList<Books> books;
public BooksPageAdapter(FragmentManager fm, private ArrayList<Books> books) {
super(fm);
this.books = books;
}
#Override
public Fragment getItem(int pos) {
return BooksFragment.newInstance(books);
}
#Override
public int getCount() {
return 5;
}
} }
And BooksFragment:
public class BooksFragment extends Fragment {
private ArrayList<Books> books;
static ContentFragment newInstance(final ArrayList<Books> books) {
BooksFragment booksFragment = new BooksFragment();
Bundle args = new Bundle();
args.putSerializable("books", books);
contentFragment.setArguments(args);
return booksFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
books = (ArrayList<Books>) getArguments().getSerializable(“books”);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_books, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
updateUI(books);
}
public void updateUI(ArrayList<Books> books) {
ListView listView = (ListView)getActivity().findViewById(R.id.list_view);
listView.setAdapter(new MyArrayAdapter(getActivity(), books));
}
private class MyArrayAdapter extends ArrayAdapter<Books> {
public MyArrayAdapter(final Context context, final List<Books> list) {
super(context, R.layout.books_menu_item, R.id.title, list);
}
#Override
public View getView(final int position,
final View convertView,
final ViewGroup parent) {
final View row = super.getView(position, convertView, parent);
Book book = getItem(position);
textView.setText(book.getTitle());
return row;
}
}
}
The FirstPage looks awesome, like it should. I see list of books titles.
But on the second page, list view is empty. All other views on second page (like TextView..) are fine except ListView.
What I do wrong ?
P.S FragmentStatePageAdapter is from support13. Fragments are from android package, not from support4

Categories

Resources