-1

I have a ListView which is populated using an ArrayAdapter. I have an OnItemClickListener attached to this ListView which works fine. Now when an item is clicked, I want to switch the view of the screen, by displaying a new fragment. How do I go about doing this? Here is some of my code from the custom ArrayAdapter I have:

protected class PostAdapter extends ArrayAdapter<Post> implements OnScrollListener, OnItemClickListener {
        private int previousTotal = 0;
        private boolean loading = true;

        public PostAdapter(List<Post> posts){
            super(getActivity(), R.layout.post_item, posts);
        }   

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getActivity().getLayoutInflater().inflate(R.layout.post_item, null);
            }

            Post thePost = getItem(position);

            TextView postTitleTextView = (TextView) convertView.findViewById(R.id.post_title);
            postTitleTextView.setText(thePost.getTitle());

            TextView postDetailsTextView = (TextView) convertView.findViewById(R.id.post_details);
            postDetailsTextView.setText(thePost.getDetails());

            TextView postScoreTextView = (TextView) convertView.findViewById(R.id.post_score);
            postScoreTextView.setText(thePost.getScore());

            return convertView;
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {            
            if (loading) {
                //update previous total, adding in the footer 
                if (totalItemCount > previousTotal + 1) {
                    loading = false;
                    previousTotal = totalItemCount; 
                    Log.d("running", "item count: " + totalItemCount);
                }
            }           
            if (!loading && 
                    (firstVisibleItem + visibleItemCount >= totalItemCount)
                    ) {
                new AddPostsTask().execute();
                loading = true;
            }           
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        /**
         * An AsyncTask which takes care of grabbing more posts and updating our 
         * adapter, making sure it is on the UI Thread.
         * @author Sukh
         *
         */
        private class AddPostsTask extends AsyncTask<Void, Void, Void> {    
            @Override
            protected void onPreExecute() {
                postsList.addFooterView(footer);
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                Log.i("running", "start fetchMorePosts");               
                posts.addAll(postsHolder.fetchMorePosts());
                Log.i("running", "end fetchMorePosts");
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Log.i("running", "start adapterChange");
                postsList.removeFooterView(footer);
                notifyDataSetChanged();
                Log.i("running", "end adapterChange");
                super.onPostExecute(result);
            }

        }

        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,
                long arg3) {
            Post selectedPost = (Post) adapter.getItemAtPosition(position);
            String title = selectedPost.getTitle();
            Log.d("running", "Clicked on:" + title);
        }

    }
sanghas26
  • 161
  • 1
  • 2
  • 10

1 Answers1

0

Inside the OnItemClick of your listview, you can create a new fragment.

Fragment creation and handling is explained very well here

Go through the article for better understanding.

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

Above code picked directly from the developers site.

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • What is the difference of using .replace? – sanghas26 Feb 20 '14 at 06:11
  • `replace` will replaces the older fragment with the new fragment without adding it into the stack. Whereas `add` will always managed to add the fragments into stack and manage it accordingly whether you add fragment to backstack or not. – GrIsHu Feb 20 '14 at 06:18
  • http://stackoverflow.com/questions/18634207/difference-between-add-replace-and-addtobackstack difference is well putted here – Gaurav Gupta Feb 20 '14 at 06:18