カスタムdialogはlistview(二)を含む

4036 ワード

半透明のlistview dialogがポップアップされます.
public class MySingleSelDialog  extends Dialog{
   private ArrayList<Restaurant> restaurants;
   private String title;
   private Context context;
   private ListView lv_list;
   private selRestaurantApdater apdater;
	TextView tv_title;
	Button btn_cancel;
	public MySingleSelDialog(Context context,ArrayList<Restaurant> restaurants,String title) {
		super(context);
		this.context = context;
		this.restaurants = restaurants;
		this.title = title;
	}
	public void setTitle(String title){
		this.title = title;
		tv_title.setText(title);
	}
	/* (non-Javadoc)
	 * @see android.app.Dialog#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.single_sel_dialog);
		lv_list = (ListView) findViewById(R.id.lv_list);
		tv_title = (TextView) findViewById(R.id.tv_title);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);
		btn_cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
		apdater = new selRestaurantApdater(getContext(), restaurants);
		lv_list.setAdapter(apdater);
		lv_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getContext(), restaurants.get(position).getId().toString(), Toast.LENGTH_SHORT).show();
			}
		});
	}

}

   
public class selRestaurantApdater extends BaseAdapter {
    private ArrayList<Restaurant> restaurants;
    private LayoutInflater  inflater;
    public selRestaurantApdater(Context context,ArrayList<Restaurant> restaurants) {
    	this.restaurants = restaurants;
    	this.inflater = LayoutInflater.from(context);
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return restaurants.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.textview1, null);
			viewHolder = new Holder();
			viewHolder.tView = (TextView) convertView.findViewById(R.id.textView);
			viewHolder.tView.setText(restaurants.get(position).getName());
	        convertView.setTag(viewHolder);
		} else {
			viewHolder = (Holder) convertView.getTag();
		}
		return convertView;
	}
	class Holder {
		TextView tView;
	}

}

テスト:
public class testDialog extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ArrayList<Restaurant> restaurants =  new ArrayList<Restaurant>();
		for (int i=0;i<5;i++) {
		   Restaurant restaurant = new Restaurant();
		   restaurant.setId(i);
		   restaurant.setName("name"+i);
		   restaurants.add(restaurant);
		}
		MySingleSelDialog mdialog = new MySingleSelDialog(this, restaurants," ");
		mdialog.show();
	}