|
访问数据库时,手机能增删数据库的数据就是显示不了数据库的里的数据不知道是哪里的问题,用的HTTP
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;
}
接下来是安卓端的:
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() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
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);
}
return list;
}
连接webservice的那个方法HttpConnSoap应该是没问题的因为数据库的增删都是可以的,就是无法实现这个显示所有信息到LISTVIEW中的这个功能不知道为什么,LOGCAT中也是一片绿没什么问题 LOGCAT中的信息: |
|
![]() |
点了后是这样的什么都木有。。。 |
![]() |
来人啊
|
![]() |
crrayList是调用接口返回的数据,在哪里定义的?又在哪里使用的?你在getAllInfo()中用了一个线程,感觉很奇怪。最好把全部的代码贴出来解决。
|
![]() |
getAllInfo中的Soap.GetWebServre方法是网络操作所以放在了子线程中 |
![]() 20分 |
分析原因就是在线程还没有执行完时候,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()
{
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);
Message msg = new Message();
if(crrayList.size()>0)
{
msg.what = REQUEST_SUCCESS;
}
else
{
msg.what = REQUEST_FALSE;
}
// 发送消息
mHandler.sendMessage(msg);
}
}).start();
}
public Handler mHandler = new Handler(){
// 接收消息
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what)
{
case REQUEST_SUCCESS:
setListView();
break;
case REQUEST_FALSE:
// 做错误处理
break;
default:
break;
}
}
};
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() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
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);
}
return list;
}
执行RequestData就可以,我没法编译,细节自己再调整看一下,应该能解决问题了。 另外,Java多线程的操作可以系统学习一下。工作中使用极为频繁。 |
![]() |
楼上的大神非常感谢很有用,太谢谢了
|
![]() |
能不能把你源码发我一份 我最新也在搞这个 一头雾水 谢谢大神 1085301968@qq.com
|
![]() |
我也是看bottle的博客照着做的,评论说要加线程,查询也是不能显示。5L写的我看不太懂,才学android没几天,能不能把你改好的发给我看看,非常感谢。 493891594@qq.com
|
![]() |
requestdata是加在哪里的,在哪里执行,新手才学习没几天没看明白 |

点了后是这样的什么都木有。。。