『壹』 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實現,可用顯示列表的控制項如下:
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<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進行判斷即可。