in adapter
holder.nameexam2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("dataList.get(position).getId()",dataList.get(position).getId());
Videos_botttomnav video= new Videos_botttomnav();
video.setArguments(bundle);
}
}
in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String str =getArguments().getString("dataList.get(position).getId()");
return view;
}
It invokes null pointer exception in getString(). how to resolve it.enter code here
I resolved the problem by adding childclicklistener in Recyclerview instead of sending data from adapter to fragment.
Related
I trying to create BottomSheetDialogFragment with rounded corners which contain fragment container.
I did the correct drawable and apply to xml of this fragment.
code:
public class ContainerFragment extends BottomSheetDialogFragment {
#Override
public void setupDialog(Dialog dialog, int style) {
View view = View.inflate(getContext(), R.layout.fragment_delivery_container, null);
dialog.setContentView(view);
// тут делаю так, чтобы фон под удалёнными краями из-за скругления был прозрачным
((View) view.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
/*getChildFragmentManager()
.beginTransaction()
.add(R.id.container, ProvidersFragment.createInstance(), "TASGGGG")
.commit();*/
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog bDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
...
return bDialog;
}
/* #Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment_delivery_container, null);
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, ProvidersFragment.createInstance(), "TASGGGG")
.commit();
return view;
}*/
....
}
In the current implementation of the code, everything is fine (corners are rounded), only the fragment transaction is commented out. If i'll uncomment, it will throw an error that the "fragment does not have a View". Accordingly, if i'll uncomment onCreateView, then edges of this View appear and I can not do anything with this
I want to open BottomSheetDialog from fragment. Before it, I tried opening BottomSheetDialog at activity and it worked. I use the same code and replace the getSupportFragmentManager with getChildFragmentManager. I also tried replacing the getSupportFragmentManager with getActivity().getSupportFragmentManager . Is there a solution?
public class MainFragment extends Fragment implements BottomSheetDialog.BottomSheetListener {
private TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_beranda, container, false);
Button buttonOpenBottomSheet = findViewById(R.id.button_open_bottom_sheet);
buttonOpenBottomSheet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheet = new BottomSheetDialog();
bottomSheet.show(getChildFragmentManager(),"BottomSheet");
}
});
}
}
Try BottomSheetDialogFragment
ModalBottomSheetDialogFragment.Builder()
.add(R.menu.menu_options)
.show(childFragmentManager, "my_bottom_sheet")
How do I call MainActivityFragment from AppCompatActivity in Android? (I am new to Android.)
Code
main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create new fragment and transaction
}
}
Fragment
public class MainActivityFragment extends Fragment {
ArrayAdapter mforecastadapter;
ListView list_view;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_main, container, false);
String Items[] = {"Sandeep", "Santhosh", "Sangeetha", "Basil", "bibhas", "sumanth", "praveen"};
List<String> weekforecast=new ArrayList<>(Arrays.asList(Items));
mforecastadapter=new ArrayAdapter<String>(getActivity(),R.layout.fragment_main,R.id.list_item_forecast_textview,Items);
list_view = (ListView) rootview.findViewById(R.id.listView);
list_view.setAdapter(mforecastadapter);
return rootview;
}
}
Don't really know what you mean by "call", but if you want to instantiate a fragment in an Activity, write this in your Activity:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
YourFragment frag = new YourFragment ();
ft.replace(R.id.container_for_fragment, frag);
ft.commit();
Notice R.id.container_for_fragment is your xml attribute for containing the fragment instance. it can be <fragment>, <FrameLayout>.
This is the most basic way to do that. You can find some more examples on SO, how to use fragments BackStack, etc.
You can also use add() instead of replace() in FragmentTransaction, depends on your need.
I hope it helps you.
I have a ParseQueryAdapter that is populating a listview inside of a fragment:
QueryAdapter:
public class CustomAdapter extends ParseQueryAdapter implements AdapterView.OnItemClickListener {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Invoice");
query.whereEqualTo("CustomerID", ParseUser.getCurrentUser());
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View cellview, ViewGroup parent) {
if (cellview == null) {
cellview = View.inflate(getContext(), R.layout.cell_layout, null);
}
TextView cellDate = (TextView) cellview.findViewById(R.id.dateTextView);
TextView invoice = (TextView) cellview.findViewById(R.id.invoiceNoTextView);
cellDate.setText(object.getString("Date"));
invoice.setText(object.getString("InvoiceNumber"));
return cellview;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
Fragment Containing Listview:
public class InvoiceHistory extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_invoice_history, container, false);
ListView invoicesListView = (ListView) layout.findViewById(R.id.invoiceList);
ParseQueryAdapter adapter = new CustomAdapter(getActivity());
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment detailView = new InvoicesFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeholder, detailView);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
invoicesListView.setAdapter(adapter);
adapter.loadObjects();
return layout;
}
}
This populates the ListView with the proper fields from the query, but when I click on one of the cells the fragment transaction takes place, but i can't figure out how to set textfields on that new fragment with ParseObject Keys from the Query.
This is the fragment that gets Presented that I want to set the Textfields in:
public class InvoicesFragment extends Fragment {
public InvoicesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
return layout;
}
Android is super new to me so maybe this is a wrong way to approach it. But all i want to do here is have that detail fragment show up with populated TextViews. Im not really sure what I'm doing.
You could approach this by setting up a constructor in your DetailsFragment that you can pass in the textview info you want to show. In the example below, the data and invoice number are passed into the constructor. When the fragment is set up, this fragment is set up with the Parse object's data:
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ParseObject object = adapter.getItem(i);
Fragment detailView = DetailsFragment.newInstance(object.getString("Date"),
object.getString("InvoiceNumber"));
// ...
}
});
Here's a portion of the DetailsFragment showing the new constructor as well as how you could use them in setting up your view:
// InvoicesFragment.java
public static class InvoicesFragment extends Fragment {
// ...
public static InvoicesFragment newInstance(String invoiceDate, String invoiceNumber) {
InvoicesFragment f = new InvoicesFragment();
Bundle args = new Bundle();
args.putString("invoiceDate", InvoiceDate);
args.putString("invoiceNumber", InvoiceNumber);
f.setArguments(args);
return f;
}
public String getInvoiceDate() {
return getArguments().getString("invoiceDate", "");
}
public String getInvoiceNumber() {
return getArguments().getString("invoiceNumber", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
// Assign some TextView, ex: dateTextView
dateTextView.setText(getInvoiceDate());
return layout;
}
}
I want use in Fragment a AsyncTask and use in AsyncTask findViewById.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category,
container, false);
View myFragmentView = inflater.inflate(R.layout.category, container, false);
cont=getActivity();
new ProgressTask(cont, myFragmentView).execute();
}
private class ProgressTask extends AsyncTask <Void,Void,Void>{
private Context mContext;
private View rootView;
public void RecuperarComentarisFoto(Context context, View rootView){
this.mContext=context;
this.rootView=rootView;
}
LinearLayout barLayout = (LinearLayout) rootView.findViewById(R.id.barLayout);
[...]
He say to me that cont=getActivity(); cannot be resolved to a variable.
What should I setfor cont?
Thanks