


1. activity_main.xml 코드
<androidx.appcompat.widget.LinearLayoutCompat
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:layout_margin="3dp"
tools:context=".MainActivity">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/cafe_terras" />
<ImageView
android:id="@+id/iv2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/arles_starry_night" />
<ImageView
android:id="@+id/iv3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/mona_lisa" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/sunflower" />
<ImageView
android:id="@+id/iv5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/the_kiss" />
<ImageView
android:id="@+id/iv6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/the_scream" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/sunrise" />
<ImageView
android:id="@+id/iv8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/the_starry_night" />
<ImageView
android:id="@+id/iv9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:scaleType="fitXY"
android:layout_margin="3dp"
android:src="@drawable/van_goghs_bedroom" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnResult"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:background="@android:color/holo_blue_light"
android:text="투표 종료" />
</androidx.appcompat.widget.LinearLayoutCompat>
2. activity_result.xml 코드
<TableLayout
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:stretchColumns="0"
android:gravity="center_vertical"
android:layout_margin="2dp"
tools:context=".ResultActivity">
<TableRow>
<TextView
android:id="@+id/tv1"
android:text="그림 1"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar1"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv2"
android:text="그림 2"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar2"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv3"
android:text="그림 3"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar3"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv4"
android:text="그림 4"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar4"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv5"
android:text="그림 5"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar5"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv6"
android:text="그림 6"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar6"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv7"
android:text="그림 7"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar7"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv8"
android:text="그림 8"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar8"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv9"
android:text="그림 9"
android:textSize="20dp" />
<RatingBar
android:id="@+id/ratingBar9"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right" />
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="돌아가기"/>
</TableRow>
</TableLayout>
3. MainActivity.java 코드
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("명화 선호도 투표");
ImageView image[] = new ImageView[9];
Integer imageId[] = {R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4,
R.id.iv5, R.id.iv6, R.id.iv7, R.id.iv8, R.id.iv9};
String imageName[] = {"포룸 광장의 카페 테라스", "아를의 별이 빛나는 밤",
"모나리자", "해바라기", "키스", "절규", "해돋이",
"별이 빛나는 밤", "반 고흐의 침실"};
final int voteCount[] = new int[9];
for(int i = 0; i < 9; i++){
voteCount[i] = 0;
}
for(int i = 0; i < imageId.length; i++){
final int index;
index = i;
image[index] = (ImageView) findViewById(imageId[index]);
image[index].setOnClickListener(v -> {
voteCount[index]++;
Toast.makeText(getApplicationContext(), imageName[index] +
" : 총 " + voteCount[index] + " 표", Toast.LENGTH_SHORT).show();
});
}
Button btnResult = (Button)findViewById(R.id.btnResult);
btnResult.setOnClickListener(v -> {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
intent.putExtra("VoteCount", voteCount);
intent.putExtra("ImageName", imageName);
startActivity(intent);
});
}
}
4. ResultActivity.java 코드
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
setTitle("투표결과");
Intent intent = getIntent();
int[] voteResult = intent.getIntArrayExtra("VoteCount");
String[] imageName = intent.getStringArrayExtra("ImageName");
TextView tv[] = new TextView[imageName.length];
RatingBar ratingBar[] = new RatingBar[imageName.length];
Integer tvID[] = {R.id.tv1, R.id.tv2, R.id.tv3,
R.id.tv4, R.id.tv5, R.id.tv6,
R.id.tv7, R.id.tv8, R.id.tv9 };
Integer ratingBarID[] = {R.id.ratingBar1, R.id.ratingBar2, R.id.ratingBar3,
R.id.ratingBar4, R.id.ratingBar5, R.id.ratingBar6,
R.id.ratingBar7, R.id.ratingBar8, R.id.ratingBar9};
for(int i = 0; i < voteResult.length; i++){
tv[i] = (TextView) findViewById(tvID[i]);
ratingBar[i] = (RatingBar) findViewById(ratingBarID[i]);
}
for(int i = 0; i < voteResult.length; i++){
tv[i].setText(imageName[i]);
ratingBar[i].setRating((float) voteResult[i]);
}
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(v -> {
finish();
});
}
}