-
-
Save udacityandroid/797532fc79eed21d801e to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity { | |
... | |
public void voteUdacicola(View view) { | |
int udacicolaVotes = 0; | |
udacicolaVotes = udacicolaVotes + 1; | |
} | |
public void votePepcity(View view) { | |
int pepcityVotes; | |
pepcityVotes = pepcityVotes + 1; | |
} | |
public void showMeVotes(View view) { | |
display(udacicolaVotes + " vs. " + pepcityVotes); | |
} | |
... | |
} |
if someone is still looking for a good explanation of local vs. global variables, I really liked this short, easy to understand explanation https://www.codecademy.com/en/forum_questions/514900b642e721e65d0003f1
Both of variables are local and both of them must be global for reachable from anypoint.
good job thank you
This Code have some error
1- int pepcityVotes; not set initial value of variable name
2- udacicolaVotes & pepcityVotes are local variable not Global variable so make error
The Correct Code Is
int udacicolaVotes = 0, pepcityVotes = 0;
public void voteUdacicola(View view) {
udacicolaVotes = udacicolaVotes + 1;
}
public void votePepcity(View view) {
pepcityVotes = pepcityVotes + 1;
}
public void showMeVotes(View view) {
display(udacicolaVotes + " vs. " + pepcityVotes);
}
globbal is in the begining
local is in lines
Errors:
- local variables out of scope.
- local variables cannot be used as counters.
- one of the local variables is not initialized.
thanks
both variables are local and they should be global for all methods to know about them
Udacicola and Pepcity should be both global to be used in showMeVotes() method. Cool example
thanks
soo... but how will become this code on
MainActivity.java and how will become the acitivityMain.xml with this ?
it gives me error
should be global Variable so other method can use it . As example here ShowMeVotes , otherwise it will give us error.
im need the xml code of voting ...
@mohammed74 you can use mine below but you need to change android:src to your own image
`
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/display_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WHICH DO YOU PREFER?"
android:textColor="#000"
android:shadowColor="#3e0909"
android:layout_marginBottom="30dp"
android:textSize="45sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/cocacola"
android:layout_alignParentLeft="true"
android:src="@drawable/cola"
android:layout_marginBottom="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cocacola"
android:gravity="center"
android:text="Vote"
android:layout_alignParentLeft="true"
android:background="#b71f1f"
android:onClick="voteUdacicola"
android:textColor="#fff"
android:layout_marginLeft="35dp"/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/pepsi"
android:id="@+id/pepsi"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pepsi"
android:gravity="center"
android:text="Vote"
android:layout_alignParentRight="true"
android:background="#285fbf"
android:textColor="#fff"
android:onClick="votePepcity"
android:layout_marginRight="30dp"/>
</RelativeLayout>
`
no
very nice .. thanx
thank you
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/display_text_view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="45sp" />
<TextView
android:id="@+id/display_text_view2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".55"
android:gravity="center"
android:text="vs"
android:textSize="45sp" />
<TextView
android:id="@+id/display_text_view3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="45sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="votePepcity"
android:text="pepcity" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="voteUdacicola"
android:text="udacicola" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
int pepcityVotes = 0;
int udacicolaVotes = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void votePepcity(View view) {
pepcityVotes = pepcityVotes + 1;
display(pepcityVotes);
return;
}
public void voteUdacicola(View view) {
udacicolaVotes = udacicolaVotes + 1;
display1(udacicolaVotes);
return;
}
public void display(int p) {
TextView t1 = (TextView) findViewById(R.id.display_text_view1);
t1.setText("" + p);
}
public void display1(int c) {
TextView t2 = (TextView) findViewById(R.id.display_text_view3);
t2.setText("" + c);
}
}
the parent layout is linearLayout vertical
can somebody please provide full code of xml and java for these examples............... i am getting very confused in this lesson 3
Both of variables are local and both of them must be global for reachable from anypoint.
having error
package com.example.ummurasul.practice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int pepsiVotes = 0;
int spriteVotes =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void voteUdacicola(View view) {
spriteVotes = spriteVotes + 1;
display1(spriteVotes);
return;
}
public void votePepcity(View view) {
pepsiVotes =pepsiVotes + 1;
display(pepsiVotes);
return;
}
}
**public void display(int p) {
TextView t1 = (TextView) findViewById(R.id.cocacola);
t1.setText("" + p);
}
public void display1(int c) {
TextView t2 = (TextView) findViewById(R.id.pepsi);
t2.setText("" + c);
}**
the error is on de above code
@Kukkulum
A global variable is initialized at the starting of the code it is global means other methods can be used this variable but local variable is initialized inside a method and there local members can used it not all the methods.
Thank you for your explanation. It is helpful to understand the difference.
best android course on this planet
very nice course
public class MainActivity extends AppCompatActivity {
...
int udacicolaVotes = 0;
int pepcityVotes = 0;
}