自定義dialog包含一個顯示圖片的listview
阿新 • • 發佈:2019-01-29
package com.brandroidapp.ui; import java.util.ArrayList; import com.brandroidapp.activity.R; import com.brandroidapp.adapter.ImageListViewAdapter; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.ListView; public class ImageDialog extends Dialog implements android.view.View.OnClickListener{ private static ImageDialog imageDialog=null; private static LayoutInflater layoutInflater; private Context context; public ImageDialog(Context context) { super(context); this.context = context; layoutInflater = LayoutInflater.from(context); } public static ImageDialog Create(Context context) { imageDialog = new ImageDialog(context); imageDialog.setContentView(layoutInflater.inflate(R.layout.onlinedialog, null)); ListView listView = (ListView) imageDialog.findViewById(R.id.listView1); ArrayList<Integer> imageList = new ArrayList<Integer>(); imageList.add(R.drawable.gongshang); imageList.add(R.drawable.nongye); ImageListViewAdapter imageListViewAdapter = new ImageListViewAdapter(context, imageList, layoutInflater); listView.setAdapter(imageListViewAdapter); return imageDialog; } @Override public void onClick(View v) { // TODO Auto-generated method stub } } package com.brandroidapp.adapter; import java.util.List; import com.brandroidapp.activity.R; import com.brandroidapp.adapter.RefreshListViewAdapter.ListItemView; import com.brandroidapp.bean.StockBean; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class ImageListViewAdapter extends BaseAdapter { private Context context;// 執行上下文 private List<Integer> listItems;// 資料集合 private LayoutInflater listContainer;// 檢視容器 static class ListItemView { // 自定義控制元件集合 public ImageView imageView; } public ImageListViewAdapter(Context context, List<Integer> listItems, LayoutInflater listContainer) { super(); this.context = context; this.listItems = listItems; this.listContainer = listContainer; } @Override public int getCount() { // TODO Auto-generated method stub return listItems.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // 自定義檢視 ListItemView listItemView = null; if (convertView == null) { // 獲取list_item佈局檔案的檢視 convertView = listContainer.inflate(R.layout.imagelistviewitem, null); listItemView = new ListItemView(); // 獲取控制元件物件 listItemView.imageView = (ImageView) convertView.findViewById(R.id.imageView1); // 設定控制元件集到convertView convertView.setTag(listItemView); } else { listItemView = (ListItemView) convertView.getTag(); } listItemView.imageView.setImageResource(listItems.get(position)); return convertView; } } ImageDialog imageDialog = ImageDialog.Create(OnLineActivity.this); imageDialog.show();//呼叫程式碼