Search

'ArrayAdapter'에 해당되는 글 1건

  1. 2011.01.06 Android - ListView / Custom ListView

Android - ListView / Custom ListView

Programming/안드로이드 2011.01.06 23:31 Posted by 파란크리스마스
ListView 제어를 MVC 모델처럼 BaseAdapter 의 getCount, getView, getItem을 구현하여
List를 보여주는 예제입니다.

ListViewTest.java

ListView를 listview 로 생성하여
BaseAdapter 상속 받아서 구현한 ArrayAdapterExample 객체를
listview.setAdapter()호출하여 listview의 adapter로 설정합니다.
package com.shryu.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewTest extends Activity implements OnClickListener {
	
  private TextView text; 

  private ArrayAdapterExample adapter;
	
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    
    LayoutInflater inflate = LayoutInflater.from(this);
    LinearLayout layout = (LinearLayout)inflate.inflate(R.layout.main, null);
    this.setContentView(layout); 
    
    text = (TextView)findViewById(R.id.TextView02); 
    
    ListView listview = new ListView(this);
    listview.setLayoutParams(new LinearLayout.LayoutParams(
    		LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 
    layout.addView(listview);
    
    adapter = new ArrayAdapterExample(this, R.layout.list_item_1);
    listview.setAdapter(adapter);
  }

  @Override
  public void onClick(View v) {
    Button btn = (Button)v;
    text.setText(btn.getText());
  }
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/TextView02"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
ArrayAdapterExample.java
package com.shryu.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class ArrayAdapterExample extends BaseAdapter implements Filterable {

	private Context mContext; 
	private int mFieldId = 0;

	public ArrayAdapterExample(Context context, int textViewResourceId) {
		super();
		mContext = context;
		mFieldId = textViewResourceId;
	}

	@Override
	public int getCount() {
		return 100;
	}

	@Override
	public Object getItem(int position) {
		String result = new String(position+"");
		return result;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView; 
		if (v == null) { 
			LayoutInflater vi = LayoutInflater.from(mContext); 
			v = vi.inflate(R.layout.list_item_1, null);
		}
			
		String value = (String)this.getItem(position); 
		if (value != null) { 
			TextView tt = (TextView) v.findViewById(R.id.TextView01);  
			tt.setText("position = " + value);                        
			Button btn = (Button) v.findViewById(R.id.Button01);  
			btn.setText(value + " 선택"); 
			btn.setOnClickListener((OnClickListener)mContext);    
		}
		return v; 
	}

	@Override
	public Filter getFilter() {
		return null;
	}
}
list_item_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
<TextView android:text="@+id/TextView01" 
	android:id="@+id/TextView01"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"/>
<Button android:text="@+id/Button01" 
	android:id="@+id/Button01"
	android:layout_width="wrap_content" 
	android:layout_height="30px"/>
</LinearLayout>
-end-
신고


 

티스토리 툴바