① 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";
}
}
}