D
D
David King2020-12-08 23:08:21
Java
David King, 2020-12-08 23:08:21

What to do if AS asks to change the type of a variable from an integer to an array?

I am writing code for a fragment, creating a messageCount variable and assigning a value to it (I do this through FirebaseDatabase, because I took the value from there, but an error was thrown and I decided to try just assigning 1), and suddenly an error occurs that you need to turn an integer into array. 5fcfdcecc904c383060458.pngI tried to move the variable outside the onCreateView() event, but the application just crashed there.

Here's the whole code:
PS - don't pay attention to the unused bits of code in the form of the Message class and something else.

package com.neura.neuramessanger;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GeneralChat#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GeneralChat extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public GeneralChat() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GeneralChat.
     */
    // TODO: Rename and change types and number of parameters
    public static GeneralChat newInstance(String param1, String param2) {
        GeneralChat fragment = new GeneralChat();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            // TODO: Rename and change types of parameters
            String mParam1 = getArguments().getString(ARG_PARAM1);
            String mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_general_chat, container, false);
        Button send = view.findViewById(R.id.send_message_to_general_chat);
        EditText inputArea = view.findViewById(R.id.message_input_area);
        ScrollView chatView = view.findViewById(R.id.chatView);
        send.setOnClickListener(v -> {
            final int messageCount;
            FirebaseDatabase.getInstance().getReference().addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    messageCount = 1;
                }

               @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    Toast.makeText(getActivity(), "Error:" + error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
//            Toast.makeText(getActivity(), messageCount, Toast.LENGTH_SHORT).show();
            TextView message = new TextView(getActivity());
            message.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            String messageText = inputArea.getText().toString().trim();
            message.setText(messageCount);
            chatView.addView(message);
            inputArea.setText(null);
        });
        return view;
    }

    public static class Message {
        public final Integer messageCount;

        public Message(Integer messageCount) { this.messageCount = messageCount; }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-12-08
@javaScriptIsPooP

This is because in Java lambdas can capture read-only variables, you cannot change local variables from a lambda. Therefore, the studio offers you a "hack". Turn the variable into an array with one element, and modify that element. This is a well-known trick.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question