androidbundlelist
① 请问在android开发中怎样将蓝牙搜索的设备放在ListView中呢
我这边举个例子吧:
在布局中拖入一个ListView空间,假如id就叫listView1;
在layout文件夹中,新建一个布局xml文件,假如叫items,可以选择RelativeLayout布局;里面放一个TextView,假如id是textView1(该布局就是ListView每一行的布局);
下面的例子中,可以把容器中的String换成自定义的类:
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没值