Creating multiple button counters in ListView in TabActivity

137 Views Asked by At

I'm trying to make an app to keep score during a game of golf. To do this I have 18 tabs in a tab activity (one for each hole) and then in each tab I have a ListView with a list of players, their score for the hole, and a plus and minus button.

I need to make it so that hitting the plus button in a row increases that players score for that hole but I'm having trouble figuring out where and how to do an OnClickListener and how to handle the indexing for the player and the hole.

I'm very new to Android Studio so any help is greatly appreciated!

HoleTabsActivity.java

package com.txstate.zms22.urban;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;



public class HoleTabsActivity extends FragmentActivity {

    int NUMBER_OF_HOLES = 18;

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

        String[] names = getResources().getStringArray(R.array.players);
        int[] scores = new int[names.length];

        ListView tabbedScorecard_ListView = findViewById(R.id.tabbedScorecard_ListView);
        ItemAdapter adapter = new ItemAdapter(getApplicationContext(), names, scores);
        tabbedScorecard_ListView.setAdapter(adapter);

        Button minus_Button = findViewById(R.id.minus_Button);

        minus_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scores[i]++;
            }
        });


    }


    @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_hole_tabs, 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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        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 view = inflater.inflate(R.layout.fragment_hole_tabs, container, false);
            TextView textView = view.findViewById(R.id.sectionLabel_TextView);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

            Button minus_Button = view.findViewById(R.id.minus_Button);
            TextView holeScore_TextView = view.findViewById(R.id.holeScore_TextView);


            return view;


        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 1 page per number of holes
            return NUMBER_OF_HOLES;
        }
    }
}

activity_hole_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.txstate.zms22.urban.HoleTabsActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"

            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

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

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

fragment_hole_tabs.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.txstate.zms22.urban.HoleTabs$PlaceholderFragment">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Header" />

        <ListView
            android:id="@+id/tabbedScorecard_ListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <View
            android:id="@+id/myRectangleView"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/hole_number_background" />
    </LinearLayout>

    <TextView
        android:id="@+id/sectionLabel_TextView"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:text="Hole ##"
        android:textColor="@color/holeUnderline"
        android:textSize="24dp"
        app:layout_constraintBottom_toTopOf="@+id/holeUnderline_View"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1" />

    <View
        android:id="@+id/holeUnderline_View"
        android:layout_width="112dp"
        android:layout_height="8dp"
        android:background="@drawable/hole_number_underline"
        app:layout_constraintEnd_toStartOf="@+id/linearLayout"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/linearLayout"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteY="481dp" />


</android.support.constraint.ConstraintLayout>

ItemAdapter.java

package com.txstate.zms22.urban;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ItemAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    String[] players;
    int[] scores;

    public ItemAdapter(Context c, String[] p, int[] s) {
        players = p;
        scores = s;
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return players.length;
    }

    @Override
    public Object getItem(int i) {
        return players[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        View v = mInflater.inflate(R.layout.player_list, null);

        TextView name_TextView = v.findViewById(R.id.name_TextView);
        String name = players[i];
        name_TextView.setText(name);

        TextView holeScore_TextView = v.findViewById(R.id.holeScore_TextView);
        int score = scores[i];
        holeScore_TextView.setText(score +"");


        Button minus_Button = v.findViewById(R.id.minus_Button);
        Button plus_Button = v.findViewById(R.id.plus_Button);





        return v;
    }
}

player_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">


    <TextView
        android:id="@+id/name_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="Player Name"
        android:textSize="@dimen/text_size"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/holeScore_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="3"
        android:textSize="@dimen/text_size"
        app:layout_constraintBottom_toBottomOf="@+id/minus_Button"
        app:layout_constraintEnd_toStartOf="@+id/plus_Button"
        app:layout_constraintStart_toEndOf="@+id/minus_Button"
        app:layout_constraintTop_toTopOf="@+id/minus_Button"
        app:layout_constraintVertical_bias="0.45" />

    <Button
        android:id="@+id/minus_Button"
        android:layout_width="@dimen/box_size"
        android:layout_height="@dimen/box_size"
        android:layout_marginStart="225dp"
        android:text="-"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@+id/name_TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/name_TextView" />

    <Button
        android:id="@+id/plus_Button"
        android:layout_width="@dimen/box_size"
        android:layout_height="@dimen/box_size"
        android:layout_marginEnd="16dp"
        android:text="+"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@+id/holeScore_TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/holeScore_TextView"
        app:layout_constraintVertical_bias="0.55" />

</android.support.constraint.ConstraintLayout>

Player List Layout The layout of each player's information in the ListView

If there's any more information I should include in my post let me know! Thanks again.

0

There are 0 best solutions below