當前位置:首頁 » 安卓系統 » androidbundlelist

androidbundlelist

發布時間: 2022-09-01 21:16:49

① 請問在android開發中怎樣將藍牙搜索的設備放在ListView中呢

我這邊舉個例子吧:


  1. 在布局中拖入一個ListView空間,假如id就叫listView1;

  2. 在layout文件夾中,新建一個布局xml文件,假如叫items,可以選擇RelativeLayout布局;裡面放一個TextView,假如id是textView1(該布局就是ListView每一行的布局);

  3. 下面的例子中,可以把容器中的String換成自定義的類:

  4. java">packagecom.example.test;

    importjava.util.ArrayList;

    importandroid.app.Activity;
    importandroid.os.Bundle;
    importandroid.view.Menu;
    importandroid.view.View;
    importandroid.view.ViewGroup;
    importandroid.widget.AdapterView;
    importandroid.widget.AdapterView.OnItemClickListener;
    importandroid.widget.BaseAdapter;
    importandroid.widget.ListView;
    importandroid.widget.TextView;
    importandroid.widget.Toast;

    {

    privateArrayList<String>btList=newArrayList<String>();//用於保存獲取到的藍牙名稱
    privateMyAdaptermAdapter;//適配器

    @Override
    protectedvoidonCreate(BundlesavedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initData();
    initListView();

    //若藍牙列表數據更新後,可以用以下方法通知ListView更新顯示
    mAdapter.notifyDataSetChanged();

    }

    /**
    *初始化數據
    */
    privatevoidinitData(){
    //假如在這里添加藍牙設備到ArrayList

    //假如列表中有數據,先清空
    btList.clear();

    for(inti=0;i<10;i++){
    btList.add("藍牙"+i);
    }

    }

    /**
    *初始化ListView
    */
    privatevoidinitListView(){
    ListViewlistView=(ListView)findViewById(R.id.listView1);
    //為ListView設置適配器
    mAdapter=newMyAdapter();
    listView.setAdapter(mAdapter);

    listView.setOnItemClickListener(newOnItemClickListener(){

    @Override
    publicvoidonItemClick(AdapterView<?>parent,Viewview,
    intposition,longid){
    //在這里對點擊事件進行
    //position是當前點擊的行;根據它可以獲得容器中對應的值
    StringbtName=btList.get(position);
    Toast.makeText(MainActivity.this,"當前點擊的是:"+btName,
    Toast.LENGTH_SHORT).show();
    }
    });
    }

    @Override
    (Menumenu){
    getMenuInflater().inflate(R.menu.main,menu);
    returntrue;
    }

    {

    @Override
    publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
    Viewinflate=getLayoutInflater().inflate(R.layout.item,null);
    TextViewtextView=(TextView)inflate.findViewById(R.id.textView1);//查找item中的textView
    StringbtName=btList.get(position);
    textView.setText(btName);
    returninflate;
    }

    @Override
    publicintgetCount(){
    //決定ListView的行數,這里設成容器內容數
    returnbtList.size();
    }

    @Override
    publicObjectgetItem(intposition){
    returnnull;
    }

    @Override
    publiclonggetItemId(intposition){
    return0;
    }

    }

    }

② android的ListView如何追加數據

listview的適配器adapter裡面調用additem(List)這樣的方法,如果沒有,就自己 寫一個,就是給adapter中的list追加數據而已,然後調用adapter的刷新就好了,esay

③ Android如何導入已有的外部資料庫(在raw下自己導入db文件)

