ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Android] 다른문서로 배열 넘기기 / 영화 투표
    BACK-END/Android 2020. 11. 10. 11:57

     

     

     

    | 새 프로젝트 만들기

     

     

    | drowable 폴더에 사진 넣기

     

    | activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/iv1"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv2"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv3"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="horizontal">
            <ImageView   
                android:id="@+id/iv4"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv5"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv6"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/iv7"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv8"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv9"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/m9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="10pt"
            android:text="레이팅바 결과 확인"/>
    </LinearLayout>
    

     

     

    | activity_two.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:stretchColumns="0">
        <TableRow>
            <TextView
                android:id="@+id/tv1"
                android:layout_gravity="center_vertical"
                android:text="그림1"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb1"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv2"
                android:layout_gravity="center_vertical"
                android:text="그림2"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb2"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv3"
                android:layout_gravity="center_vertical"
                android:text="그림3"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb3"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv4"
                android:layout_gravity="center_vertical"
                android:text="그림4"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb4"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv5"
                android:layout_gravity="center_vertical"
                android:text="그림5"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb5"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv6"
                android:layout_gravity="center_vertical"
                android:text="그림6"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb6"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv7"
                android:layout_gravity="center_vertical"
                android:text="그림7"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb7"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv8"
                android:layout_gravity="center_vertical"
                android:text="그림8"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb8"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv9"
                android:layout_gravity="center_vertical"
                android:text="그림9"
                android:textSize="12pt"/>
            <RatingBar
                android:id="@+id/rb9"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_gravity="right"/>
        </TableRow>
        <TableRow>
            <Button
                android:id="@+id/btnReturn"
                android:layout_span="2"
                android:textSize="12pt"
                android:text="메인화면"/>
        </TableRow>
    </TableLayout>

     

     

    | MainActivity.java

    package com.example.and1110;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
        int count[]=new int[9]; //누적수
        ImageView iv[]=new ImageView[9];
        int 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 imgMsg[]={
            "마블", "걸캅스", "피카츄", "뽀로로", "몬스터랜드",
            "교회오빠","로지","물의기억", "배심원들"
        };
    
    
        Button btn; //투표결과
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //count배열초기화
            for(int i=0; i<count.length;i++){count[i]=0;}
    
            for(int i=0; i<imageID.length; i++){
                final int index=i;
                iv[i]=findViewById(imageID[i]);
                iv[i].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        //count[i]++;  for반복문안에 있는 i 쓰면 에러발생
                        count[index]++;
                        Toast tt=Toast.makeText(getApplication(), imgMsg[index]+"  총:"+count[index]+"표" , Toast.LENGTH_SHORT);
                        tt.show();
                    }
                });
            }//그림관련 for end
    
            btn=findViewById(R.id.btn1);
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent it=new Intent(getApplicationContext(), Two.class);
                    it.putExtra("count", count);
                    it.putExtra("imgMsg", imgMsg);
                    startActivity(it);
                }
            });
        }//onCreate end
    
    }//MainActivity class END
    
    
    
    
    

     

     

    | Two.java

    package com.example.and1110;
    
    import android.content.Intent;
    import android.media.Rating;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RatingBar;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class Two extends AppCompatActivity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_two);
    
            Intent it = this.getIntent();
            int[ ] count= it.getIntArrayExtra("count");
            String[] imgMsg = it.getStringArrayExtra("imgMsg");
    
            TextView tv[]=new TextView[imgMsg.length];
            RatingBar rb[]=new RatingBar[imgMsg.length];
    
            int 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 };
            int rbID[ ]={R.id.rb1, R.id.rb2, R.id.rb3,R.id.rb4,R.id.rb5,R.id.rb6,R.id.rb7,R.id.rb8,R.id.rb9 };
    
            for(int i=0; i<9; i++){
              tv[i]=findViewById(tvID[i]);
              rb[i]=findViewById(rbID[i]);
    
              tv[i].setText(imgMsg[i]);
              rb[i].setRating((float)count[i]);
            }
    
            //버튼제어하기
            Button btnReturn = findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    finish();
                }
            });
        }//end
    }//Two class END
    

     

     

    | 이거 꼭 추가

     

     

     

    | 출력결과

     

    | 결과 확인

     

    댓글

Today
Designed by Danbee Park.