앞에서 설명한 것처럼 어댑터를 확장하여 ListActivity에서 사용해야합니다. TextView와 ImageView를 사용하여 XML 파일을 만듭니다 (LinearLayout 또는 기타 레이아웃 사용). TextView 및 ImageView에 ID를 추가하여 목록의 위치에 따라 ID를 변경할 수 있습니다.
다음은 3 개의 행을 만들고 그 안에 텍스트를 a, b, c로 설정하는 코드 예제입니다.
public class MyActivity extends ListActivity {
private String[] data = {"a","b","c"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MyAdapter(this, R.layout.rowxml, data));
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context c, int i, String[] s) {
super(c, i, s);
}
@Override
public View getView(int position, View v, ViewGroup parent) {
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.rowxml, null);
}
TextView tw = (TextView) v.findViewById(R.id.text);
if (tw != null) tw.setText(data[position]);
// You can do something similar with the ImageView
return v;
}
}
}
[Android 개발자 자원] (http://developer.android.com/resources/index.html) –