操作方法是用FileInputStream讀取原資料庫,再用 FileOutputStream把讀取到的東西寫入到那個目錄。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 packagecom.android.ImportDatabase; importjava.io.File; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.InputStream; importandroid.content.Context; importandroid.database.sqlite.SQLiteDatabase; importandroid.os.Environment; importandroid.util.Log; publicclassDBManager { privatefinalintBUFFER_SIZE =400000; publicstaticfinalString DB_NAME ="countries.db";//保存的資料庫文件名 publicstaticfinalString PACKAGE_NAME ="com.android.ImportDatabase"; publicstaticfinalString DB_PATH ="/data" + Environment.getDataDirectory().getAbsolutePath() +"/" + PACKAGE_NAME; //在手機里存放資料庫的位置 privateSQLiteDatabase database; privateContext context; DBManager(Context context) { this.context = context; } publicvoidopenDatabase() { this.database =this.openDatabase(DB_PATH +"/"+ DB_NAME); } privateSQLiteDatabase openDatabase(String dbfile) { try{ if(!(newFile(dbfile).exists())) { //判斷資料庫文件是否存在,若不存在則執行導入,否則直接打開資料庫 InputStream is =this.context.getResources().openRawResource( R.raw.countries);//欲導入的資料庫 FileOutputStream fos =newFileOutputStream(dbfile); byte[] buffer =newbyte[BUFFER_SIZE]; intcount =0; while((count = is.read(buffer)) >0) { fos.write(buffer,0, count); } fos.close(); is.close(); } SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); returndb; }catch(FileNotFoundException e) { Log.e("Database","File not found"); e.printStackTrace(); }catch(IOException e) { Log.e("Database","IO exception"); e.printStackTrace(); } returnnull; } ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 packagecom.android.ImportDatabase; importjava.util.ArrayList; importandroid.app.Activity; importandroid.database.Cursor; importandroid.database.sqlite.SQLiteDatabase; importandroid.os.Bundle; { privateSQLiteDatabase database; ArrayList<CityClass> CITY; @Override publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH +"/"+ DBManager.DB_NAME,null); CITY = getCity(); // do something with CITY database.close(); } privateArrayList<CityClass> getCity() { Cursor cur = database.rawQuery("SELECT city.id_city, city.name FROM taxi, city WHERE city.id_city = taxi.id_city GROUP BY city.id_city",null); if(cur !=null) { intNUM_CITY = cur.getCount(); ArrayList<CityClass> taxicity =newArrayList<CityClass>(NUM_CITY); if(cur.moveToFirst()) { do{ String name = cur.getString(cur.getColumnIndex("name")); intid = cur.getInt(cur.getColumnIndex("id_city")); CityClass city =newCityClass("",0); System.out.println(name); //額外添加一句,把select到的信息輸出到Logcat city.city_name = name; city.city_id = id; taxicity.add(city); }while(cur.moveToNext()); } returntaxicity; }else{ returnnull; } } } 查看輸出的結果:

④ Android中如何在兩界面間傳遞下面的集合

intent啊 intent是android的運輸大隊長

Intent newIntent = new Intent();
newIntent.setClass(this, another.class);
List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

Bundle bundle = new Bundle();
bundle.putSerializable(」data「, listData );
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

在another類中的oncreate()方法取
List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
List<Map<String, Object>> listData= (List) bundle.getSerializable(」data「);
}
大概是這個樣子,祝你順利

⑤ android list怎麼緩存

要想優化ListView首先要了解它的工作原理,列表的顯示需要三個元素:ListView、Adapter、顯示的數據;

這里的Adapter就是用到了適配器模式,不管傳入的是什麼View在ListView中都能顯示出來。

下面簡單說下上圖的原理:

1、如果你有幾千幾萬甚至更多的選項(item)時,其中只有可見的項目(滿屏顯示的Item數目)存在內存(說的優化就是說在內存中的優化!)中,其他的在Recycler中

2、ListView先請求一個type1視圖(getView)然後請求其他可見的項目。convertView在getView中是空(null)的,第一次都是為空的,只要顯示過了convertView都不為空,會保存在Recycler中

3、當item1滾出屏幕,並且一個新的項目從屏幕低端上來時,ListView再請求一個type1視圖。convertView此時不是空值了,它的值是item1。你只需設定新的數據然後返回convertView,不必重新創建一個視圖,省去了inflate和findViewById的時間,性能就得到了優化。

