导航:首页 > 操作系统 > sortedlistandroid

sortedlistandroid

发布时间:2023-07-19 17:00:59

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实现,可用显示列表的控件如下:

  1. Listview

  2. GridView

  3. ExpandListview

显示具体的数据需要通过Adapter实现,Android目前有4种Adapter:

  1. ArrayAdapter

  2. SimpleAdapter

  3. SimpleCursorAdapter

  4. BaseAdapter ( 自定义Adapter)

具体操作步骤 ( 以自定义Adapter为例):

  1. 在xml中定义Listview布局

  2. 在代码中通过ID找到Listview控件

  3. 构建Adapter对象,新建一个类继承自BaseAdapter,重写它的四个方法,具体如下代码

  4. 构造好适配器后设置Listview的adapter对象为新建的适配器,界面即可显示数据

  5. 在数据变动的地方,只需要调用adapter的notifyDataSetChanged方法即可刷新界面


  6. 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";
}
}

}

阅读全文

与sortedlistandroid相关的资料

热点内容
想自己做网站要学编程吗 浏览:595
租个服务器开个私服需要什么 浏览:272
图片换成pdf格式 浏览:661
javamidi编程 浏览:831
android60demo 浏览:65
头条算法怎么复习 浏览:512
灯光控制通道可以编程设置吗 浏览:781
webpack命令行 浏览:805
卸载云服务器操作系统 浏览:29
java文件移动文件夹 浏览:449
针织公斤足称算法 浏览:457
电脑下载的图片从文件夹重新编组 浏览:645
安卓源码编译引导失败 浏览:676
苹果怎么添加自定义app文件夹 浏览:930
橙牛app怎么找客服 浏览:307
php对象魔术方法 浏览:487
OBV能量潮幅图指标源码 浏览:915
编程15个好习惯 浏览:676
电脑u盘文件夹显示屏幕保护程序 浏览:803
我的世界服务器版本怎么下载 浏览:600