简体   繁体   中英

How i can add decrease button to my ListView to decrease numbers?

I want to add a button to decrease the numbers in the list view, and I don't want the number to go down from zero.

NightActivity:

public class NightActivity extends AppCompatActivity {

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

        final ArrayList<Word> words = new ArrayList<>();
        words.add(new Word("Thing", "another Thing", 1)); // <--- This number I want to decrease it.
        words.add(new Word("ThingOne","another ThingOne", 3)); // <--- This number I want to decrease it.
        words.add(new Word("ThingTwo", "another ThingTwo", 3)); // <--- This number I want to decrease it.
        words.add(new Word("ThingThree", "another ThingThree", 3)); // <--- This number I want to decrease it.
        words.add(new Word("ThingFour", "another ThingFour", 1)); // <--- This number I want to decrease it.

        WordAdapter adapter = new WordAdapter(this, words);

        ListView listView = findViewById(R.id.list);
        listView.setAdapter(adapter);

    }
} 

Word:

public class Word {

    private final String name;
    private final String extraText;
    private final int repetition;

    public Word(String name, String extraText, int repetition) {
        this.name = name;
        this.extraText = extraText;
        this.repetition = repetition;
    }

    public String getName() {
        return name;
    }

    public String getExtraText() {
        return extraText;
    }

    public int getRepetition() {
        return repetition;
    }
}

WordAdapter:

public class WordAdapter extends ArrayAdapter<Word> {

    public WordAdapter(Activity context, ArrayList<Word> words) {
        super(context, 0, words);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        Word currentWord = getItem(position);

        TextView nameTextView = listItemView.findViewById(R.id.name_text);
        nameTextView.setText(currentWord.getname());

        TextView extraTextView = listItemView.findViewById(R.id.extra_text);
        extraTextView.setText(currentWord.getExtraText());

        TextView repetitions = listItemView.findViewById(R.id.number_of_repetitions);
        String formattedRepetition = formatRepetition(currentWord.getRepetition());
        repetitions.setText(formattedRepetition);

        return listItemView;
    }

    private String formatRepetition(int repetition) {
        DecimalFormat repetitionFormat = new DecimalFormat("0");
        return repetitionFormat.format(repetition);
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/corner"
        android:padding="10dp">

        <TextView
            android:id="@+id/name_text"
            style="@style/nameText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@id/extra_text"
            android:gravity="center">

            <TextView
                android:id="@+id/number_of_repetitions"
                style="@style/repetition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="2"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/button_repetitions"
                android:layout_width="90dp"
                android:layout_height="50dp"
                android:text="button"/>

        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="13dp">

        <TextView
            android:id="@+id/morning"
            style="@style/night"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/night_corner"
            android:text="@string/category_night" />

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="13dp" />

        <TextView
            android:id="@+id/evening"
            style="@style/day"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/day_corner"
            android:text="@string/category_day" />

    </LinearLayout>
</ScrollView>

I added a button, but I don't know how to activate it, and I don't know where I will put onClickListener , I am a beginner so please help me and thank you for reading my code.

First of all, to change the value of repetition of the word, you have to create a setter for repetition in the Word class as below:

public class Word {
    .....
    ..... // your code including constructor & getters
    .....
    
    public void setRepetition(int repetition) 
    {
        this.repetition = repetition;
    }
}

Now, to change the value of repetition on the button's click, apply onClickListener on the button in WordAdapter.java as below:

public class WordAdapter extends ArrayAdapter<Word> {
    .....
    ..... // your code
    .....

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        .....
        ..... // your code
        .....

        listItemView.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int current_repetition = currentWord.getRepetition();
                if (current_repetition > 0)
                    currentWord.setRepetition(current_repetition - 1)
                listItemView.textView.setText(currentWord.getRepetition())
            }
        });

        return listItemView;
    }
}

Try this and tell whether it works or not.

First of all you should use recyclerview instead of list view. To decrease your number you must add button to list_item, and set click listener

I didn't clearly understand what you want, but as far as I understand you want when you press the button it will subtract the number in the repetitions by one. Well we will add a button variable in the wordAdaptor activity, we know it, and then we will do the setOnclickListener event, then we start the rest of the process, this code after modification and I added a condition that if the number reaches zero its stop

public class WordAdapter extends ArrayAdapter {

public WordAdapter(Activity context, ArrayList<Word> words) {
    super(context, 0, words);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    Word currentWord = getItem(position);

    TextView nameTextView = listItemView.findViewById(R.id.name_text);
    nameTextView.setText(currentWord.getName());

    TextView extraTextView = listItemView.findViewById(R.id.extra_text);
    extraTextView.setText(currentWord.getExtraText());

    TextView repetitions = listItemView.findViewById(R.id.number_of_repetitions);
    String formattedRepetition = formatRepetition(currentWord.getRepetition());
    repetitions.setText(formattedRepetition);

    Button bRepetitions = listItemView.findViewById(R.id.button_repetitions);

    bRepetitions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int nRepetitions;
            String mRepetitions = repetitions.getText().toString();
            nRepetitions = Integer.parseInt(mRepetitions);
            nRepetitions -= 1;
            if (nRepetitions < 0) {
                nRepetitions = 0;
                Toast.makeText(getContext(), "Can not be less than 0", Toast.LENGTH_SHORT).show();}
            repetitions.setText(String.valueOf(nRepetitions));
        }
    });
    return listItemView;
}

private String formatRepetition(int repetition) {
    DecimalFormat repetitionFormat = new DecimalFormat("0");
    return repetitionFormat.format(repetition);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM