Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button onclick inside fragment is not working

I am trying to set / override the onclick method for a button (R.id.buy_button) inside one of my views (inside a fragment). The application doesn't return errors. However it does not trigger the onclick method. I'm not sure if my implementation is wrong or perhaps my button is incorrectly configured. Any help / comments / guidance would be appreciated.

My onCreateView is here:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.list_item, container, false);
    Button b = (Button) view.findViewById(R.id.buy_button);
    b.setOnClickListener(this);

    return inflater.inflate(R.layout.fragment_item_list, container, false);
}

My entire fragment code:

public static class AlbumsFragment extends ListFragment implements View.OnClickListener {
        private static final String ARG_SECTION_NUMBER = "section_number";

        public AlbumsFragment() {
        }

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

        @Override
        public void onActivityCreated(Bundle b){
            super.onActivityCreated(b);

            ListView listView = getListView();
            List<Album> albums = null;

            albums = Album.listAll(Album.class);
            Log.i("ALBUMS", albums.toString());
            ArrayAdapter<Album> adapter =
                    new ArrayAdapter<Album>(getActivity(), R.layout.list_item, R.id.textView,  albums);
            if (!albums.equals("")) {
                listView.setAdapter(adapter);
            }
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }

        @Override
        public void onDetach() {
            super.onDetach();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.list_item, container, false);
            Button b = (Button) view.findViewById(R.id.buy_button);
            b.setOnClickListener(this);

            return inflater.inflate(R.layout.fragment_item_list, container, false);
        }

        public void gotoPurchaseWeb() {
            Context context = getActivity().getApplicationContext();
            Intent intent = new Intent(context, PurchaseSong.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

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

            return fragment;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.buy_button:
                    Context context = getActivity().getApplicationContext();
                    Intent intent = new Intent(context, PurchaseSong.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    break;
            }
        }
    }

My list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:weightSum="1"
        android:showDividers="middle|end"
        android:visibility="visible">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/black"/>

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/textView"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:layout_marginTop="@dimen/zero_space"
            android:layout_marginBottom="@dimen/zero_space"
            android:paddingTop="3dp"
            android:paddingLeft="8dp"
            android:paddingRight="5dp"
            android:paddingBottom="0dp"
            android:layout_weight="24.33" />

    </LinearLayout>

    <Button
        style="@style/ButtonText"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/button_height"
        android:text="@string/buy_album"
        android:id="@+id/buy_button"
        android:background="@drawable/blue_button"
        android:paddingTop="7dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp" />

My entire project: https://github.com/markbratanov/MyFitPlayer/tree/master/Player/src/main/java/com/markbratanov/myfitplayer

Any help would be appreciated. Thanks.

like image 993
markbratanov Avatar asked May 06 '14 09:05

markbratanov


1 Answers

You should return your View instead of returning new inflated instance of your View in onCreateView callback function inside of your Fragment.

return inflater.inflate(R.layout.fragment_item_list, container, false);

instead use :

return view;
like image 174
Arash GM Avatar answered Nov 19 '22 22:11

Arash GM