① android中怎麼把list中內容按a-z排序
PinyinComparator介面用來對ListView中的數據根據A-Z進行排序,前面兩個if判斷主要是將不是以漢字開頭的數據放在後面
package com.example.sortlistview;
import java.util.Comparator;
/**
*
* @author xiaanming
*
*/
public class PinyinComparator implements Comparator<SortModel> {
public int compare(SortModel o1, SortModel o2) {
//這里主要是用來對ListView裡面的數據根據ABCDEFG...來排序
if (o2.getSortLetters().equals("#")) {
return -1;
} else if (o1.getSortLetters().equals("#")) {
return 1;
} else {
return o1.getSortLetters().compareTo(o2.getSortLetters());
}
}
}
② 如何在Android開發中動態載入的list列表數據
Android中載入list列表數據主要是通過Adapter實現,可用顯示列表的控制項如下:
Listview
GridView
ExpandListview
顯示具體的數據需要通過Adapter實現,Android目前有4種Adapter:
ArrayAdapter
SimpleAdapter
SimpleCursorAdapter
BaseAdapter ( 自定義Adapter)
具體操作步驟 ( 以自定義Adapter為例):
在xml中定義Listview布局
在代碼中通過ID找到Listview控制項
構建Adapter對象,新建一個類繼承自BaseAdapter,重寫它的四個方法,具體如下代碼
構造好適配器後設置Listview的adapter對象為新建的適配器,界面即可顯示數據
在數據變動的地方,只需要調用adapter的notifyDataSetChanged方法即可刷新界面
packagecom.beryl.gougou;
importandroid.content.Context;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importjava.util.List;
/**
*Createdbyyton16/11/14.
*/
{
privateList<String>datalist;
privateLayoutInflaterinflater;
publicMyAdapter(Contextcontext,List<String>datalist){
this.datalist=datalist;
inflater=LayoutInflater.from(context);
}
@Override
publicintgetCount(){
returndatalist.size();
}
@Override
publicObjectgetItem(intposition){
returndatalist.get(position);
}
@Override
publiclonggetItemId(intposition){
returnposition;
}
@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
//此處參考網上的view緩存機制,示例demo不多說明
returnnull;
}
}
③ android 怎麼取list數據
按以下代碼可獲取到list數據:
package com.example.sdtg.sdsw;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ListActivity extends Activity {
// 命名空間
// String nameSpace = "http://tempuri.org/";
// 調用的方法名稱
// String methodName = "GetSjSearch";
// EndPoint
// String endPoint = "http://192.168.0.145/webservice2/gswebservice.asmx";
// SOAP Action
// String soapAction = "http://tempuri.org/GetSjSearch";
// List<Map<String, Object>> mList;
ListView ListV;
HashMap<String, Object> map = new HashMap<String, Object>();
private List<Map<String,String>> listItems;
SimpleAdapter mListAdapter;
String name="";
String addr="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listItems = new ArrayList<Map<String,String>>();
ListV=(ListView)findViewById(R.id.ListView01);
Handler indicate = new Handler();
//獲取主頁面傳的值
final Intent data = getIntent();
name=data.getStringExtra("Name");
addr=data.getStringExtra("Addr");
new NetAsyncTask().execute();
ListV.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//獲得選中項的HashMap對象
TextView viewtmc = (TextView) arg1.findViewById(R.id.textListmc);
TextView viewdz = (TextView) arg1.findViewById(R.id.textlistdz);
TextView viewid = (TextView) arg1.findViewById(R.id.textlistid);
//String playerChanged = c.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Name", viewtmc.getText().toString());
bundle.putString("Addr", viewdz.getText().toString());
bundle.putString("ID", viewid.getText().toString());
final Intent data = getIntent();
data.putExtras(bundle);
//跳轉回MainActivity
//注意下面的RESULT_OK常量要與回傳接收的Activity中onActivityResult()方法一致
ListActivity.this.setResult(RESULT_OK, data);
//關閉當前activity
ListActivity.this.finish();
}
});
};
class NetAsyncTask extends AsyncTask<Object, Object, String> {
@Override
protected void onPostExecute(String result) {
if (result.equals("success")) {
mListAdapter = null;
mListAdapter = new SimpleAdapter(ListActivity.this, listItems, R.layout.item,new String[]{"title", "info", "img"}, new int[]{R.id.textListmc, R.id.textlistdz, R.id.textlistid});
ListV.setAdapter(mListAdapter);
}
super.onPostExecute(result);
}
@Override
protected String doInBackground(Object... params) {
// 命名空間
String nameSpace = "http://tempuri.org/";
// 調用的方法名稱
String methodName = "GetSjSearch";
// EndPoint
String endPoint = "http://192.168.0.145/webservice2/gswebservice.asmx";
// SOAP Action
String soapAction = "http://tempuri.org/GetSjSearch";
// 指定WebService的命名空間和調用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 設置需調用WebService介面需要傳入的兩個參數mobileCode、userId
rpc.addProperty("name", name);
rpc.addProperty("address", addr);
// 生成調用WebService方法的SOAP請求信息,並指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut = rpc;
// 設置是否調用的是dotNet開發的WebService
envelope.dotNet = true;
// 等價於envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 調用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
SoapObject object;
// 開始調用遠程方法
try {
object = (SoapObject) envelope.getResponse();
int count = object.getPropertyCount();
// 得到伺服器傳回的數據
int count1 = object.getPropertyCount();
if(count1>0)
{
for (int i = 0; i < count1; i++) {
Map<String,String> listItem = new HashMap<String, String>();
SoapObject soapProvince = (SoapObject)object.getProperty(i);
listItem.put("title", soapProvince.getProperty("DJXX_NSRMC").toString());
listItem.put("info", soapProvince.getProperty("DJXX_ZCDJ").toString());
//listItem.put("img", soapProvince.getProperty("DJXX_NSRSBH").toString());
listItems.add(listItem);
}}
} catch (IOException e) {
e.printStackTrace();
//return "IOException";
}
return "success";
}
}
}