自定义dialog包含一个显示图片的listview

时间:2022-07-03 19:44: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();//调用代码