在做一个公交查询的应用,新建项目是设定的1.5的模拟器,后来要用Google Map,所以升级到了2.2,但是用AutoCompleteTextView显示查询车次信息时,在1.5的模拟器上可以实现的提示输入功能在升级2.2版本上之后,下拉列表项莫名其妙的少了几条,百思不得其解,求各位指点,可惜不能上截图,下面是我的代码
public class Main extends ListActivity
{
private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory()
.getAbsolutePath();
private final String DATABASE_FILENAME = "businfo.db";
private SQLiteDatabase database;
private View dialogView;
private Cursor cursor1;
private View dialogSDView;
private AutoCompleteTextView actvKeyWord;
private AutoCompleteTextView actvStartStop;
private AutoCompleteTextView actvDestination;
private static String[] items = new String[]
{"地图浏览","定位当前位置","查找定位","车次查询","站点查询","站站查询","换乘查询"};
private Intent busNumIntent;
private Intent busStopIntent;
private Intent busStartEndIntent;
@Override
protected void onListItemClick(ListView l, View view, int position, long id)
{
try
{
switch(position)
{
case 3:
String sql1 = "select BUSNUM from businfo";
cursor1 = database.rawQuery(sql1, null);
String result1="";
String[] autoString1 = new String[]{};
dialogView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.inputdialog, null);
actvKeyWord = (AutoCompleteTextView) dialogView.findViewById(R.id.actvKeyWord);
if(cursor1.getCount()>0)
{
Log.d("Message", String.valueOf(cursor1.getCount()));
cursor1.moveToFirst();
for(int i=0;i<cursor1.getCount();i++)
{
result1 = result1 + cursor1.getString(cursor1.getColumnIndex("BUSNUM"))+",";
cursor1.moveToNext();
}
autoString1 = result1.split(",");
Toast.makeText(this, autoString1[0]+autoString1[1]+autoString1[2]+autoString1[3]+autoString1[4]+autoString1[5], Toast.LENGTH_LONG).show();
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line
,autoString1);
actvKeyWord.setHint("例如:12路");
actvKeyWord.setAdapter(adapter1);
busNumIntent = new Intent(this,BusNumInfo.class);
new AlertDialog.Builder(this).setTitle("提示").setMessage("请输入查询的车次")
.setView(dialogView).setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
busNumIntent.putExtra("busnumber", actvKeyWord.getText().toString().trim());
startActivity(busNumIntent);
}
})
.setNegativeButton("取消", null);
break;
default:
break;
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
@Override
protected void onRestart() {
Log.d("message", "Main is Restart,DB is Started");
database = openDatabase();
super.onRestart();
}
@Override
protected void onStop() {
Log.d("message", "Main is Stop,Cursor is Closed,DB is Closed");
if(cursor1!=null)
{
cursor1.close();
}
database.close();
super.onStop();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
database = openDatabase();
List<Map<String,Object>> appItems = new ArrayList<Map<String,Object>>();
for(int i=0;i<items.length;i++)
{
Map<String,Object> appItem = new HashMap<String,Object>();
appItem.put("Logo", R.drawable.bus1);
appItem.put("ItemName", items[i]);
appItems.add(appItem);
}
SimpleAdapter itemAdapter = new SimpleAdapter(this,appItems,R.layout.main,
new String[]{"ItemName","Logo"},
new int[]{R.id.tvMainItem,R.id.ivLogo});
setListAdapter(itemAdapter);
}
private SQLiteDatabase openDatabase()
{
try
{
String databaseFilename = DATABASE_PATH+"/"+DATABASE_FILENAME;
Log.d("path110", databaseFilename);
if(!(new File(databaseFilename).exists()))
{
InputStream is = getResources().openRawResource(R.raw.businfo);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while((count = is.read(buffer))>0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
return database;
}
catch(Exception e)
{
Log.d("message110", e.toString());
}
return null;
}
}
我测试后发现,我的cursor只返回了6条记录,而我数据库中有11条记录,为什么升级模拟器,数据查询会丢失数据呢?
public class Main extends ListActivity
{
private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory()
.getAbsolutePath();
private final String DATABASE_FILENAME = "businfo.db";
private SQLiteDatabase database;
private View dialogView;
private Cursor cursor1;
private View dialogSDView;
private AutoCompleteTextView actvKeyWord;
private AutoCompleteTextView actvStartStop;
private AutoCompleteTextView actvDestination;
private static String[] items = new String[]
{"地图浏览","定位当前位置","查找定位","车次查询","站点查询","站站查询","换乘查询"};
private Intent busNumIntent;
private Intent busStopIntent;
private Intent busStartEndIntent;
@Override
protected void onListItemClick(ListView l, View view, int position, long id)
{
try
{
switch(position)
{
case 3:
String sql1 = "select BUSNUM from businfo";
cursor1 = database.rawQuery(sql1, null);
String result1="";
String[] autoString1 = new String[]{};
dialogView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.inputdialog, null);
actvKeyWord = (AutoCompleteTextView) dialogView.findViewById(R.id.actvKeyWord);
if(cursor1.getCount()>0)
{
Log.d("Message", String.valueOf(cursor1.getCount()));
cursor1.moveToFirst();
for(int i=0;i<cursor1.getCount();i++)
{
result1 = result1 + cursor1.getString(cursor1.getColumnIndex("BUSNUM"))+",";
cursor1.moveToNext();
}
autoString1 = result1.split(",");
Toast.makeText(this, autoString1[0]+autoString1[1]+autoString1[2]+autoString1[3]+autoString1[4]+autoString1[5], Toast.LENGTH_LONG).show();
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line
,autoString1);
actvKeyWord.setHint("例如:12路");
actvKeyWord.setAdapter(adapter1);
busNumIntent = new Intent(this,BusNumInfo.class);
new AlertDialog.Builder(this).setTitle("提示").setMessage("请输入查询的车次")
.setView(dialogView).setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
busNumIntent.putExtra("busnumber", actvKeyWord.getText().toString().trim());
startActivity(busNumIntent);
}
})
.setNegativeButton("取消", null);
break;
default:
break;
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
@Override
protected void onRestart() {
Log.d("message", "Main is Restart,DB is Started");
database = openDatabase();
super.onRestart();
}
@Override
protected void onStop() {
Log.d("message", "Main is Stop,Cursor is Closed,DB is Closed");
if(cursor1!=null)
{
cursor1.close();
}
database.close();
super.onStop();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
database = openDatabase();
List<Map<String,Object>> appItems = new ArrayList<Map<String,Object>>();
for(int i=0;i<items.length;i++)
{
Map<String,Object> appItem = new HashMap<String,Object>();
appItem.put("Logo", R.drawable.bus1);
appItem.put("ItemName", items[i]);
appItems.add(appItem);
}
SimpleAdapter itemAdapter = new SimpleAdapter(this,appItems,R.layout.main,
new String[]{"ItemName","Logo"},
new int[]{R.id.tvMainItem,R.id.ivLogo});
setListAdapter(itemAdapter);
}
private SQLiteDatabase openDatabase()
{
try
{
String databaseFilename = DATABASE_PATH+"/"+DATABASE_FILENAME;
Log.d("path110", databaseFilename);
if(!(new File(databaseFilename).exists()))
{
InputStream is = getResources().openRawResource(R.raw.businfo);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while((count = is.read(buffer))>0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
return database;
}
catch(Exception e)
{
Log.d("message110", e.toString());
}
return null;
}
}
我测试后发现,我的cursor只返回了6条记录,而我数据库中有11条记录,为什么升级模拟器,数据查询会丢失数据呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货