AneData全能文件恢復,超強數據恢復工具 硬碟移動硬碟SD卡U盤相機手機內存卡全支持

查 看

了解了它的工作原理後,我們就可以重復利用convertView,只要不為空就直接使用,改變它的內容就行了。

使用ListView的時候都會搭配一個Adapter,為了使得性能更優,ListView會緩存行item(某行對應的View)。ListView通過Adapter的getView函數獲得每行的item。

更多問題解決辦法請參考android學習手冊,例子、源碼、文檔全部搞定,採用androidstudo的目錄結構,360手機助手中下載。

packagecom.dzt.listviewdemo;

importjava.util.ArrayList;

importandroid.app.Activity;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.ImageView;
importandroid.widget.ListView;
importandroid.widget.TextView;

{

privateListAdapteradapter;
privateListViewlv=null;
privateArrayList<string>list=newArrayList<string>();

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv_list);
adapter=newListAdapter();
for(inti=0;i<100;i++){
list.add(item+i);
}
lv.setAdapter(adapter);
}

{

;

ListAdapter(){
mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
publicintgetCount(){
//TODOAuto-generatedmethodstub
returnlist.size();
}

@Override
publicObjectgetItem(intposition){
//TODOAuto-generatedmethodstub
returnlist.get(position);
}

@Override
publiclonggetItemId(intposition){
//TODOAuto-generatedmethodstub
returnposition;
}

@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
//TODOAuto-generatedmethodstub
System.out.println(getView+position++convertView);
viewHolderholder=null;
if(convertView==null){
convertView=mInflater.inflate(R.layout.item,null);
holder=newviewHolder();
holder.text=(TextView)convertView.findViewById(R.id.tv_text);
holder.image=(ImageView)convertView
.findViewById(R.id.iv_img);
convertView.setTag(holder);
}else{
holder=(viewHolder)convertView.getTag();
}
holder.text.setText(list.get(position));
if(position%2==0){
holder.image.setImageResource(R.drawable.ic_launcher);
}else{
holder.image.setImageResource(R.drawable.icon);
}

returnconvertView;
}

}

/**
*使用一個類來保存Item中的元素
*
*@authorAdministrator
*
*/
publicstaticclassviewHolder{
publicTextViewtext;
publicImageViewimage;
}
}


⑥ android中bundle怎麼傳遞泛型

一定要,拿常見的泛型方法舉例 List算是最常見的。 自定義類 1234567891011class testObject (){ public string TestName; public int TestIntValue;}//聲明一個泛型類List<testObject > mList = new List<testObject >(); mList.Add(new testOnject(){TestName="somename" , TestIntValue = 1} );//調用泛型方法 testObject[] mArray = mList.ToArray<testObject[]>(); 另外 Linq中 也大量需要這種利用<> 給出泛型具體目標類型的語法。

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

}

⑧ 安卓bundle報錯list 實體類已經實現了介面Serializable 還是報錯

activity2getAA沒值

熱點內容
怎麼算伺服器ip 發布:2025-01-12 08:59:19 瀏覽:854
安卓與ios哪個適合做主力機 發布:2025-01-12 08:54:11 瀏覽:340
微軟怎麼關閉配置更新 發布:2025-01-12 08:34:23 瀏覽:316
wifi的有限的訪問許可權 發布:2025-01-12 08:34:14 瀏覽:609
cftp文件重命名 發布:2025-01-12 08:33:27 瀏覽:881
https的加密演算法 發布:2025-01-12 08:19:15 瀏覽:654
資料庫交 發布:2025-01-12 08:09:06 瀏覽:472
一台剪輯電腦要什麼配置 發布:2025-01-12 07:50:16 瀏覽:12
android與java 發布:2025-01-12 07:50:12 瀏覽:498
列印機手機連接密碼是什麼 發布:2025-01-12 07:48:31 瀏覽:586