• 非常标准的类,获得和展示listview或gridview数据。并且点击获得详情页面。


    1、

    获得和展示listview或gridview

    package com.anzi.jmsht.ui;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    import com.anzi.jmsht.AppException;
    import com.anzi.jmsht.AppManager;
    import com.anzi.jmsht.Constant;
    import com.anzi.jmsht.net.xml.api.Net;
    import com.anzi.jmsht.ui.ImgActivity.ImgGridViewAdapter;
    import com.anzi.jmsht.util.AsyncLoader;
    import com.anzi.jmsht.util.Constants;
    import com.anzi.jmsht.util.PreferencesHelper;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.GridView;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemClickListener;
    
    /**
     * 应用
     * 
     * @author tf
     * 
     */
    public class PicListActivity extends Activity {
    
    	private GridView picgrid;
    	private List<Map<String, Object>> list;
    	SharedPreferences sp;
    	private ExecutorService fixedThreadPool;
    	private PreferencesHelper ph;
    	private HashMap<String, Object> map;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.activity_pic_list);
    		// 注册默认的未捕捉异常处理类
    		Thread.setDefaultUncaughtExceptionHandler(AppException
    				.getAppExceptionHandler());
    		// 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待:
    		fixedThreadPool = Executors.newFixedThreadPool(3);
    		AppManager.getAppManager().addActivity(this);
    		ph = new PreferencesHelper(getApplicationContext(), Constants.ANZIDate);
    		initView();
    		getData();
    	/*	VideoListAdapter adapter = new VideoListAdapter(this, list);
    		picgrid.setAdapter(adapter);*/
    		setListener();
    	}
    
    	private void getData() {
    		list = new ArrayList<Map<String, Object>>();
    		// 获得线程消息
    		final Handler handler = new Handler() {
    			@Override
    			public void handleMessage(Message msg) {
    				String s = (String) msg.obj;
    				if ("ok".equals(s)) {
    					// 显示余额
    					/*ImgGridViewAdapter adapter = new ImgGridViewAdapter(
    							PicListActivity.this, list);
    					imgGridView.setAdapter(adapter);*/
    					VideoListAdapter adapter = new VideoListAdapter(PicListActivity.this, list);
    					picgrid.setAdapter(adapter);
    				} else if ("no".equals(s)) {
    					Toast.makeText(getApplicationContext(), "系统问题", 1)
    							.show();
    				}
    			}
    		};
    //线程池执行线程任务 fixedThreadPool.execute(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { map = new HashMap<String, Object>(); String result = Net.taojin(Constant.taojinUrl + "/querydata.shtml", "type", "images", "userId", ph.getValue("uid"), "currentPageNo", "1", "pageSize", "100"); // 解析json字符串 // result = result.substring(1, result.length()-1); Log.i("图片结果", result); JSONObject jsonObject = new JSONObject(result); map.put("code", jsonObject.getString("code"));// 状态0异常 1正常 JSONArray jsonArray = jsonObject.getJSONArray("data"); JSONObject jsonObject2; for (int i = 0; i < jsonArray.length(); i++) { Map<String, Object> map = new HashMap<String, Object>(); jsonObject2 = (JSONObject) jsonArray.opt(i); map.put("id", jsonObject2.getInt("id"));// 视频id map.put("name", jsonObject2.getString("name"));// 图片名字 map.put("path", jsonObject2.getString("path"));// 图片地址 map.put("integral", jsonObject2.getString("integral"));// 积分 map.put("pic_collect_number", "10人收藏"); map.put("pic_seen_number", "1人观看"); map.put("collect", "0"); map.put("get", "0"); // map.put("bitmap", // Net.getBitmap(jsonObject2.getString("path")));//图片 //map.put("path", jsonObject2.getString("path")); list.add(map); }
    //发送消息,通知Handler Message msg = new Message(); msg.obj = "ok"; handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); // 通知UI Message msg = new Message(); msg.obj = "no"; handler.sendMessage(msg); } } }); } private void initView() { picgrid = (GridView) findViewById(R.id.picgrid); } private void setListener() {
    //点击详情,进入详情页面 picgrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub

    //传值id ,img,integral到BigImgActivity
    Intent i = new Intent(PicListActivity.this, BigImgActivity.class); i.putExtra("img", (String) list.get(position).get("path")); i.putExtra("integral", (String) list.get(position).get("integral")); i.putExtra("id", (Integer) list.get(position).get("id")); startActivity(i); } }); /* picgrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // ByteArrayOutputStream bs = new ByteArrayOutputStream(); // Bitmap bitmap = (Bitmap)list.get(position).get("bitmap"); // bitmap.compress(Bitmap.CompressFormat.JPEG,100,bs); Intent i = new Intent(PicListActivity.this, BigImgActivity.class); i.putExtra("img", (String) list.get(position).get("path")); i.putExtra("integral", (String) list.get(position).get("integral")); i.putExtra("id", (Integer) list.get(position).get("id")); Toast.makeText(PicListActivity.this, "点击了"+position, 0).show(); startActivity(i); } });*/ } class VideoListAdapter extends BaseAdapter { private AsyncLoader loader; private Context context; private List<Map<String, Object>> data; private LayoutInflater inflater; public VideoListAdapter(Context context, List<Map<String, Object>> data) { this.context = context; this.data = data; inflater = LayoutInflater.from(context); loader = new AsyncLoader(context); } @Override public int getCount() { // TODO Auto-generated method stub return data.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @SuppressLint("ResourceAsColor") @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (null == convertView) { convertView = inflater.inflate(R.layout.pic_grid_item, null); holder = new ViewHolder(); holder.pic_name = (TextView) convertView .findViewById(R.id.pic_name); holder.pic_collect_number = (TextView) convertView .findViewById(R.id.pic_collect_number); holder.pic_seen_number = (TextView) convertView .findViewById(R.id.pic_seen_number); holder.pic_integral = (TextView) convertView .findViewById(R.id.pic_integral); holder.pic_integral_get = (TextView) convertView .findViewById(R.id.pic_integral_get); holder.pic_btn_collect = (ImageButton) convertView .findViewById(R.id.pic_btn_collect); holder.re_pic_collected = (RelativeLayout) convertView .findViewById(R.id.re_pic_collected); holder.re_pic_collect = (RelativeLayout) convertView .findViewById(R.id.re_pic_collect); holder.pic_iamge_icon = (ImageView) convertView .findViewById(R.id.pic_iamge_icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if ("1".equals(data.get(position).get("collect"))) {// 已收藏 holder.re_pic_collected.setVisibility(View.VISIBLE); holder.re_pic_collect.setVisibility(View.GONE); } else {// 未收藏 holder.re_pic_collected.setVisibility(View.GONE); holder.re_pic_collect.setVisibility(View.VISIBLE); } if ("1".equals(data.get(position).get("get"))) {// 已获得积分 holder.pic_integral.setVisibility(View.GONE); holder.pic_integral_get.setVisibility(View.VISIBLE); } else {// 未获得积分 holder.pic_integral.setVisibility(View.VISIBLE); holder.pic_integral.setText((CharSequence) data.get(position).get("integral")); holder.pic_integral_get.setVisibility(View.GONE); } holder.pic_btn_collect.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub holder.re_pic_collected.setVisibility(View.VISIBLE); holder.re_pic_collect.setVisibility(View.GONE); list.get(position).put("collect", "1"); } }); holder.pic_name.setText((CharSequence) data.get(position).get("name")); holder.pic_collect_number.setText((CharSequence) data.get(position).get( "pic_collect_number")); holder.pic_seen_number.setText((CharSequence) data.get(position).get( "pic_seen_number")); holder.pic_integral.setText("赠送积分:"+(CharSequence) data.get(position).get("integral"));

    //异步加载图片
    loader.displayImage((String) data.get(position).get("path"), holder.pic_iamge_icon); // holder.ll_bg_video_pic.setBackgroundDrawable(background) return convertView; } // 视图容器类,属性对应布局文件元素 private class ViewHolder { TextView pic_name, pic_collect_number; TextView pic_seen_number, pic_integral, pic_integral_get; RelativeLayout re_pic_collected, re_pic_collect; ImageButton pic_btn_collect; ImageView pic_iamge_icon; } } }

    2.BigImgActivity.class

    package com.anzi.jmsht.ui;
    
    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import com.anzi.jmsht.AppException;
    import com.anzi.jmsht.AppManager;
    import com.anzi.jmsht.Constant;
    import com.anzi.jmsht.Info;
    import com.anzi.jmsht.net.xml.api.Net;
    import com.anzi.jmsht.util.AsyncLoader;
    import com.anzi.jmsht.util.Constants;
    import com.anzi.jmsht.util.DESCode;
    import com.anzi.jmsht.util.Md5Util;
    import com.anzi.jmsht.util.PreferencesHelper;
    import com.anzi.jmsht.util.PullHandler;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    /**
     * 大图片预览
     * 
     * @author hhh
     * 
     */
    public class BigImgActivity extends Activity implements OnClickListener {
    
    	private ExecutorService fixedThreadPool;
    	// private Button back;
    	private ImageView bigImg;
    	private String integral;
    	private PreferencesHelper ph;
    	private ArrayList<Info> States;
    	private int id;
    	private String imgpath;
    	private AsyncLoader loader;
    	private Handler handler = new Handler(){
    		public void handleMessage(Message msg) {
    			switch (msg.what) {
    			case 1:
    				addIntengral();
    				break;
    
    			default:
    				break;
    			}
    		}
    	};
    	private Timer timer;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.activity_big_img);
    		// 注册默认的未捕捉异常处理类
    		Thread.setDefaultUncaughtExceptionHandler(AppException
    				.getAppExceptionHandler());
    		// 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待:
    		fixedThreadPool = Executors.newFixedThreadPool(3);
    		AppManager.getAppManager().addActivity(this);
    		ph = new PreferencesHelper(this, Constants.ANZIDate);
    		loader = new AsyncLoader(this);
    
    		getImg();
    		initView();
    		//10秒后获取积分
    		timer = new Timer();
    		timer.schedule(new MyTask(), 10000);
    	}
    
    	@Override
    	protected void onDestroy() {
    		super.onDestroy();
    		if (fixedThreadPool != null) {
    			fixedThreadPool.shutdown();
    		}
    	}
    
    	/*
    	 * 执行延时任务
    	 */
    	private class MyTask extends TimerTask{
    		@Override
    		public void run() {
    			Message message = new Message();
    			message.what = 1;
    			handler.sendMessage(message);
    		}
    
    	}
    
    	/*
    	 * 获得图片数据
    	 */
    	private void getImg() {
    		Intent i = getIntent();
    		imgpath = i.getStringExtra("img");
    		integral = i.getStringExtra("integral");
    		id = i.getIntExtra("id", 0);
    			}
    
    	private void initView() {
    		// back = (Button) findViewById(R.id.back);
    		// back.setOnClickListener(this);
    		bigImg = (ImageView) findViewById(R.id.bigImg);
    		bigImg.setOnClickListener(this);
    		// bigImg.setImageBitmap(bmp);
    		loader.displayImage(imgpath, bigImg);
    	}
    
    	@Override
    	public void onClick(View v) {
    		switch (v.getId()) {
    		case R.id.bigImg:
    			AppManager.getAppManager().finishActivity();
    			break;
    
    		default:
    			break;
    		}
    	}
    
    	/*
    	 * 查看图片添加积分
    	 */
    	private void addIntengral() {
    		// 获得线程消息
    		final Handler handler = new Handler() {
    			@Override
    			public void handleMessage(Message msg) {
    				String s = (String) msg.obj;
    				if ("ok".equals(s)) {
    					// 显示余额
    					Toast.makeText(getApplicationContext(),
    							(String) States.get(0).getItem("result"), 1).show();
    				} else if ("no".equals(s)) {
    					Toast.makeText(getApplicationContext(), "系统问题", 1)
    					.show();
    				}
    			}
    		};
    		fixedThreadPool.execute(new Runnable() {
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				try {
    					String result = Net.addIntengral(
    							Constant.netcard + "/addintegralyqs.xhtml",
    							"xml",
    							DESCode.requestXml("<?xml version="1.0" encoding="UTF-8"?><root><ReCord><sigen>"
    									+ DESCode.responseXml(ph.getValue("sigen"))
    									.split("_")[1]
    											+ "</sigen><integral>"
    											+ integral
    											+ "</integral><id>"
    											+ id
    											+ "</id><type>image</type></ReCord></root>"),
    											"key", Md5Util.MD5(integral));

    // private ArrayList<Info> States; States = PullHandler.URLReadXmlByPull(result, new String[] { "status", "result" }, "ReCord"); // 通知UI Message msg = new Message(); msg.obj = "ok"; handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); // 通知UI Message msg = new Message(); msg.obj = "no"; handler.sendMessage(msg); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //用户点击返回键时停止观看图片,计时器关闭 timer.cancel(); AppManager.getAppManager().finishActivity(); return true; } return super.onKeyDown(keyCode, event); } }
  • 相关阅读:
    Props属性
    逆卷积:convtranspose2d(fractionally-strided convolutions)
    nn.ReflectionPad2d(镜像填充)
    conv1*1的作用
    如何将jupyter中的.ipynb文件转换成python中的.py文件
    低光图像增强学习
    pytorch 中的variable函数
    BCELoss和BCEWithLogitsLoss
    正则化的理解
    网络压缩方法总结
  • 原文地址:https://www.cnblogs.com/childhooding/p/4461872.html
Copyright © 2020-2023  润新知