public class ReaderActivity extends Activity
{
private ShelfAdapter mAdapter;
// 书架列表
private ListView shelf_list;
private Button shelf_image_button;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.shelf);
init();
mAdapter = new ShelfAdapter();// new adapter对象才能用
shelf_list.setAdapter(mAdapter);
}
private void init()
{
shelf_image_button = (Button) findViewById(R.id.shelf_image_button);
shelf_list = (ListView) findViewById(R.id.shelf_list);
}
// ShelfAdapter类 继承BaseAdapter
public class ShelfAdapter extends BaseAdapter
{
// 假设数据
int[] size = new int[10];
// 构造函数
public ShelfAdapter()
{
}
@Override
public int getCount()
{
if (size.length > 3)
{
return size.length;
} else
{
return 3;
}
}
@Override
public Object getItem(int position)
{
return size[position];
}
@Override
public long getItemId(int position)
{
return position;
}
// 得到新生的视图
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// 生成布局适配器
LayoutInflater layout_inflater = (LayoutInflater) ReaderActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//生成新的View
View layout = layout_inflater.inflate(R.layout.shelf_list_item,
null);
Button button_1, button_2, button_3;
button_1 = (Button) layout.findViewById(R.id.button_1);
button_2 = (Button) layout.findViewById(R.id.button_2);
button_3 = (Button) layout.findViewById(R.id.button_3);
// 为button设置监听
button_1.setOnClickListener(new ButtonOnClick());
button_2.setOnClickListener(new ButtonOnClick());
button_3.setOnClickListener(new ButtonOnClick());
return layout;
}
};
public class ButtonOnClick implements OnClickListener
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button_1:
Toast.makeText(ReaderActivity.this, "button1",
Toast.LENGTH_SHORT).show();
break;
case R.id.button_2:
Toast.makeText(ReaderActivity.this, "button2",
Toast.LENGTH_SHORT).show();
break;
case R.id.button_3:
Toast.makeText(ReaderActivity.this, "button3",
Toast.LENGTH_SHORT).show();
break;
}
}
}
}
转载:http://www.adobex.com/android/source/details/00000397.htm