0

this is my first post here, so sorry if I am not precise enough.

I am developing an Android app and in one activity i have many Textviews and a Button. I get a arrayList from my firebase database and whenever I click the Button I want to set the backgroundcolor of the first Textview to red and Change the text of the Textview with the String in the arrayList. When I click the Button after that I want the same to happen with the second element in my arrayList and the second textview.

Here's my Code:

public class ContainerShipMenuActivity extends AppCompatActivity {

private static final String TAG = null;
FirebaseDatabase database;
DatabaseReference ref;
DatabaseReference database2;

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

    Intent intent = getIntent();
    String message = intent.getStringExtra("EXTRA_MESSAGE");

    // Capture the layout's TextView and set the string as its text
    TextView textView3 = findViewById(R.id.textView3);
    textView3.setText(message);

    database = FirebaseDatabase.getInstance();
    ref = database.getReference("orders/1/containerLarge");

    Log.d(TAG, "Value is: " + ref);

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> contID= new ArrayList<>();
            int count = 0;


            for (DataSnapshot snp : dataSnapshot.getChildren()) {
                contID.add(String.valueOf(snp.getKey()));
                Log.d(TAG, "Value is: " + snp);
            }

            for (String s : contID){

                Button buttonNext = findViewById(R.id.nextcon);
                buttonNext.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        count++;
                        String cellVal = "cell_" + count;
                        int viewVal = getResources().getIdentifier(cellVal, "id", getPackageName());
                        TextView cell = findViewById(viewVal);
                        cell.setText(s);
                        cell.setBackgroundColor(Color.rgb(255,0,0));

                        TextView con = findViewById(R.id.toLoad);
                        con.setText("Next Container: " + s);
                    }
                });
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "Failed to read value.", databaseError.toException());
        }
    });

That's it so far. I hope you can help me. I am also new to Java, so the solution could be really easy.

enter image description here

Best regards domidoof

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
domidoof
  • 1
  • 2

0 Answers0