public list<string> selectallcargoinfor()
{
list<string> list = new list<string>();
try
string sql = "select * from c";
sqlcommand cmd = new sqlcommand(sql,sqlcon);
sqldatareader reader = cmd.executereader();
while (reader.read())
//将結果集資訊添加到傳回向量中
list.add(reader[0].tostring());
list.add(reader[1].tostring());
list.add(reader[2].tostring());
}
reader.close();
cmd.dispose();
catch(exception)
return list;
接下來是安卓端的:
這個是mainactivity中的設定listview的方法
private void setlistview() {
listview.setvisibility(view.visible);
list<hashmap<string, string>> list = new arraylist<hashmap<string, string>>();
list = dbutil.getallinfo();
adapter = new simpleadapter(mainactivity.this, list, r.layout.adapter_item,
new string[] { "cno", "cname", "cnum" },
new int[] { r.id.txt_cno, r.id.txt_cname, r.id.txt_cnum });
listview.setadapter(adapter);
這個是操作類:
public list<hashmap<string, string>> getallinfo() {
arraylist.clear();
brraylist.clear();
crraylist.clear();
new thread(new runnable() {
@override
public void run() {
// todo auto-generated method stub
crraylist = soap.getwebservre("selectallcargoinfor", arraylist, brraylist);
}).start();
hashmap<string, string> temphash = new hashmap<string, string>();
temphash.put("cno", "cno");
temphash.put("cname", "cname");
temphash.put("cnum", "cnum");
list.add(temphash);
for (int j = 0; j < crraylist.size(); j += 3) {
hashmap<string, string> hashmap = new hashmap<string, string>();
hashmap.put("cno", crraylist.get(j));
hashmap.put("cname", crraylist.get(j + 1));
hashmap.put("cnum", crraylist.get(j + 2));
list.add(hashmap);
連接配接webservice的那個方法httpconnsoap應該是沒問題的因為資料庫的增删都是可以的,就是無法實作這個顯示所有資訊到listview中的這個功能不知道為什麼,logcat中也是一片綠沒什麼問題
logcat中的資訊:
05-02 15:51:40.642: i/system.out(3678): <?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"><soap:body><selectallcargoinforresponse xmlns="http://tempuri.org/"><selectallcargoinforresult><string>1</string><string>rice</string><string>100</string><string>2</string><string>dog</string><string>50</string><string>3</string><string>白癡</string><string>25</string></selectallcargoinforresult></selectallcargoinforresponse></soap:body></soap:envelope>
05-02 15:51:40.647: i/system.out(3678): <?xml version="1.0" encoding="utf-8"?
05-02 15:51:40.647: i/system.out(3678): soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"
05-02 15:51:40.647: i/system.out(3678): soap:body
05-02 15:51:40.647: i/system.out(3678): selectallcargoinforresponse xmlns="http://tempuri.org/"
05-02 15:51:40.647: i/system.out(3678): selectallcargoinforresult
05-02 15:51:40.647: i/system.out(3678): 0
05-02 15:51:40.647: i/system.out(3678): string>1</string
05-02 15:51:40.647: i/system.out(3678): string>rice</string
05-02 15:51:40.647: i/system.out(3678): string>100</string
05-02 15:51:40.647: i/system.out(3678): string>2</string
05-02 15:51:40.652: i/system.out(3678): string>dog</string
05-02 15:51:40.652: i/system.out(3678): string>50</string
05-02 15:51:40.652: i/system.out(3678): string>3</string
05-02 15:51:40.652: i/system.out(3678): string>白癡</string
05-02 15:51:40.652: i/system.out(3678): string>25</string
05-02 15:51:40.652: i/system.out(3678): /selectallcargoinforresult
05-02 15:51:40.652: i/system.out(3678): 1
============解決方案1============
分析原因就是線上程還沒有執行完時候,getallinfo早已執行完畢以後,,是以在執行for (int j = 0; j < crraylist.size(); j += 3)時候, crraylist為零行。你取出的logcat是執行請求以後的傳回資料,這時候setlistview方法早已經走完,是以隻有一行資料。截圖中的一行資料來自
hashmap<string, string> temphash = new hashmap<string, string>();
temphash.put("cno", "cno");
temphash.put("cname", "cname");
temphash.put("cnum", "cnum");
list.add(temphash);
使用thread應該配合handler來使用。
我把代碼修改一下
private final static int request_success = 1;
private final static int request_false = 0;
private void requestdata()
message msg = new message();
if(crraylist.size()>0)
msg.what = request_success;
else
msg.what = request_false;
// 發送消息
mhandler.sendmessage(msg);
public handler mhandler = new handler(){
// 接收消息
public void handlemessage(message msg) {
super.handlemessage(msg);
switch (msg.what)
case request_success:
setlistview();
break;
case request_false:
// 做錯誤處理
default:
};
執行requestdata就可以,我沒法編譯,細節自己再調整看一下,應該能解決問題了。
你給的分數太少了,大牛們都不給你解答。如果解決問題就給分哈。
另外,java多線程的操作可以系統學習一下。工作中使用極為頻繁
最新内容請見作者的github頁:http://qaseven.github.io/