티스토리 뷰

예제 2

사용자 리스트뷰에서 특정 아이템을 선택하면 상세 정보를 가진 화면으로 전환하기 


연락처를 클릭하면 클릭된 코난의 등장인물의 사진과 정보들을 출력해주는 창으로 넘어가는것을 구현해 보아라.



기존의 것은 예제 1과 동일하다. 

추가 해줄것은 다음창으로 넘어갈때의 레이아웃과 액티비티 이다. 




소스코드(두번째 창 layout)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
 
        android:id="@+id/ivPhoto"/>
    <TextView
        android:background="#c568da"
        android:id="@+id/charactername"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:textStyle="bold"/>
    <TextView
        android:background="#00fffb"
        android:id="@+id/characternumber"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:textStyle="bold"/>
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Back"
        android:layout_gravity="center_horizontal"
        android:id="@+id/btnBack"/>
 
</LinearLayout>
cs




소스코드(두번째 창 Activity)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.sh415.project4_1;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
public class soloactivity extends AppCompatActivity {
 
    Button btnSecond;
    TextView Name,Num;
    ImageView Img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.solo_photo);
        btnSecond = (Button)findViewById(R.id.btnBack);
        Name = (TextView)findViewById(R.id.charactername) ;
        Num = (TextView)findViewById(R.id.characternumber);
        Img = (ImageView)findViewById(R.id.ivPhoto);
 
 
 
 
 
        Intent it = getIntent();
 
        Name.setText(it.getStringExtra("Name"));
        Num.setText(it.getStringExtra("PhoneNo"));
        Img.setImageResource(it.getIntExtra("Imgsrc",0));
 
/*
        Toast.makeText(this, it.getStringExtra("Imgsrc") +it.getIntExtra("id", 0), Toast.LENGTH_SHORT).show();
*/
 
        btnSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
 
    }
}
 
cs


소스코드(기존 Activity 변화)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.sh415.project4_1;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
 
import java.util.ArrayList;
 
public class ListViewAct extends AppCompatActivity implements AdapterView.OnItemClickListener {
    ListView lv; ArrayList<RowItem> data; MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        data = new ArrayList<RowItem>();
        RowItem conan = new RowItem(R.drawable.conan,"코난","010-1111-1111");
        RowItem rose = new RowItem(R.drawable.rose,"장미","010-2222-2222");
        RowItem ran = new RowItem(R.drawable.ran,"미란","010-3333-3333");
        RowItem kid = new RowItem(R.drawable.kid,"키드","010-4444-4444");
        RowItem han = new RowItem(R.drawable.mori,"명환","010-5555-5555");
        RowItem aeri = new RowItem(R.drawable.aeri,"애리","010-6666-6666");
        data.add(conan);
        data.add(rose);
        data.add(ran);
        data.add(kid);
        data.add(han);
        data.add(aeri);
        myAdapter = new MyAdapter(this,data);
        lv = (ListView)findViewById(R.id.lv);
        lv.setAdapter(myAdapter);
        lv.setOnItemClickListener(this);
    }
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 
            Intent it = new Intent(ListViewAct.this, soloactivity.class);
            it.putExtra("Name", data.get(position).getName());
            it.putExtra("PhoneNo", data.get(position).getPhoneNo());
            it.putExtra("Imgsrc", data.get(position).getImgId());
            startActivity(it);
 
/*
        Toast.makeText(this,data.get(position).getName(),Toast.LENGTH_SHORT).show();*/
    }
}
 
cs

중요! 38번 라인을 잘 살펴보면 Intent를 이용하여 두번째 액티비티에 정보를 넘겨주는 작업이다.
putExtra함수를 이용하여 지정된 스트링에다가 정보를 담는것이다.


소스코드(두번째 창 Activity)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.sh415.project4_1;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
public class soloactivity extends AppCompatActivity {
 
    Button btnSecond;
    TextView Name,Num;
    ImageView Img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.solo_photo);
        btnSecond = (Button)findViewById(R.id.btnBack);
        Name = (TextView)findViewById(R.id.charactername) ;
        Num = (TextView)findViewById(R.id.characternumber);
        Img = (ImageView)findViewById(R.id.ivPhoto);
 
 
 
 
 
        Intent it = getIntent();
 
        Name.setText(it.getStringExtra("Name"));
        Num.setText(it.getStringExtra("PhoneNo"));
        Img.setImageResource(it.getIntExtra("Imgsrc",0));
 
/*
        Toast.makeText(this, it.getStringExtra("Imgsrc") +it.getIntExtra("id", 0), Toast.LENGTH_SHORT).show();
*/
 
        btnSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
 
    }
}
 
cs
getIntExtra 함수는 지정된 스트링을 활용하여 얻고자 하는 정보를 갖고오게 해주는 함수이다.
이때 , String은 별도로 디폴드값이 필요 없지만 int는 디폴드 값이 필요하다.
그래서 끝에 0을 넣어 주었다. 



출력화면


초기 화면모습이다.



명환을 클릭해 보았다.


]


Back 버튼을 눌렀다.



마지막으로 코난까지 아주 잘나오는 모습이다.





댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31