IT/Android

android - 투표 최대결과 인텐트 전환하며 값 넘겨주기 (Activity, Intent, putExtra, getIntent, getIntArrayExtra, getStringArrayExtra)

노마드오브 2018. 9. 23. 21:39

파일명 : activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">

<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic1" />
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic2" />
<ImageView
android:id="@+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">

<ImageView
android:id="@+id/iv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic4" />
<ImageView
android:id="@+id/iv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic5" />
<ImageView
android:id="@+id/iv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">

<ImageView
android:id="@+id/iv7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic7" />
<ImageView
android:id="@+id/iv8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic8" />
<ImageView
android:id="@+id/iv9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic9" />
</LinearLayout>
<Button
android:id="@+id/btnResult"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="투표종료" />
</LinearLayout>



파일명 : activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity"
android:gravity="center_vertical"
android:stretchColumns="0">

<TableRow>
<TextView
android:id="@+id/txtView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_span="2" />
</TableRow>
<TableRow>
<ImageView
android:id="@+id/imgView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:layout_span="2" />
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv1"
android:layout_gravity="center_vertical"
android:text="그림1"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar1"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv2"
android:layout_gravity="center_vertical"
android:text="그림2"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar2"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv3"
android:layout_gravity="center_vertical"
android:text="그림3"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar3"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv4"
android:layout_gravity="center_vertical"
android:text="그림4"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar4"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv5"
android:layout_gravity="center_vertical"
android:text="그림5"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar5"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv6"
android:layout_gravity="center_vertical"
android:text="그림6"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar6"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv7"
android:layout_gravity="center_vertical"
android:text="그림7"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar7"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv8"
android:layout_gravity="center_vertical"
android:text="그림8"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar8"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<TextView
android:id="@+id/tv9"
android:layout_gravity="center_vertical"
android:text="그림9"
android:textSize="15dp" />
<RatingBar
android:id="@+id/rbar9"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</RatingBar>
</TableRow>

<TableRow>
<Button
android:id="@+id/btnReturn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="돌아가기" />
</TableRow>

</TableLayout>



파일명 : MainActivity.java

package com.example.it.myapplication8_4;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("명화 선호도 투표");

final int voteCount[] = new int[9];
for (int i=0; i<9; i++) {
voteCount[i] = 0;
}
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
};

final String[] imgName = {
"독서하는 소녀", "꽃장식 모자 소녀", "부채를 든 소녀", "이레느깡 단 베르양",
"잠자는 소녀", "테라스의 두 자매", "피아노 레슨", "피아노 앞의 소녀들", "해변에서"
};

for (int i=0; i<imageId.length; i++) {
final int index;
index = i;
image[index] = (ImageView) findViewById(imageId[index]);
image[index].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
voteCount[index]++;
Toast.makeText(getApplicationContext(), imgName[index] + ": 총 " + voteCount[index] + " 표", Toast.LENGTH_SHORT).show();
}
});
}

Button btnFinish = (Button) findViewById(R.id.btnResult);
btnFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
intent.putExtra("VoteCount", voteCount);
intent.putExtra("ImageName", imgName);
startActivity(intent);
}
});

}
}


파일명 : ResultActivity.java

package com.example.it.myapplication8_4;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class ResultActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
setTitle("투표결과");


final String[] imgName = {
"독서하는 소녀", "꽃장식 모자 소녀", "부채를 든 소녀", "이레느깡 단 베르양",
"잠자는 소녀", "테라스의 두 자매", "피아노 레슨", "피아노 앞의 소녀들", "해변에서"
};
int[] image = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8, R.drawable.pic9 };


Intent intent = getIntent();
int[] voteResult = intent.getIntArrayExtra("VoteCount");
String[] imageName = intent.getStringArrayExtra("ImageName");

TextView[] tv = new TextView[imageName.length];
RatingBar[] rbar = new RatingBar[imageName.length];

TextView txtView1 = (TextView) findViewById(R.id.txtView1);
ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);

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[] rbarID = {
R.id.rbar1, R.id.rbar2, R.id.rbar3, R.id.rbar4, R.id.rbar5, R.id.rbar6, R.id.rbar7, R.id.rbar8, R.id.rbar9
};

for (int i = 0; i < voteResult.length; i++) {
tv[i] = (TextView) findViewById(tvID[i]);
rbar[i] = (RatingBar) findViewById(rbarID[i]);
}

float maxNum = 0;
int maxIndex = 0;
for (int i = 0; i < voteResult.length; i++ ) {
tv[i].setText(imageName[i]);
rbar[i].setRating((float) voteResult[i]);

if (maxNum < (float) voteResult[i]) {
maxNum = (float) voteResult[i];
maxIndex = i;
}
}

txtView1.setText(imgName[maxIndex]);
imgView1.setImageResource(image[maxIndex]) ;

Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}

);
}
}


파일명 : AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.it.myapplication8_4">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity"></activity>
</application>

</manifest>


'IT > Android' 카테고리의 다른 글

android - 양방향 intent, 사칙연산  (0) 2018.09.24
android - 양방향 intent  (0) 2018.09.24
android - ratingBar  (0) 2018.09.23
android - Activity, Intent 응용 (액티비티 화면 세개 전환)  (0) 2018.09.22
android - Activity, Intent  (0) 2018.09.22