A very standard class that obtains and exhibits ListView or GridView data. and click the Get Details page.

Source: Internet
Author: User

1.

Get and show a ListView or 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;/** * Application * * @author TF * */public class PicL Istactivity extends Activity {private GridView picgrid;private list<map<string, object>> List; Sharedpreferences sp;private executorservice fixedthreadpool;private preferenceshelper ph;private HashMap<String, object> map; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); GetWindow (). Requestfeature (Window.feature_no_title); Setcontentview (r.layout.activity_pic_list);// Registers the default non-catching exception handling class Thread.setdefaultuncaughtexCeptionhandler (Appexception.getappexceptionhandler ());//Create a thread pool to control the maximum number of concurrent threads, and the excess threads will wait in the queue: Fixedthreadpool = Executors.newfixedthreadpool (3);Appmanager.getappmanager (). Addactivity (This);p h = 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>> ();//Get thread messagefinal Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {String s = (string) msg.obj;if ("OK". Equals (s)) {//Display balance/*imggridviewada Pter adapter = new Imggridviewadapter (piclistactivity.this, list); Imggridview.setadapter (adapter); */ Videolistadapter adapter = new Videolistadapter (piclistactivity.this, list);p Icgrid.setadapter (adapter);} else if ("No". Equals (s)) {Toast.maketext (Getapplicationcontext (), "System Problem", 1). Show ();}};
//thread pool execution thread taskFixedthreadpool.execute (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubtry {map = new hashmap<string, object> (); String result = Net.taojin (constant.taojinurl+ "/querydata.shtml", "type", "Images", "UserId", Ph.getvalue ("UID"), " Currentpageno "," 1 "," pageSize "," 100 ");//Parse JSON string//result = result.substring (1, Result.length ()-1); LOG.I ("Picture result", result); Jsonobject jsonobject = new Jsonobject (result), Map.put ("Code", Jsonobject.getstring ("Code"));//Status 0 exception 1 normal jsonarray Jsonarray = Jsonobject.getjsonarray ("Data"); Jsonobject jsonobject2;for (int i = 0; i < jsonarray.length (); i++) {map<string, object> Map = new Hashmap<str ING, object> (); jsonObject2 = (jsonobject) jsonarray.opt (i); Map.put ("id", Jsonobject2.getint ("id"));//Video Idmap.put ("Name", Jsonobject2.getstring ("name"));//Picture name Map.put ("Path", jsonobject2.getstring ("path");//Picture address map.put (" Integral ", jsonobject2.getstring (" Integral "));//Integral Map.put (" Pic_collect_number "," 10 people Collection "); Map.put (" Pic_seen_ Number "," 1 people viewing "); Map.put (" Collect ","0 "); Map.put (" Get "," 0 ");//Map.put (" Bitmap ",//Net.getbitmap (jsonobject2.getstring (" path "));//Picture//map.put (" path " , jsonobject2.getstring ("path")); List.add (map);}
//Send a message to notify HandlerMessage msg = new Message ();Msg.obj = "OK"; handler.sendmessage (msg);} catch (Exception e) {e.printstacktrace ();//notification Uimessage msg = new Message () Msg.obj = "No"; Handler.sendmessage (msg);}}); }private void Initview () {Picgrid = (GridView) Findviewbyid (R.id.picgrid);} private void Setlistener () {
//Click Details to go to the details pagePicgrid.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int position,long arg3) {//TODO auto-generated met Hod stub

//value ID, img,integral to bigimgactivity
intent i = new Intent (piclistactivity.this, bigimgactivity.class); i.putextra ("img ", (String) list.get (position). Get ("path")); I.putextra (" Span style= "color: #ff0000;" >integral ", (String) list.get (position). Get (" integral ")); I.putextra (" id , (Integer) list.get (position). Get ("id")); startactivity (i); }});/*picgrid.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic 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, "clicked" +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&LT;MAP <string, object>> data) {This.context = Context;this.data = Data;inflater = layoutinflater.frOm (context); loader = new Asyncloader (context);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn data.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn data.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @SuppressLint ("Resourceascolor") @Overridepublic 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 = n EW 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"))) {//Favorites holder.re_pic_collected.setVisibility (view.visible); Holder.re_pic_collect.setVisibility (View.gone);} else {//not collectible holder.re_pic_collected.setVisibility (view.gone); holder.re_pic_collect.setVisibility (view.visible);} if ("1". Equals (Data.get (position). Get ("get")) {//Earned points holder.pic_integral.setVisibility (view.gone); holder.pic_ Integral_get.setvisibility (view.visible);} else {//not earned points 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 () {@Overridepublic void OnClick (View arg0) {//TODO Auto-generated method Stubholder.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 ("Bonus points:" + (charsequence) data.get (position ). Get ("integral"));

//Loading pictures asynchronously
loader.displayimage (String) data.get (position). Get ("path"), Holder.pic_iamge_icon);Holder.ll_bg_video_pic.setBackgroundDrawable (background) return convertview;} View Container class, property corresponding to layout file element 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;/** * Big Picture Preview * * @author HHH * */public C Lass 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, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); GetWindow (). Requestfeature (Window.feature_no_title); Setcontentview (r.layout.activity_big_img);// Registers the default non-catching exception handling class Thread.setdefaultuncaughtexceptionhandler (Appexception.getappexceptionhandler ());//create a fixed-line pool, You can control the maximum number of concurrent threads, and the excess threads will wait in the queue:Fixedthreadpool = Executors.newfixedthreadpool (3); Appmanager.getappmanager (). addactivity (this);ph = new Preferenceshelper (this, constants.anzidate); loader = new Asyncloader (this); Getimg (); Initview ();// 10 seconds to get the integral timer = new timer (); Timer.schedule (new MyTask (), 10000);} @Overrideprotected void OnDestroy () {Super.ondestroy (); if (fixedthreadpool! = null) {Fixedthreadpool.shutdown ();}} /* * Perform delay task */private class MyTask extends timertask{@Overridepublic void run () {Message message = new Message (); MESSAGE.W hat = 1;handler.sendmessage (message);}}/ * * Get Picture data * /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);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.bigimg:appmanager.getappmanager (). Finishactivity ( ); break;default:break;}} /* * View picture add points */private void Addintengral () {//Get thread message final Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {String s = (string) msg.obj;if ("OK". Equals (s)) {//Display balance toast.maketext (g Etapplicationcontext (), (String) states.get (0). GetItem ("Result"), 1). Show (); else if ("No". Equals (s)) {Toast.maketext (Getapplicationcontext (), "System Problem", 1). Show ();}};Fixedthreadpool.execute (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubtry {String result = Net.addintengral (Constant.netcard + "/A Ddintegralyqs.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");//Notify Uimessage msg = new Message ();Msg.obj = "OK"; handler.sendmessage (msg);} catch (Exception e) {e.printstacktrace ();//notification Uimessage msg = new Message () Msg.obj = "No"; Handler.sendmessage (msg);}}); } @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = keyevent.keycode_back&& Event.getrepeatcount () = = 0) {//When the user clicks the Back button to stop viewing the picture, the timer closes Timer.cancel (); Appmanager.getappmanager (). finishactivity (); return true;} Return Super.onkeydown (KeyCode, event);}}

A very standard class that obtains and exhibits ListView or GridView data. and click the Get Details page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.