I made my DataFakeGenerator to creat some fake datas to test my Recycelerview , but when i want to use it in one of my Fragments i have an error.
my error is in this line:
PostAdapter postAdapter = new PostAdapter(DataFakeGenerator.getData(this));
it says that i can not use "this",so what statement can i use in this parentheses?
here is my Fragment :
public class Frag_joke extends Fragment {
public static Fragment instance(){
Fragment fragment = new Frag_joke();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup)inflater.inflate(R.layout.frag_joke,null);
RecyclerView recyclerView = (RecyclerView)layout.findViewById(R.id.recycler_view);
PostAdapter postAdapter = new PostAdapter(DataFakeGenerator.getData(this));
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setAdapter(postAdapter);
return layout;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}}
here is my DataFakeGenerator :
public class DataFakeGenerator {
public static List<Post> getData(){
List<Post> posts= new ArrayList<>();
for(int i=0;i<=6;i++){
Post post= new Post();
post.setId(i);
post.setContent("sample text");
posts.add(post);
}
return posts;
}}
and here is my PostAdapter:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder>{
private Context context;
private List<Post> posts;
public PostAdapter(Context context, List<Post>posts){
this.context = context;
this.posts = posts;
}
#Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_post,parent,false);
return new PostViewHolder(view);
}
#Override
public void onBindViewHolder(PostViewHolder holder, int position) {
Post post=posts.get(position);
holder.content.setText(post.getContent());
}
#Override
public int getItemCount() {
return posts.size();
}
public class PostViewHolder extends RecyclerView.ViewHolder{
private TextView content;
public PostViewHolder(View itemView) {
super(itemView);
content=(TextView)itemView.findViewById(R.id.post_content);
}}}
You are probably confused with your implementation. DataFakeGenerator.getData() does not need any parameter. It's your adapter constructor that has 2.
To create your adapter you can use the following instead:
PostAdapter postAdapter = new PostAdapter(getContext(), DataFakeGenerator.getData());
PS: this wouldn't work with the adapter creation (if that's the code you are actually trying to use), as the Fragment is not a context (but the Activity is, which is why you can access it with getContext() from the fragment
You can set preview on any cardview if you've. Let's you have a cardview name item_emergency_contact then, you can use tools:listitem="#layout/your_card_view"
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/contacts_recyclerView_emergency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
tools:listitem="#layout/item_emergency_contact"
app:layout_constraintTop_toBottomOf="#+id/add_emergency_contacts" />
Related
I just got started with Firestore. So I tried fitting my data directly into a recyclerview, here's the code I used for reference.
Here's my UserRecyclerView Fragment :
public class UserRecyclerView extends Fragment{
private ArrayList<User> user;
RecyclerView mRecyclerView;
public UserRecyclerView(){}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_view,container,false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
FirestoreRecyclerAdapter adapter = getFirestoreRecyclerAdapter();
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
#NonNull
private FirestoreRecyclerAdapter getFirestoreRecyclerAdapter() {
Query query = FirebaseFirestore.getInstance()
.collection("users");
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class).build();
return new FirestoreRecyclerAdapter<User, UserViewHolder>(options) {
#Override
protected void onBindViewHolder(UserViewHolder holder, int position, User model) {
Log.d("USER", "DATA: "+model.getName());
holder.BindView(position, model);
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_list, parent, false);
return new UserViewHolder(view);
}
};
}
private class UserViewHolder extends RecyclerView.ViewHolder{
TextView info;
UserViewHolder(View itemView) {
super(itemView);
info = (TextView) itemView.findViewById(R.id.info_text);
}
void BindView(int position, User model){
info.setText(model.getName()+" , "+model.getId());
}
}
There's nothing in my MainActivity except for starting the above fragment. I don't know what's wrong with my code. It shows no errors, when I run the app it doesn't crash. It just enters into the recyclerview layout, I checked it by placing a textview field for being sure(now removed). I want you to help me out with the code. It's probably because of the adapter? Help me figure out. I have also uploaded a snippet of my database.
Thanks in advance!
Make sure you call startListening on the adapter to initiate the connection to Cloud Firestore, otherwise no data will be loaded.
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
See:
https://github.com/firebase/FirebaseUI-Android/blob/master/firestore/README.md#firestorerecycleradapter-lifecycle
Create recyclerview like below:
FirestoreRecyclerAdapter adapter = getFirestoreRecyclerAdapter();
//add below line
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
I am trying to implement viewpager with Tablayout. In this everything is fine working properly, but the one problem is when i come back from another screen view pager lost their position with Tablayout and when i scroll its crash the app.I find many solutions but all of these cannot work for this. Here is my code Adapter Class:-
public class Portfolio_Adapter extends FragmentStatePagerAdapter {
int tabCount;
public Portfolio_Adapter(FragmentManager fm,int tabCount) {
super(fm);
this.tabCount=tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Websites_Fragment websites_fragment=new Websites_Fragment();
return websites_fragment;
case 1:
Mobileapplication_fragment mobileapplication_fragment=new Mobileapplication_fragment();
return mobileapplication_fragment;
case 2:
Graphics_Fragment graphics_fragment=new Graphics_Fragment();
return graphics_fragment;
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
Fragment class:-
public class Portfolio_fragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.portfolio_fragment, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Portfolio");
tabLayout = (TabLayout) view.findViewById(R.id.portfolio_tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addTab(tabLayout.newTab().setText("Websites"));
tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) view.findViewById(R.id.portfolio_pager);
Portfolio_Adapter adapter = new Portfolio_Adapter(getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
View tab = slidingTabStrip.getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tab.getLayoutParams();
layoutParams.weight = 1;
tab.setLayoutParams(layoutParams);
}
return view;
}
}
Your code is right you only have to change the placement of code put your
tabLayout.setupWithViewPager(viewPager);
,
tabLayout.addTab(tabLayout.newTab().setText("Websites"));
tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
after setadapter in viewPager
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
I've been searching a lot and I can't make it work.
I have a problem trying to get fragments inside a viewpager with a tabstrip.
I've implemented a SparseArray as I read here and several methods that I found here but I can't make it work.
The thing is that everytime I call adapter.getRegisteredFragment(position).. I always receive null unless I made it inside the onPageSelected event of the tabsStrip, there it works.. but I don't want to get the fragments there.
Those are my classes:
My fragment:
public class WeekFragment extends Fragment implements View.OnClickListener
{
private static final String ARG_POSITION = "position";
private int position;
private LinearLayout[] btns;
public static WeekFragment newInstance(int position) {
WeekFragment f = new WeekFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
fillFragment();
}
private void fillFragment()
{
// Irrelevant stuff..
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
return inflater.inflate(R.layout.week_fragment, container, false);
}
#Override
public void onClick(View v)
{
// Irrelevant stuff
}
public LinearLayout[] getBtns()
{
return btns;
}
public void setBtns(LinearLayout[] btns)
{
this.btns = btns;
}
}
My adapter:
public class WeekAdapter extends FragmentPagerAdapter
{
Calendar cal;
Context context;
SparseArray<Fragment> registeredFragments;
private String[] TITLES = new String[6];
public WeekAdapter(FragmentManager fm, Context context)
{
super(fm);
registeredFragments = new SparseArray<>();
this.context = context;
fillTitles();
}
private void fillTitles()
{
// Fill titles
}
#Override
public CharSequence getPageTitle(int position)
{
return TITLES[position];
}
#Override
public int getCount()
{
return TITLES.length;
}
#Override
public Fragment getItem(int position)
{
return WeekFragment.newInstance(position);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
and my activity:
public class MyActivity extends FragmentActivity
{
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private WeekAdapter adapter;
private List<DayResumeItem> listDayResumesItems;
private User u;
private View mProgressView;
private View mRotaView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rota2);
mProgressView = findViewById(R.id.view_progress);
mRotaView = findViewById(R.id.view_rota);
this.u = this.getIntent().getExtras().getParcelable(getString(R.string.parcel_user));
// Initialize the ViewPager and set an adapter
pager = (ViewPager) findViewById(R.id.pager);
adapter = new WeekAdapter(getSupportFragmentManager(), getApplicationContext());
pager.setAdapter(adapter);
// Bind the tabs to the ViewPager
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
WeekFragment f = (WeekFragment)adapter.getRegisteredFragment(week);
// Do stuff.. Here, f is not null. Here I can work, but I don't want to.
}
#Override
public void onPageSelected(int position)
{
}
#Override
public void onPageScrollStateChanged(int state)
{
}
});
WeekFragment f = (WeekFragment)adapter.getRegisteredFragment(week);
// Do stuff.. Here, f is null and I can't work.
}
#Override
protected void onResume()
{
super.onResume();
getList();
}
}
It is everytime I call adapter.getRegisteredFragment(position) on my activity where it crash because it always return null..
I swear that I've been searching a lot but I'm unable to make it work.
Thank you very much everybody!
I think my problem was that I was calling this adapter.getRegisteredFragment(position) in the onCreate of my activity, and in the onCreate, the viewpager is still not fully loaded and the registeredFragments aren't still instantiated, so the list is still empty..
If you move this callings to another place when the viewpager is fully loaded, it will work.
The items in registeredFragments are initialized in instantiateItem() method, which should be called during the process of drawing views. And this drawing process happens after onCreate()/onResume().
I am not sure what stuff you want to do by getting Fragment in onCreate(), but generally it is not a good idea since the Fragment is not initialized at that moment. You should access the fragment in onPageSelected() as you said.
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 seek to receive data from AsyncTask inside a fragment. I'm aware to achieve this with activity but having difficulties in fragment.
GetResult class
public interface GetResult {
void getData(ArrayList<String> result);
}
myFrag class
public class myFrag extends Fragment implements GetResult{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.myFrag_layout, container, false);
Button myBtn = (Button) view.findViewById(R.id.Button1);
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
myAsync obj = new myAsync();
obj.setListener(getActivity());
}
});
}
}
myAsync class
public class myAsync extends AsyncTask<Void, Void, String>{
GetResult interfaceObj = null;
public void setListener( GetResult interfaceObj ) {
this.interfaceObj = interfaceObj;
}
}
I'm having problem with obj.setListener(getActivity). It say The method setListener(GetResult) in the type myAsync is not applicable for the arguments (FragmentActivity). Thanks in advance to everyone.
I didn't figured out wt the problem is but there is another way to do it. I'll just share it here.
1st the interface class GetResult.class is same
2nd Changes in the fragment class myFrag.class
public class myFrag extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.myFrag_layout, container, false);
Button myBtn = (Button) view.findViewById(R.id.Button1);
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
getLoc obj = new getLoc(getActivity(), new GetResultInterface() {
#Override
public void processFinish(ArrayList<String> result) {
// U can get the result here
}
obj.execute(url);
});
}
}
3rd In AsyncTask myAsync.class
public class myAsync extends AsyncTask<Void, Void, String>{
Context context;
GetResult interfaceObj = null;
public myAsync(Context context, GetResultInterface interfaceObj) {
this.context = context;
this.interfaceObj = interfaceObj;
}
#Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
interfaceObj.processFinish(result);
}
}