Android

Intent 주고받기 정리..

빠꾸스 2012. 3. 1. 00:34
요즘 Android Project를 진행하고 있는데 진행하면서 공부한 내용을 정리 중입니다.

첫번째로 Intent 주고 받기.
ListView에서 어떤 항목을 클릭했을때 ListView의 Data를 전달해서 새로운 Activity를 실행하는 내용이였다.
이때 처리방법은 Activity에서 OnItemClickListener 를 이용하는 방법과 Adapter에서 OnClickListener를 이용하는 방법이 있다.


ReceiveActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import android.os.Bundle;
...
...
...
 
public class ReceiveActivity extends Activity
{
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView( R.layout.receiveActivity);
 
          //String이 아닌경우엔 default 변수가 필요하다.
          int n1 =  0;
          String ID = getIntent().getStringExtras("id");
          int num = getIntent().getIntExtras("num",n1);
     }
}
 
Activity에서 OnItemClickListener를 이용해 Intent 전달하는 방법

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
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
...
...
...
public class ActivityClick extends Activity implements OnItemClickListener
{
      
    private ListView LvData;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        ....
        ....
        ....
        LvData.setOnItemClickListener(this);
    }
 
    public void onItemClick(AdapterView<?> parent, View v, int position,
 
long id)
    {
        // 선택한 Item postion을 받아온다.
        PersonData pd = (PersonData)parent.getItemAtPosition(position);
          
        //Receive Activity로 넘길 Bundle 을만든다.
        Bundle extras = new Bundle();
        extras.putString("id", pd.getId());
        extras.putInt("num", pd.getNumber());
          
        // Intent를 생성하여 ReceiveActivity class를 context로 넘겨준다.
        Intent intent = new Intent(this,ReceiveActivity.class);
          
        // 위에서 만든 bundle을 intent에 담는다.
        intent.putExtras(extras);
          
        // Activity 시작.
        startActivity(intent);
      }
}

Adapter에서 OnClickListener를 이용해 Intent 전달하는 방법

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
49
50
51
...
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
...
...
   
public class AdapterListViewCustomAdapter extends ArrayAdapter<Data>
                                              implements OnClickListener
{
  
    /**
     * 해당 position의 뷰를 만들어 반환한다.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
                   
        // 선택한 Item position을 tag로 저장한다.
        convertView.setTag(position);
           
        convertView.setOnClickListener(this);
           
        return convertView;
    }
   
    public void onClick(View v)
    {
        // 위에서 저장한 tag(position)을 꺼내온다.
        int position = (Integer) v.getTag();
           
        PersonData pd = getItem(position);
           
        //Receive Activity로 넘길 Bundle 을만든다.
        Bundle extras = new Bundle();
        extras.putString("id", pd.getId());
        extras.putInt("num", pd.getNumber());
           
  
        // Intent를 생성하여 ReceiveActivity class를 context로 넘겨준다.
        Intent intent = new Intent(mContext, ReceiveActivity.class);
           
        // 위에서 만든 bundle을 intent에 담는다.
        intent.putExtras(extras);
           
        // Activity 시작.
        mContext.startActivity(intent);
    }
   
}
 
이전 Activity에서 값을 가져올때 key (id, num)값을 이용하는데 보낼때와 받을때 key는 동일해야한다.