0

My APP is composed by 4 fragments, 1 activity (mainpage). Each fragment show somethings; in this case I want to show to the user some informations about telecommunications, but i really need to use a RecycleView.

If I try to put a TextView, it works fine, but with a ListView or a RecycleView it doesn't show nothing.

homepage.java

import ...

public class homepage extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new 
SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new 
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new 
TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_homepage, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;

        switch(getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                rootView = inflater.inflate(R.layout.fragment_homepage, 
container, false);
                break;
            case 2:
                rootView = inflater.inflate(R.layout.fragment_fibra, 
container, false);
                break;
            case 3:
                rootView = inflater.inflate(R.layout.fragment_fibra, 
container, false);
                break;
            case 4:
                rootView = inflater.inflate(R.layout.fragment_aiuto, 
container, false);
                break;
        }
        return rootView;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }
}
}

fragment_fibra.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fibra"
android:background="#EFEFEF">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppBarOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ListaFibra"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:visibility="visible" />

</FrameLayout>

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> 
{
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_homepage, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}
theforestecologist
  • 4,667
  • 5
  • 54
  • 91
Elia
  • 7
  • 5
  • What do you mean by `yesterday` and `today`? I cannot see any other questions in your account. This is a little bit weird. Maybe it's a good idea to remove these lines from your question. Here is a good help site: https://stackoverflow.com/help/how-to-ask – colidyre Aug 30 '18 at 13:52
  • I can't see any code that sets the data/adapter for the recyclerview – Capricorn Aug 30 '18 at 13:57
  • colidyre I mean that yesterday I was using the ListView, with the same problem as today with the RecycleView. Capricorn i did it now thanks to Esteban, but I always have some problems – Elia Aug 31 '18 at 06:00

1 Answers1

0

You are never using the recyclerview.

You can do the following things in the onCreateView method of your Fragment:

  • Create a list of items to pass to your RecyclerView: ArrayList items = new ArrayList<>();
  • Don't forget to fill previous list with some items.
  • Get an instance of your RecyclerView: RecyclerView recyclerView = findViewById(R.id.ListaFibra);
  • Set LayoutManager: recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  • Get an instance of your adapter: MyAdapter adapter = new MyAdapter(this, items);
  • Set adapter on your RecylcerView: recyclerView.setAdapter(adapter);

That could be a starting point. Hope it helps :)

  • I had a problem with MyAdapter adapter = new MyAdapter(this ,items); (with this) – Elia Aug 30 '18 at 20:43
  • oh, it is asking for the context (as LinearLayoutManager does). As you are working inside a fragment instead of **this** use **getActivity()**. Anyway, any error you have, can You show the error that the IDE shows you? – Esteban Samaniego Aug 31 '18 at 03:21
  • Now I have this problem: E/RecyclerView: No adapter attached; skipping layout – Elia Aug 31 '18 at 06:33
  • As the IDE suggests: In MyAdapter constructor you are expecting a String[], but in the fragment you are sending an ArrayList. You'll have to change that. – Esteban Samaniego Aug 31 '18 at 15:26
  • E/RecyclerView: No adapter attached; skipping layout I read all the links about this problem (on google) but nothing... – Elia Sep 03 '18 at 18:09