导航:首页 > 操作系统 > androidlist类

androidlist类

发布时间:2022-05-25 13:21:37

‘壹’ android怎么传一个list集合

方法1: 直接让User类继承Serializable或者Parcelable接口即可,Intent只可以传输序列化的对象<pre t="code" l="java">//User类
public class User implements Serializable{
private String name;
.
}

//直接加入intent
List<User> list = new ArrayList<User>();
Intent intent = new Intent();
intent.putExtra("list",list);
方法2: 把list集合转为字符串表示,可以使用json格式,直接用Gson框架转换即可,再到另一个activity转换回来<pre t="code" l="java">List<User> list = new ArrayList<User>();
Type type = new TypeToken<ArrayList<User>()>(){}.getType();
String json = new Gson().toJson(list,type);
intent.putExtra("list",json);

//转换回List<User>
String json = getIntent.getStringExtra("list");
Type type = new TypeToken<ArrayList<User>()>(){}.getType();
List<User> list = new Gson().fromJson(json,type);

‘贰’ android 如何正确循环删除list中的数据

当我们使用for循环删除列表中的数据的时候,会存在问题,因为ArrayList的父类AbstractList里有个modCount的字段记录着List的总数,for循环的时候如果增加或者删除了元素,(修改不会影响),此字段会变化,那么在下次for循环的时候检查到跟之前的长度不同,此时会报异常。
解决方法如下:
Iterator it=lists.iterator();
while(it.hasNext){
it.next();
if(true){
it.remove();
}
}

‘叁’ android list和map的区别

List按对象进入的顺序保存对象,不做排序或编辑操作)。Map同样对每个
元素保存一份,但这是基于"键"的,Map也有内置的排序,因而不关心元素添加的顺序。如果添加元素的顺序对你很重要,应该使用
LinkedHashSet或者LinkedHashMap.

‘肆’ android list集合中有相识查找的方法吗

list集合有个方法 contains(Object),就是用来查找集合中是否存在某个对象的,以下为JDK中的源代码:
/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}

‘伍’ android 怎么在activity之间传递List 类型的数据

Intent 传值可以传递对象,但是比较麻烦,要序列化

给你一种建议,将这个List转成JSON字符串

另外一个Activity再将这个字符串返序列即可

JSON与java对象相互转换google提供了一个非常牛X的工具Gson

用法非常简单,一行代码搞定


‘陆’ 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";
}
}

}

‘柒’ 在android中 List 和ArrayList的区别,越详细越好

List是一个接口,而ArrayList是List的一个实现类,对于android和J2SE来讲,两者之间的区别不大。

‘捌’ 如何在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<String>类型转换为String【】类型

请直接调用这个函数: static String[] convert(List<String[]> from) {
ArrayList<String> list = new ArrayList<String>();
for (String[] strings : from) {
Collections.addAll(list, strings);
}
return list.toArray(new String[list.size()]);
}
【以下是如何调用这个函数的例子】public static void main(String[] args) {
List<String[]> list = new ArrayList<String[]>();
list.add(new String[] { "one", "two" });
list.add(new String[] { "three", "four", "five" });
list.add(new String[] { "six", "seven" });
String[] converted = convert(list);
System.out.print(converted.toString());
}

‘拾’ android怎么取出list里面特定的数据

只需要循环一下,判断即可。

示例代码:
1.遍历整个list集合
for(int i=0; i<list.size(); i++){
}
2.在for循环中增加判断代码
if(list.get(i).equals("指定")){}

3.得到每一个item进行判断即可。

阅读全文

与androidlist类相关的资料

热点内容
录像免压缩 浏览:502
总结所学过的简便算法 浏览:358
南昌哪些地方需要程序员 浏览:756
三台服务器配置IP地址 浏览:173
如何用命令方块连续对话 浏览:275
win7linux共享文件夹 浏览:304
命令符打开本地服务 浏览:599
android应用程序源码 浏览:702
安卓开发工程师简历怎么写 浏览:60
热水器水量服务器是什么意思 浏览:117
stk卫星编译 浏览:480
对后台程序员的要求 浏览:761
ios大文件夹图标 浏览:626
生的计划pdf 浏览:714
oppoa93加密便签在哪查找 浏览:21
两个数字的加减乘除运算编程 浏览:227
给手机加密码忘记了怎么办 浏览:601
单片机运算符 浏览:297
移动端微信商城源码 浏览:446
编程猫下一个背景在哪里 浏览:359