Click here to Skip to main content
15,312,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in android programming, my project is about getting RSS Feeds from web ,it works fine, then I have moved the app to be in Navigation drawer menu.
Then the application become not working I'm sure there is no error on code but in how to call the fragment that do the work "I think that !". anyway this my code>>

my Main Activity:
Java
public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView=null;
Toolbar toolbar=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RssFragment fragment=new RssFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container,fragment);
    fragmentTransaction.commit();
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

my RssFragment:
Java
public class RssFragment extends Fragment implements AdapterView.OnItemClickListener {

private ProgressBar progressBar;
private ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_rss, container, false);
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    listView = (ListView) view.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            startService();
            pullToRefresh.setRefreshing(false);
        }
    });
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    startService();
}

private void startService() {
    Intent intent = new Intent(getActivity(), RssService.class);
    getActivity().startService(intent);
}

/**
 * Once the {@link RssService} finishes its task, the result is sent to this BroadcastReceiver
 */
private BroadcastReceiver resultReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        progressBar.setVisibility(View.GONE);
        List<RssItem> items = (List<RssItem>) intent.getSerializableExtra(RssService.ITEMS);
        if (items != null) {
            RssAdapter adapter = new RssAdapter(getActivity(), items);
            listView.setAdapter(adapter);
        } else {
            Toast.makeText(getActivity(), "An error occurred while downloading the rss feed.",
                    Toast.LENGTH_LONG).show();
        }
    }
};

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RssAdapter adapter = (RssAdapter) parent.getAdapter();
    RssItem item = (RssItem) adapter.getItem(position);
    Uri uri = Uri.parse(item.getLink());
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}
@Override
public void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter(RssService.ACTION_RSS_PARSED);
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(resultReceiver, intentFilter);
}

@Override
public void onStop() {
    super.onStop();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(resultReceiver);
}

my app_bar_main :
Java
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/fragment_container"
    android:layout_height="match_parent" />

this is was the main activity in the app that call RssFragment before moved to navigition drawer main activity :
Java
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        addRssFragment();
    }
}
private void addRssFragment() {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    RssFragment fragment = new RssFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit();
}
}

I really want help with that!!

Note: The application was working well but when I moved it to navigation drawer it did not work and the activity does not show anything,just swipe to refresh widget.

What I have tried:

change frame layout to be :
Java
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/fragment_container"
    android:layout_height="match_parent" />

and try to call RSSfragmet using :
Java
FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        RssFragment fragment = new RssFragment();

        transaction.add(R.id.fragment_container, fragment);

        transaction.commit();
Posted
Comments
David Crow 30-Aug-18 10:58am
   
Is your RSS feed being displayed in the navigation drawer?
Member 13366692 31-Aug-18 3:26am
   
no

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900