- 浏览: 80584 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
这节将阐述一个listview 的滑动分页效果,和contextmenu用法,根据listview选项弹出一个contextmenu菜单,再根据所选择的菜单项执行查看详情或者编辑提交,效果如图:
自定义listview项:
contextmenu,长按选项唤出:
选择详情菜单:
选择编辑菜单:
滑动分页,当大滚动到最好一项时,自动加载下一页:
代码:
package com.gk.view; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnCreateContextMenuListener; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import com.gk.R; import com.gk.data.AnswerServiceHelper; import com.gk.data.TaskByPageHelper; import com.gk.data.UserDataServiceHelper; import com.gk.model.Health; public class taskView extends Activity implements OnCreateContextMenuListener, OnScrollListener { private View view; private ListView listview; private TextView cardid; private TextView recorder; private TextView consultationtype; private TextView consultationexpert; private TextView consultationcontent; private TextView answer; private TextView answer1; private TextView clientname; private AlertDialog selfdialog; private Health health; private ProgressBar progressBar; private ProgressDialog progressdialog; private int lastItem = 0; private SimpleAdapter adapter; private int page = 1; private int allpage = 1; private List<Health> heallist = new ArrayList<Health>(); private List<Map<String, Object>> httpResult = new ArrayList<Map<String, Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // LayoutInflater inflater = (LayoutInflater) // getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); // view = inflater.inflate(R.layout.taskview, null); adapter = new SimpleAdapter(this, httpResult, R.layout.taskview, new String[] { "title", "info", "img" }, new int[] { R.id.title, R.id.info, R.id.img }); listview = new ListView(this); listview.setAdapter(adapter); listview.setOnCreateContextMenuListener(this); listview.setOnScrollListener(this); setContentView(listview); dealData(); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("详情菜单"); menu.add(0, 0, 1, "查看详情"); menu.add(0, 1, 2, "编辑解答"); menu.add(0, 2, 3, "取消查看"); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item .getMenuInfo(); health = heallist.get(menuinfo.position); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("iid", health.getIid().toString())); String host = UserDataServiceHelper.getHost(); String uri = host + "/task.do?task=getHealthById"; Bundle bundle = UserDataServiceHelper.getObjectByid(uri, params); if (item.getItemId() == 0) { if (bundle != null) { health = (Health) bundle.get("health"); // 创建对话框 LayoutInflater inflater = (LayoutInflater) getApplicationContext() .getSystemService(LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.detail, null); // 设置 cardid = (TextView) view.findViewById(R.id.cardid); recorder = (TextView) view.findViewById(R.id.recorder); consultationtype = (TextView) view .findViewById(R.id.consultationtype); consultationexpert = (TextView) view .findViewById(R.id.consultationexpert); consultationcontent = (TextView) view .findViewById(R.id.consultationcontent); answer = (TextView) view.findViewById(R.id.answer); clientname = (TextView) view.findViewById(R.id.clientname); cardid.setText(health.getCardid()); recorder.setText(health.getRecorder()); consultationtype.setText(health.getConsultationtype()); consultationexpert.setText(health.getConsultationexpert()); consultationcontent.setText(health.getConsultationcontent()); answer.setText(health.getAnswer()); clientname.setText(health.getName()); AlertDialog.Builder ad = new AlertDialog.Builder(taskView.this); ad.setView(view); ad.setTitle("详情").create().show(); Toast.makeText(this, page + "/" + allpage + "页", Toast.LENGTH_LONG).show(); } } if (item.getItemId() == 1) { if (bundle != null) { health = (Health) bundle.get("health"); // 创建对话框 LayoutInflater inflater = (LayoutInflater) getApplicationContext() .getSystemService(LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.detail, null); // 设置 cardid = (TextView) view.findViewById(R.id.cardid); recorder = (TextView) view.findViewById(R.id.recorder); consultationtype = (TextView) view .findViewById(R.id.consultationtype); consultationexpert = (TextView) view .findViewById(R.id.consultationexpert); consultationcontent = (TextView) view .findViewById(R.id.consultationcontent); answer = (TextView) view.findViewById(R.id.answer); answer.setVisibility(View.INVISIBLE);// 设置不可见 answer1 = (TextView) view.findViewById(R.id.answer1); answer1.setVisibility(View.VISIBLE);// 设置可见 clientname = (TextView) view.findViewById(R.id.clientname); cardid.setText(health.getCardid()); recorder.setText(health.getRecorder()); consultationtype.setText(health.getConsultationtype()); consultationexpert.setText(health.getConsultationexpert()); consultationcontent.setText(health.getConsultationcontent()); answer1.setText(health.getAnswer()); clientname.setText(health.getName()); AlertDialog.Builder ad = new AlertDialog.Builder(taskView.this); ad.setView(view); ad.setTitle("解答"); selfdialog = ad.create(); selfdialog.setButton("提交", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String host = UserDataServiceHelper.getHost(); String uri = host + "/task.do?task=updateAnswer"; String answer = answer1.getText().toString(); String iid = health.getIid().toString(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add((new BasicNameValuePair("answer", answer))); params.add((new BasicNameValuePair("iid", iid))); String state = AnswerServiceHelper.updateAnswer(uri, params); Toast.makeText(taskView.this, "提示:" + state, Toast.LENGTH_LONG).show(); } }); selfdialog.setButton2("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selfdialog.cancel(); } }); selfdialog.show(); } } return super.onContextItemSelected(item); } private static Bundle bundle; public Bundle getBundle() { bundle = getIntent().getExtras(); return bundle; } private List<Map<String, Object>> getMapData(List<Health> httpResult) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (Iterator<Health> it = httpResult.iterator(); it.hasNext();) { Map<String, Object> map = new HashMap<String, Object>(); Health health = (Health) it.next(); map.put("title", health.getName()); map.put("info", health.getConsultationexpert()); map.put("img", R.drawable.ic_launcher); list.add(map); } return list; } public List<Health> getData(int page) { String overflag = getIntent().getStringExtra("overflag"); String consultationexpert = getIntent().getStringExtra( "consultationexpert"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("overflag", overflag)); params.add(new BasicNameValuePair("consultationexpert", consultationexpert)); params.add(new BasicNameValuePair("page", String.valueOf(page))); TaskByPageHelper task = new TaskByPageHelper(); String host = UserDataServiceHelper.getHost(); String uri = host + "/task.do?task=getTask"; Bundle bundle = task.getTask(uri, params); page = Integer.valueOf(bundle.getString("page")); allpage = Integer.valueOf(bundle.getString("allpage")); List<Health> healist = (List<Health>) bundle.get("list"); return healist; } private void dealData() { List<Health> data = getData(this.page); // heallist =data; List<Map<String, Object>> a = getMapData(data); for (int i = 0, size = a.size(); i < size; i++) { httpResult.add(a.get(i)); heallist.add(data.get(i)); } adapter.notifyDataSetChanged(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (lastItem == adapter.getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE) { if (page == allpage || page > allpage) { Toast.makeText(this, "尾页", Toast.LENGTH_LONG).show(); } else { page = page + 1; progressdialog =ProgressDialog.show(taskView.this, "请等待...", "正在加载..."); refreshHandler.sleep(100); Toast.makeText(this, page + "/" + allpage + "页", Toast.LENGTH_LONG).show(); } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { lastItem = firstVisibleItem + visibleItemCount; } //加载 private RefreshHandler refreshHandler =new RefreshHandler(); //处理器 class RefreshHandler extends Handler{ @Override public void handleMessage(Message msg) { try{ dealData(); }catch(Exception e){ e.printStackTrace(); }finally{ progressdialog.dismiss();//解除进度条 } } public void sleep(long delayMillis){ this.removeMessages(0); sendMessageDelayed(obtainMessage(0), delayMillis); } } }
taskview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="15px" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="15px" /> </LinearLayout> <!-- <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbars="vertical"/> --> </LinearLayout>
后台逻辑:
package com.gk.data; import java.io.Serializable; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.util.SparseArray; import com.gk.model.Agent; import com.gk.model.Health; import com.gk.util.BundleUtil; public class TaskByPageHelper { private static HttpResponse httpresponse =null; public static Bundle getTask(String uri,List<NameValuePair> params){ HttpPost httqRequest =new HttpPost(uri); try{ httqRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); httpresponse = new DefaultHttpClient().execute(httqRequest); if(httpresponse.getStatusLine().getStatusCode() == 200){ //取出应答字符串 String strResult = EntityUtils.toString(httpresponse.getEntity()); JSONObject json =new JSONObject(strResult); String pageSize =json.getString("pageSize"); String allcount = json.getString("allcount"); String allpage = json.getString("allpage"); String page = json.getString("page"); JSONArray jsonarray = json.getJSONArray("list"); List<Health> healthlist =new ArrayList<Health>(); for(int i=0; i<jsonarray.length(); i++){ JSONObject object =(JSONObject)jsonarray.get(i); Health health =new Health(); health.setIid(object.getInt("iid")); health.setConsultationexpert(object.getString("consultationexpert")); health.setName(object.getString("name")); healthlist.add(health); } Bundle bundle =new Bundle(); bundle.putString("pageSize", pageSize); bundle.putString("allcount", allcount); bundle.putString("allpage",allpage); bundle.putString("page", page); bundle.putSerializable("list", (Serializable) healthlist); System.out.println(json.toString()); return bundle; }else{ return null; } }catch(ClientProtocolException e){ e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } }
发表评论
-
android 自定义view
2012-04-07 15:54 1670自定义view:1. 继承view,重构构造方法: publi ... -
Android 获取联系人
2012-03-22 17:18 1075Cursor cursor = content ... -
一些常用的控件及其项目实例
2011-12-16 09:49 917下载须知:进入下载页面后,可以点击普通用户进行下载,如图: ... -
listview实例 simpleAdapter自定义列
2011-12-16 09:30 1291listview 利用simpleAdapter自定义列布局, ... -
android音乐播放器开源项目实例
2011-12-16 09:17 1520android音乐播放器开源项目实例,部分效果图如下: ... -
Android QQ登陆界面实例
2011-12-16 09:01 1233QQ登陆效果: 代码下载: -
Spinner 和Menu菜单展示关于退出的用法
2011-12-15 15:40 2996这节接着上一节“自定义登陆弹出框”,我们编写一个spinne ... -
android 自定义登陆弹出框
2011-12-15 15:09 16711从本节将开始阐述一个完整系统Demo,从登陆--主页--查 ... -
Android 全屏logo 页面
2011-12-15 14:41 3534package com.gk; import andr ...
相关推荐
.NET Standard 2.1、.NET 6、.NET 7、.NET8 版本SQLBuilder,Expression表达式转换为SQL语句,支持SqlServer、MySql、Oracle、Sqlite、PostgreSql;基于Dapper实现了不同数据库对应的数据仓储Repository;
各国数字服务贸易进出口额(2010-2022年)
2024版银行及保险高净值客户健康绿皮书
【博客】MVC、MVP、MVVM设计模式的案例_pgc
Python数据抓取淘宝电商商品图片
基于python开发爬虫脚本,并使用django,echarts对数据进行分析_pgc
Tesseract繁体中文OCR数据文件,垂直布局
## 介绍 碳排放数据是衡量地方经济发展与环境可持续性的重要指标。碳排放数据通常包括二氧化碳排放量,来源包括能源消耗以及工业、交通、建筑等领域的排放。由于中国地理辽阔、经济发展不平衡,各地的碳排放水平差异较大。本次分享的数据是根据EDGAR资源提取的中国地级市二氧化碳排放数据,数据年份为2000-2023年 ## 一、数据介绍 数据名称:中国地级市二氧化碳排放数据 数据年份:2000-2023年 数据范围:300个地级以上城市 数据来源:EDGAR_2024_GHG of October 2024 数据说明:根据EDGAR提取的城市碳排放数据 ## 二、数据指标 年份、省份、城市、城市代码、所属地域、二氧化碳排放总量
基于多算法融合的齿轮箱故障诊断模型优化:GADF-CNN-BKA-LSSVM算法的原理与应用,基于GADF-CNN-BKA-LSSVM的齿轮箱故障诊断 首先,利用格拉姆角场差(GADF)时频分辨率高、可以深度反映时间序列内在结构和关系的特点,对采集到的一维故障数据信号转为二维图像,得到图像后并将图像进行降维处理;然后,将第一步得到的格拉姆角场差图像输入二维卷积神经网络(CNN)进行自适应故障特征提取;最后,取CNN的全连接层结果作为LSSVM分类器的输入,并采用黑翅鸢优化算法BKA对LSSVM分类器的超参数进行优化,以提高模型泛化能力,避免模型陷入局部最优 附赠常春藤优化算法IVY和鹈鹕优化算法POA ,基于GADF-CNN-BKA-LSSVM; 故障诊断; 图像降维; 特征提取; 模型泛化; 常春藤优化算法IVY; 鹈鹕优化算法POA,基于多算法优化的齿轮箱故障诊断:GADF-CNN-BKA-LSSVM模型
省级-创新资源错配指数(2000-2022年)
TencentMeeting_0300000000_3.30.30.420_x86_64.publish.officialwebsite.exe
基于FPGA的Endat绝对值编码器PG卡源代码优化与实现:海德汉1313编码器应用案例,基于fpga的海德汉1313 Endat绝对值编码器pg卡源代码 ,基于FPGA的海德汉1313; Endat; 绝对值编码器; PG卡; 源代码,基于FPGA的Endat绝对值编码器PG卡源代码
【Vue+PHP】基于thinkphp6、vue(iview-admin)前后端分离快速开发框架
## 1.基本信息 数据名称:ALOS 12.5m DEM 数据 空间位置:中国 数据格式:栅格 数据大小:61.5GB 空间分辨率:12.5m ## 2.内容概述 ALOS 12.5m DEM 数据,是ALOS(Advanced Land Observing Satellite. 2006年发射)卫星相控阵型L波段合成孔径雷达(PALSAR)采集的高程数据。PALSAR传感器具有高分辨率、扫描式合成孔径雷达、极化三种观测模式。ALOS Dem高程数据水平及垂直精度可达12.5米。 ALOS是日本宇宙航空研究所(JAXA)的Advanced Land Observing Satellite-1(高级陆地观测卫星-1,ALOS)项目。ALOS卫星载有三个传感器:全色遥感立体测绘仪(PRISM),主要用于数字高程测绘;先进可见光与近红外辐射计-2(AVNIR-2),用于精确陆地观测;相控阵型L波段合成孔径雷达(PALSAR),用于全天时全天候陆地观测。
基于六自由度机械臂模型的MPC预测控制方法研究,六自由度机械臂模型预测控制mpc ,六自由度机械臂;模型预测控制;MPC,六自由度机械臂的MPC预测控制模型
## 介绍 碳排放数据是衡量地方经济发展与环境可持续性的重要指标。碳排放数据通常包括二氧化碳排放量,来源包括能源消耗以及工业、交通、建筑等领域的排放。由于中国地理辽阔、经济发展不平衡,各地的碳排放水平差异较大。 本次分享的数据是根据EDGAR资源提取的中国各省、地级市、区县二氧化碳排放数据,数据年份为1980-2023年。 ## 一、数据介绍 数据名称:中国省、市、县二氧化碳排放数据 数据年份:1980-2023年 数据范围:各省、地级市、区县 数据来源:EDGAR_2024_GHG of October 2024 数据说明:根据EDGAR提取的城市碳排放数据 ## 二、数据指标 年份、省份、城市、城市代码、区县、二氧化碳排放总量 ## 三、数据展示
基于混合整数规划与次梯度法的分散PEV充电控制策略:比较与验证分析,基于混合整数规划问题次梯度法的分散PEV控制 本文提出了一种解决智能电网中充电式电动汽车(pev)车队充电问题的分散方法,其中通过MPC策略将全局和局部约束和目标与电网模型一起考虑。 充电问题的表示涉及连续变量和整数变量的组合(特别是布尔性质的)。 它被公式化为混合整数线性规划(MILP)问题,使用次梯度方法求解。 将所提出的方法与集中式方法进行比较,集中式方法通常通过分支定界算法来解决。 结果表明,分散式方法能够实现接近集中式方法的性能,同时减少计算负担,特别是对于大型车队。 这些发现通过在测试案例上执行的模拟来支持。 ,混合整数规划问题;次梯度法;分散PEV控制;MPC策略;全局和局部约束;MILP问题;计算负担;测试案例模拟,基于混合整数规划的次梯度法:分散PEV充电控制策略研究
.archivetemp活动七:小古文.doc
适用操作系统:windows。 适用CPU架构:x64。
数值分析与公式定理