검색결과 리스트
글
커스텀 항목 뷰는 원하는 항목 배치에 좋다.
먼저 커스텀 레이아웃을 작성한다.
mail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="test"
android:layout_alignParentRight="true"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list"
/>
</RelativeLayout>
Item 클래스
public class Item {
String _name;
public Item(String name){
this._name = name;
}
}
Adapter 클래스
class ListAdapter extends BaseAdapter{
Context _main;
LayoutInflater _inflater;
ArrayList<Item> _item;
int _layout;
public ListAdapter(Context context, int layout, ArrayList<Item> arr){
this._main=context;
this._inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this._layout=layout;
this._item=arr;
}
public int getCount(){
return this._item.size();
}
public String getItem(int index){
return this._item.get(index)._name;
}
public long getItemId(int index){
return index;
}
public View getView(int index, View view, ViewGroup parent){
final int idx = index;
if(view==null){
view = this._inflater.inflate(this._layout,parent,false);
}
TextView txtview = (TextView)view.findViewById(R.id.text);
txtview.setText(this._item.get(idx)._name);
Button btn = (Button)view.findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
String str = _item.get(idx)._name;
Toast.makeText(_main, str, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
메인 클래스
public class ImageViewActivity extends Activity{
ArrayList<Item> arr;
ListView list ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arr = new ArrayList<Item>();
Item item;
item = new Item("first");
arr.add(item);
item = new Item("second");
arr.add(item);
item = new Item("third");
arr.add(item);
ListAdapter adapter = new ListAdapter(this,R.layout.main,arr);
ListView list;
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
실행을 하면 어댑터를 통하여 제어를 하는 것이 확인 가능하다.
RECENT COMMENT