android 에서 list 를 사용하기 위해서는 다음과 같은 과정을 거친다.
1. 먼저 layout 상에서 list를 생성할 view 를 선택 한다.
2. 오른쪽에 보면 outline 란이 있는데 이곳에서 + 를 선택한 뒤 ListView 를 선택한다.
3. 동일 한 방법으로 TextView 도 생성 해준다.
4. ListView 의 프로퍼티 메뉴에서 id 를 찾은 후 '반드시'다음과 같이 변경한다. @android:id/list 안드로이드 내의 리소스를 이용해야하기 때문에 사용자 임의의 아이디 값은 적용되지 않기 때문이다.
5. layout height 는 wrap_content, width 는 fill_parent 로 선택한다.
6. TextView 의 프로퍼티 에 id 를 @android:id/empty 로 변경한다. 이렇게 설정해 두면 list 에 내용이 없을 경우 TextView 의 내용을 보여준다.
7. TextViwe 의 text 항목을 res/values/string.xml 에 추가한 뒤 적용한다.
8. 이후 실제 소스
inputText = (EditText)findViewById(R.id.inputText);
inputButton = (Button)findViewById(R.id.inputButton);
list = new ArrayList<String>();
inputButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.add(inputText.getText().toString());
inputText.setText("");
adapter.notifyDataSetChanged();
}
}
);
adapter = new ArrayAdapter<String>
(
this,
android.R.layout.simple_list_item_1,
list
);
setListAdapter(adapter);
기타.
리스트에는 adapter 를 추가해야 하는데 위와 같다.
1. 먼저 layout 상에서 list를 생성할 view 를 선택 한다.
2. 오른쪽에 보면 outline 란이 있는데 이곳에서 + 를 선택한 뒤 ListView 를 선택한다.
3. 동일 한 방법으로 TextView 도 생성 해준다.
4. ListView 의 프로퍼티 메뉴에서 id 를 찾은 후 '반드시'다음과 같이 변경한다. @android:id/list 안드로이드 내의 리소스를 이용해야하기 때문에 사용자 임의의 아이디 값은 적용되지 않기 때문이다.
5. layout height 는 wrap_content, width 는 fill_parent 로 선택한다.
6. TextView 의 프로퍼티 에 id 를 @android:id/empty 로 변경한다. 이렇게 설정해 두면 list 에 내용이 없을 경우 TextView 의 내용을 보여준다.
7. TextViwe 의 text 항목을 res/values/string.xml 에 추가한 뒤 적용한다.
8. 이후 실제 소스
inputText = (EditText)findViewById(R.id.inputText);
inputButton = (Button)findViewById(R.id.inputButton);
list = new ArrayList<String>();
inputButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.add(inputText.getText().toString());
inputText.setText("");
adapter.notifyDataSetChanged();
}
}
);
adapter = new ArrayAdapter<String>
(
this,
android.R.layout.simple_list_item_1,
list
);
setListAdapter(adapter);
기타.
리스트에는 adapter 를 추가해야 하는데 위와 같다.
'Android' 카테고리의 다른 글
change emulator partition size (0) | 2013.10.08 |
---|---|
toast (0) | 2013.10.08 |
auto import (0) | 2013.10.08 |
custom dialog (0) | 2013.10.08 |
dialog (0) | 2013.10.08 |