안녕하세요^^ 일요일에도 열심히 하시는 안펍여러분들!
너무 수고하시네요..질문이 하나있습니다^^
우선 첨부된 파일 한번 봐주세요~ㅠㅠ
ListView를 커스텀어댑터를 사용해 구성했는데요..
우선 이미지와 text두가지형식으로 만들었구요..
ArrayList를 만들어서 .add시켰습니다..
그런데 그림파일 처럼 문제점이..마지막에 add된놈만 보이더라구요..
그렇다고 다른놈들은 아예 안보이는것도 아닌
이미지와 텍스트는 안나오고 선택만 되는 그런상황이 발생합니다.;;
아무리 머리를 쥐어짜도 해답이 나오질 않네요 ㅠㅠ 명쾌한 답변을 기다리겠습니다!
혹시 필요하실지도 몰라 이쪽부분 java코드와 xml코드를 올려봅니다^^ 그럼모두 건승하세요~
public class Setting extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.setting);
ArrayList<Set> s_orders = new ArrayList<Set>();
Set s1 = new Set(R.drawable.btn0, "Setting 1");
Set s2 = new Set(R.drawable.btn1, "Setting 2");
Set s3 = new Set(R.drawable.btn3, "Setting 3");
s_orders.add(s1);
s_orders.add(s2);
s_orders.add(s3);
s_orders.add(s3);
SettingAdapter s_adapter = new SettingAdapter(this, R.layout.row, s_orders);
setListAdapter(s_adapter);
}
private class SettingAdapter extends ArrayAdapter<Set>{
private ArrayList<Set> items;
public SettingAdapter(Context context,
int textViewResourceId,
ArrayList<Set> items){
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(v==null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Set s = items.get(position);
if(s != null){
ImageView iv = (ImageView)findViewById(R.id.icon);
TextView tv = (TextView)findViewById(R.id.label);
if(iv != null){iv.setBackgroundResource(s.getImage());}
if(tv != null){tv.setText(s.getText());}
}
return v;
}
}
public class Set{
private int iconId;
private String textSet;
public Set(int iconId, String textSet){
this.iconId = iconId;
this.textSet = textSet;
}
public int getImage(){
return iconId;
}
public String getText(){
return textSet;
}
}
}이쪽은 row.xml(각행)이구요
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minHeight="62px">
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2.5">
<ImageView android:layout_gravity="center_vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/icon"></ImageView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent">
<TextView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/label" android:textSize="30px"></TextView>
</LinearLayout>
</LinearLayout>
이쪽은 각행들이 출력될 setting.xml입니다.
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientation = "vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/background"> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

View getView(int position, View convertView, ViewGroup parent) 메소드에서
ImageView iv = (ImageView)findViewById(R.id.icon); -> ImageView iv = (ImageView)v.findViewById(R.id.icon);
TextView tv = (TextView)findViewById(R.id.label); -> TextView tv = (TextView)v.findViewById(R.id.label);
API 문서를 살펴보면,
Activity.findViewById(int id)
: Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
View.findViewById(int id)
: Look for a child view with the given id.
라고 나와 있습니다.
R.id.icon 과 R.id.label 로 대표되는 view 는 Activity 의 onCreate(Bundle) 에서 처리된 XML(setting.xml) 에서
찾을수 있는 view 가 아니라, row.xml 에서 전개된 view(getView 메소드의 convertView 또는 v) 의 자식 view 이므로
convertView.findViewById(int) 나 v.findViewById(int) 로 얻어오는 것이 맞겠죠.




생성자 부분에 로그찍고, getView안에 명령마다 로그를 찍었는데
getView가 한번만 실행되고 말아버리더군요,,,
이건 우찌된일인지......