listview 滑动分页 contextmenu菜单用法

来源:互联网 发布:淘宝装修做满页面 编辑:程序博客网 时间:2024/04/30 16:51
 

自定义listview项:

 

contextmenu,长按选项唤出:

选择详情菜单:


选择编辑菜单:



 滑动分页,当大滚动到最好一项时,自动加载下一页:
 

 
 代码:

Java代码 复制代码
  1. package com.gk.view;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.HashMap;   
  5. import java.util.Iterator;   
  6. import java.util.List;   
  7. import java.util.Map;   
  8.   
  9. import org.apache.http.NameValuePair;   
  10. import org.apache.http.message.BasicNameValuePair;   
  11.   
  12. import android.app.Activity;   
  13. import android.app.AlertDialog;   
  14. import android.app.ProgressDialog;   
  15. import android.content.DialogInterface;   
  16. import android.content.DialogInterface.OnClickListener;   
  17. import android.os.Bundle;   
  18. import android.os.Handler;   
  19. import android.os.Message;   
  20. import android.view.ContextMenu;   
  21. import android.view.ContextMenu.ContextMenuInfo;   
  22. import android.view.LayoutInflater;   
  23. import android.view.MenuItem;   
  24. import android.view.View;   
  25. import android.view.View.OnCreateContextMenuListener;   
  26. import android.widget.AbsListView;   
  27. import android.widget.AbsListView.OnScrollListener;   
  28. import android.widget.AdapterView.AdapterContextMenuInfo;   
  29. import android.widget.ListView;   
  30. import android.widget.ProgressBar;   
  31. import android.widget.SimpleAdapter;   
  32. import android.widget.TextView;   
  33. import android.widget.Toast;   
  34.   
  35. import com.gk.R;   
  36. import com.gk.data.AnswerServiceHelper;   
  37. import com.gk.data.TaskByPageHelper;   
  38. import com.gk.data.UserDataServiceHelper;   
  39. import com.gk.model.Health;   
  40.   
  41. public class taskView extends Activity implements OnCreateContextMenuListener,   
  42.   OnScrollListener {   
  43.   
  44.  private View view;   
  45.  private ListView listview;   
  46.  private TextView cardid;   
  47.  private TextView recorder;   
  48.  private TextView consultationtype;   
  49.  private TextView consultationexpert;   
  50.  private TextView consultationcontent;   
  51.  private TextView answer;   
  52.  private TextView answer1;   
  53.  private TextView clientname;   
  54.  private AlertDialog selfdialog;   
  55.  private Health health;   
  56.   
  57.  private ProgressBar progressBar;   
  58.  private ProgressDialog progressdialog;   
  59.  private int lastItem = 0;   
  60.  private SimpleAdapter adapter;   
  61.  private int page = 1;   
  62.  private int allpage = 1;   
  63.  private List<Health> heallist = new ArrayList<Health>();   
  64.  private List<Map<String, Object>> httpResult = new ArrayList<Map<String, Object>>();   
  65.   
  66.  @Override  
  67.  protected void onCreate(Bundle savedInstanceState) {   
  68.   super.onCreate(savedInstanceState);   
  69.   // LayoutInflater inflater = (LayoutInflater)  
  70.   // getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
  71.   // view = inflater.inflate(R.layout.taskview, null);  
  72.   adapter = new SimpleAdapter(this, httpResult, R.layout.taskview,   
  73.     new String[] { "title""info""img" }, new int[] {   
  74.       R.id.title, R.id.info, R.id.img });   
  75.   listview = new ListView(this);   
  76.   listview.setAdapter(adapter);   
  77.   listview.setOnCreateContextMenuListener(this);   
  78.   listview.setOnScrollListener(this);   
  79.   setContentView(listview);   
  80.   dealData();   
  81.  }   
  82.   
  83.  @Override  
  84.  public void onCreateContextMenu(ContextMenu menu, View v,   
  85.    ContextMenuInfo menuInfo) {   
  86.   menu.setHeaderTitle("详情菜单");   
  87.   menu.add(001"查看详情");   
  88.   menu.add(012"编辑解答");   
  89.   menu.add(023"取消查看");   
  90.  }   
  91.   
  92.  @Override  
  93.  public boolean onContextItemSelected(MenuItem item) {   
  94.   AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item   
  95.     .getMenuInfo();   
  96.   health = heallist.get(menuinfo.position);   
  97.   List<NameValuePair> params = new ArrayList<NameValuePair>();   
  98.   params.add(new BasicNameValuePair("iid", health.getIid().toString()));   
  99.   String host = UserDataServiceHelper.getHost();   
  100.   String uri = host + "/task.do?task=getHealthById";   
  101.   Bundle bundle = UserDataServiceHelper.getObjectByid(uri, params);   
  102.   
  103.   if (item.getItemId() == 0) {   
  104.    if (bundle != null) {   
  105.     health = (Health) bundle.get("health");   
  106.   
  107.     // 创建对话框   
  108.     LayoutInflater inflater = (LayoutInflater) getApplicationContext()   
  109.       .getSystemService(LAYOUT_INFLATER_SERVICE);   
  110.     view = inflater.inflate(R.layout.detail, null);   
  111.     // 设置   
  112.     cardid = (TextView) view.findViewById(R.id.cardid);   
  113.     recorder = (TextView) view.findViewById(R.id.recorder);   
  114.     consultationtype = (TextView) view   
  115.       .findViewById(R.id.consultationtype);   
  116.     consultationexpert = (TextView) view   
  117.       .findViewById(R.id.consultationexpert);   
  118.     consultationcontent = (TextView) view   
  119.       .findViewById(R.id.consultationcontent);   
  120.     answer = (TextView) view.findViewById(R.id.answer);   
  121.     clientname = (TextView) view.findViewById(R.id.clientname);   
  122.   
  123.     cardid.setText(health.getCardid());   
  124.     recorder.setText(health.getRecorder());   
  125.     consultationtype.setText(health.getConsultationtype());   
  126.     consultationexpert.setText(health.getConsultationexpert());   
  127.     consultationcontent.setText(health.getConsultationcontent());   
  128.     answer.setText(health.getAnswer());   
  129.     clientname.setText(health.getName());   
  130.   
  131.     AlertDialog.Builder ad = new AlertDialog.Builder(taskView.this);   
  132.     ad.setView(view);   
  133.     ad.setTitle("详情").create().show();   
  134.     Toast.makeText(this, page + "/" + allpage + "页",   
  135.       Toast.LENGTH_LONG).show();   
  136.    }   
  137.   
  138.   }   
  139.   
  140.   if (item.getItemId() == 1) {   
  141.    if (bundle != null) {   
  142.     health = (Health) bundle.get("health");   
  143.   
  144.     // 创建对话框   
  145.     LayoutInflater inflater = (LayoutInflater) getApplicationContext()   
  146.       .getSystemService(LAYOUT_INFLATER_SERVICE);   
  147.     view = inflater.inflate(R.layout.detail, null);   
  148.     // 设置   
  149.     cardid = (TextView) view.findViewById(R.id.cardid);   
  150.     recorder = (TextView) view.findViewById(R.id.recorder);   
  151.     consultationtype = (TextView) view   
  152.       .findViewById(R.id.consultationtype);   
  153.     consultationexpert = (TextView) view   
  154.       .findViewById(R.id.consultationexpert);   
  155.     consultationcontent = (TextView) view   
  156.       .findViewById(R.id.consultationcontent);   
  157.     answer = (TextView) view.findViewById(R.id.answer);   
  158.     answer.setVisibility(View.INVISIBLE);// 设置不可见  
  159.     answer1 = (TextView) view.findViewById(R.id.answer1);   
  160.     answer1.setVisibility(View.VISIBLE);// 设置可见  
  161.     clientname = (TextView) view.findViewById(R.id.clientname);   
  162.   
  163.     cardid.setText(health.getCardid());   
  164.     recorder.setText(health.getRecorder());   
  165.     consultationtype.setText(health.getConsultationtype());   
  166.     consultationexpert.setText(health.getConsultationexpert());   
  167.     consultationcontent.setText(health.getConsultationcontent());   
  168.     answer1.setText(health.getAnswer());   
  169.     clientname.setText(health.getName());   
  170.   
  171.     AlertDialog.Builder ad = new AlertDialog.Builder(taskView.this);   
  172.     ad.setView(view);   
  173.     ad.setTitle("解答");   
  174.     selfdialog = ad.create();   
  175.   
  176.     selfdialog.setButton("提交"new OnClickListener() {   
  177.      @Override  
  178.      public void onClick(DialogInterface dialog, int which) {   
  179.       String host = UserDataServiceHelper.getHost();   
  180.       String uri = host + "/task.do?task=updateAnswer";   
  181.       String answer = answer1.getText().toString();   
  182.       String iid = health.getIid().toString();   
  183.       List<NameValuePair> params = new ArrayList<NameValuePair>();   
  184.       params.add((new BasicNameValuePair("answer", answer)));   
  185.       params.add((new BasicNameValuePair("iid", iid)));   
  186.   
  187.       String state = AnswerServiceHelper.updateAnswer(uri,   
  188.         params);   
  189.   
  190.       Toast.makeText(taskView.this"提示:" + state,   
  191.         Toast.LENGTH_LONG).show();   
  192.      }   
  193.     });   
  194.     selfdialog.setButton2("取消"new OnClickListener() {   
  195.   
  196.      @Override  
  197.      public void onClick(DialogInterface dialog, int which) {   
  198.       selfdialog.cancel();   
  199.      }   
  200.     });   
  201.     selfdialog.show();   
  202.    }   
  203.   
  204.   }   
  205.   return super.onContextItemSelected(item);   
  206.  }   
  207.   
  208.  private static Bundle bundle;   
  209.   
  210.  public Bundle getBundle() {   
  211.   bundle = getIntent().getExtras();   
  212.   return bundle;   
  213.  }   
  214.   
  215.  private List<Map<String, Object>> getMapData(List<Health> httpResult) {   
  216.   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();   
  217.   for (Iterator<Health> it = httpResult.iterator(); it.hasNext();) {   
  218.    Map<String, Object> map = new HashMap<String, Object>();   
  219.    Health health = (Health) it.next();   
  220.    map.put("title", health.getName());   
  221.    map.put("info", health.getConsultationexpert());   
  222.    map.put("img", R.drawable.ic_launcher);   
  223.    list.add(map);   
  224.   }   
  225.   return list;   
  226.  }   
  227.   
  228.  public List<Health> getData(int page) {   
  229.   String overflag = getIntent().getStringExtra("overflag");   
  230.   String consultationexpert = getIntent().getStringExtra(   
  231.     "consultationexpert");   
  232.   List<NameValuePair> params = new ArrayList<NameValuePair>();   
  233.   params.add(new BasicNameValuePair("overflag", overflag));   
  234.   params.add(new BasicNameValuePair("consultationexpert",   
  235.     consultationexpert));   
  236.   params.add(new BasicNameValuePair("page", String.valueOf(page)));   
  237.   TaskByPageHelper task = new TaskByPageHelper();   
  238.   String host = UserDataServiceHelper.getHost();   
  239.   String uri = host + "/task.do?task=getTask";   
  240.   Bundle bundle = task.getTask(uri, params);   
  241.   page = Integer.valueOf(bundle.getString("page"));   
  242.   allpage = Integer.valueOf(bundle.getString("allpage"));   
  243.   List<Health> healist = (List<Health>) bundle.get("list");   
  244.   return healist;   
  245.  }   
  246.   
  247.  private void dealData() {   
  248.   List<Health> data = getData(this.page);   
  249.   // heallist =data;   
  250.   List<Map<String, Object>> a = getMapData(data);   
  251.   for (int i = 0, size = a.size(); i < size; i++) {   
  252.    httpResult.add(a.get(i));   
  253.    heallist.add(data.get(i));   
  254.   }   
  255.   adapter.notifyDataSetChanged();   
  256.  }   
  257.   
  258.  @Override  
  259.  public void onScrollStateChanged(AbsListView view, int scrollState) {   
  260.   if (lastItem == adapter.getCount()   
  261.     && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {   
  262.    if (page == allpage || page > allpage) {   
  263.     Toast.makeText(this"尾页", Toast.LENGTH_LONG).show();   
  264.    } else {   
  265.     page = page + 1;   
  266.     progressdialog =ProgressDialog.show(taskView.this"请等待...""正在加载...");   
  267.     refreshHandler.sleep(100);   
  268.     Toast.makeText(this, page + "/" + allpage + "页",   
  269.       Toast.LENGTH_LONG).show();   
  270.    }   
  271.   }   
  272.  }   
  273.   
  274.  @Override  
  275.  public void onScroll(AbsListView view, int firstVisibleItem,   
  276.    int visibleItemCount, int totalItemCount) {   
  277.   lastItem = firstVisibleItem + visibleItemCount;   
  278.  }   
  279.     
  280.  //加载   
  281.  private RefreshHandler refreshHandler =new RefreshHandler();   
  282.     
  283.  //处理器   
  284.  class RefreshHandler extends Handler{   
  285.   
  286.   @Override  
  287.   public void handleMessage(Message msg) {   
  288.    try{   
  289.     dealData();   
  290.    }catch(Exception e){   
  291.     e.printStackTrace();   
  292.    }finally{   
  293.     progressdialog.dismiss();//解除进度条   
  294.    }   
  295.   }   
  296.      
  297.   public void sleep(long delayMillis){   
  298.    this.removeMessages(0);   
  299.    sendMessageDelayed(obtainMessage(0), delayMillis);   
  300.   }   
  301.  }   
  302. }  

 

taskview.xml

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="horizontal" android:layout_width="fill_parent"  
  4.  android:layout_height="fill_parent" >   
  5.   
  6.   
  7.  <ImageView android:id="@+id/img"    
  8.   android:layout_width="wrap_content"  
  9.   android:layout_height="wrap_content"    
  10.   android:layout_margin="5px"/>   
  11.   
  12.  <LinearLayout android:orientation="vertical"  
  13.   android:layout_width="wrap_content"    
  14.   android:layout_height="wrap_content">   
  15.   
  16.   <TextView android:id="@+id/title"    
  17.    android:layout_width="wrap_content"  
  18.    android:layout_height="wrap_content"    
  19.    android:textColor="#FFFFFFFF"  
  20.    android:textSize="15px" />   
  21.   <TextView android:id="@+id/info"    
  22.    android:layout_width="wrap_content"  
  23.    android:layout_height="wrap_content"    
  24.    android:textColor="#FFFFFFFF"  
  25.    android:textSize="15px" />   
  26.   
  27.  </LinearLayout>   
  28. <!--  <ListView   
  29.      android:id="@id/android:list"  
  30.      android:layout_width="fill_parent"  
  31.      android:layout_height="wrap_content"  
  32.      android:drawSelectorOnTop="false"  
  33.      android:scrollbars="vertical"/> -->   
  34.   
  35. </LinearLayout>  

  

后台逻辑:

Java代码 复制代码
  1. package com.gk.data;   
  2.   
  3. import java.io.Serializable;   
  4. import java.sql.Timestamp;   
  5. import java.util.ArrayList;   
  6. import java.util.List;   
  7.   
  8. import org.apache.http.HttpResponse;   
  9. import org.apache.http.NameValuePair;   
  10. import org.apache.http.client.ClientProtocolException;   
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;   
  12. import org.apache.http.client.methods.HttpPost;   
  13. import org.apache.http.impl.client.DefaultHttpClient;   
  14. import org.apache.http.message.BasicNameValuePair;   
  15. import org.apache.http.protocol.HTTP;   
  16. import org.apache.http.util.EntityUtils;   
  17. import org.json.JSONArray;   
  18. import org.json.JSONObject;   
  19.   
  20. import android.content.Intent;   
  21. import android.os.Bundle;   
  22. import android.os.Parcelable;   
  23. import android.util.SparseArray;   
  24.   
  25. import com.gk.model.Agent;   
  26. import com.gk.model.Health;   
  27. import com.gk.util.BundleUtil;   
  28. public class TaskByPageHelper {   
  29.   
  30.   
  31.  private static HttpResponse httpresponse =null;   
  32.     
  33.  public static Bundle getTask(String uri,List<NameValuePair> params){   
  34.   HttpPost httqRequest =new HttpPost(uri);   
  35.   try{   
  36.    httqRequest.setEntity(new  UrlEncodedFormEntity(params,HTTP.UTF_8));   
  37.    httpresponse = new DefaultHttpClient().execute(httqRequest);   
  38.    if(httpresponse.getStatusLine().getStatusCode() == 200){   
  39.     //取出应答字符串   
  40.    String strResult = EntityUtils.toString(httpresponse.getEntity());   
  41.    JSONObject json =new JSONObject(strResult);   
  42.    String pageSize =json.getString("pageSize");   
  43.    String allcount = json.getString("allcount");   
  44.    String allpage = json.getString("allpage");   
  45.    String page = json.getString("page");   
  46.       
  47.    JSONArray jsonarray = json.getJSONArray("list");   
  48.    List<Health> healthlist =new ArrayList<Health>();   
  49.    for(int i=0; i<jsonarray.length(); i++){   
  50.     JSONObject object =(JSONObject)jsonarray.get(i);   
  51.     Health health =new Health();   
  52.     health.setIid(object.getInt("iid"));   
  53.     health.setConsultationexpert(object.getString("consultationexpert"));   
  54.     health.setName(object.getString("name"));   
  55.     healthlist.add(health);   
  56.    }   
  57.       
  58.    Bundle bundle =new Bundle();   
  59.    bundle.putString("pageSize", pageSize);   
  60.    bundle.putString("allcount", allcount);   
  61.    bundle.putString("allpage",allpage);   
  62.    bundle.putString("page", page);   
  63.    bundle.putSerializable("list", (Serializable) healthlist);   
  64.       
  65.    System.out.println(json.toString());   
  66.    return bundle;   
  67.   }else{   
  68.    return null;   
  69.   }   
  70.   }catch(ClientProtocolException e){   
  71.    e.printStackTrace();   
  72.   } catch (Exception e) {   
  73.    e.printStackTrace();   
  74.   }   
  75.   return null;   
  76.  }   
  77.     
  78. }  
原创粉丝点击