Android怎么传递数据到mysql,然后listview会刷新数据
来源:互联网 发布:淘宝电脑直通车是什么 编辑:程序博客网 时间:2024/05/29 18:01
我是通过php传递的,现在可以把全部数据传递过来,但是不知道怎么传递数据过去并且返回?而且listview不会刷新。
Android的代码:
private void getAdapter() {
String url = "http://192.168.21.46/Library/books_list.php";
HttpConnectionUtil connUtil = new HttpConnectionUtil();
connUtil.asyncConnect(url, HttpMethod.POST,
new HttpConnectionCallback() {
@Override
public void execute(String response) {
books = JsonToList(response);
setInAdapter();
listView.setAdapter(adapter);
}
});
}
/**
* 将数据配入ListView中
*/
protected void setInAdapter() {
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
// 将persons中的数据转换到ArrayList<Map<String,String>>中
// String>>中,因为SimpleAdapter要用这个类型的数据进行适配
Map<String, String> map;
for (Book b : books) {
map = new HashMap<String, String>();
map.put("id", b.getId());
map.put("author", b.getAuthor());
map.put("name", b.getName());
map.put("press", b.getPress());
lists.add(map);
}
// HashMap<String, String>中的key
String[] from = { "id", "author", "name", "press" };
// list_item.xml中对应的控件ID
int[] to = { R.id.lb_lv_id, R.id.lb_lv_author, R.id.lb_lv_bookname,
R.id.lb_lv_press };
adapter = new SimpleAdapter(this, lists, R.layout.listview, from, to);
}
protected List<Book> JsonToList(String response) {
List<Book> list = new ArrayList<Book>();
try {
JSONObject jObiect = new JSONObject(response);
JSONArray array = jObiect.getJSONArray("books");
// // 将字符串转换为Json数组
// JSONArray array = new JSONArray(response);
// 数组长度
length = array.length();
for (int i = 0; i < length; i++) {
// 将每一个数组再转换成Json对象
JSONObject obj = array.getJSONObject(i);
book = new Book();
book.setId(obj.getString("id"));
book.setAuthor(obj.getString("author"));
book.setName(obj.getString("name"));
book.setPress(obj.getString("press"));
list.add(book);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
PHP的代码:
if(isset($_GET['classify'])){
$classify = $_GET['classify'];
if($classify == "全部"){
$sql_select = mysql_query("select * from books");
}
else{
$sql_select = mysql_query("select * from books where b_classify = '$classify'");
}
if(!empty($sql_select)){
if(mysql_num_rows($sql_select) > 0) {
// looping through all results
// products node
$response["books"] = array();
while(!!$row= mysql_fetch_array($sql_select)) {
// temp user array
$book= array();
$book["id"] = $row["b_id"];
$book["name"] = $row["b_name"];
$book["author"] = $row["b_author"];
$book["press"] = $row["b_press"];
// push single product into final response array
array_push($response["books"], $book);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
}
如图,就是可以把”计算机“的字符传递过去,然后返回计算机类的数据填写在listview,并且listview刷新!!!
Android的代码:
private void getAdapter() {
String url = "http://192.168.21.46/Library/books_list.php";
HttpConnectionUtil connUtil = new HttpConnectionUtil();
connUtil.asyncConnect(url, HttpMethod.POST,
new HttpConnectionCallback() {
@Override
public void execute(String response) {
books = JsonToList(response);
setInAdapter();
listView.setAdapter(adapter);
}
});
}
/**
* 将数据配入ListView中
*/
protected void setInAdapter() {
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
// 将persons中的数据转换到ArrayList<Map<String,String>>中
// String>>中,因为SimpleAdapter要用这个类型的数据进行适配
Map<String, String> map;
for (Book b : books) {
map = new HashMap<String, String>();
map.put("id", b.getId());
map.put("author", b.getAuthor());
map.put("name", b.getName());
map.put("press", b.getPress());
lists.add(map);
}
// HashMap<String, String>中的key
String[] from = { "id", "author", "name", "press" };
// list_item.xml中对应的控件ID
int[] to = { R.id.lb_lv_id, R.id.lb_lv_author, R.id.lb_lv_bookname,
R.id.lb_lv_press };
adapter = new SimpleAdapter(this, lists, R.layout.listview, from, to);
}
protected List<Book> JsonToList(String response) {
List<Book> list = new ArrayList<Book>();
try {
JSONObject jObiect = new JSONObject(response);
JSONArray array = jObiect.getJSONArray("books");
// // 将字符串转换为Json数组
// JSONArray array = new JSONArray(response);
// 数组长度
length = array.length();
for (int i = 0; i < length; i++) {
// 将每一个数组再转换成Json对象
JSONObject obj = array.getJSONObject(i);
book = new Book();
book.setId(obj.getString("id"));
book.setAuthor(obj.getString("author"));
book.setName(obj.getString("name"));
book.setPress(obj.getString("press"));
list.add(book);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
PHP的代码:
if(isset($_GET['classify'])){
$classify = $_GET['classify'];
if($classify == "全部"){
$sql_select = mysql_query("select * from books");
}
else{
$sql_select = mysql_query("select * from books where b_classify = '$classify'");
}
if(!empty($sql_select)){
if(mysql_num_rows($sql_select) > 0) {
// looping through all results
// products node
$response["books"] = array();
while(!!$row= mysql_fetch_array($sql_select)) {
// temp user array
$book= array();
$book["id"] = $row["b_id"];
$book["name"] = $row["b_name"];
$book["author"] = $row["b_author"];
$book["press"] = $row["b_press"];
// push single product into final response array
array_push($response["books"], $book);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
}
如图,就是可以把”计算机“的字符传递过去,然后返回计算机类的数据填写在listview,并且listview刷新!!!
0 0
- Android怎么传递数据到mysql,然后listview会刷新数据
- Android ListView数据刷新
- android listview数据刷新
- Android ListView数据刷新
- Android Listview下拉刷新数据
- Android ListView局部刷新数据
- Android ListView嵌套ListView、滚动刷新数据
- Android动态刷新listview中的数据
- Android动态刷新listview中的数据
- android listview 用simplecursoradapter适配器数据刷新
- android listview刷新数据,不用跳转页面
- android ListView 数据刷新时抛.nullpointerexception
- Android之ListView内容数据刷新
- Android listview下拉刷新,出现重复数据
- ListView刷新数据方法
- ListView数据动态刷新
- Qt ListView 刷新数据
- ListView数据动态刷新
- pyspider 中文简介
- 未完待续
- 局域网内如何连接MYSQL数据库
- 字符串的操作函数
- 程辑包‘installr’是用R版本3.3.2 来建造的解决方法
- Android怎么传递数据到mysql,然后listview会刷新数据
- 了解CSS overflow属性
- 一个数组中只有两个数字是出现一次的,其他数字都是成对出现,找出这两个数字
- RNN在自然语言处理中的应用
- jvm - 类的初始化过程
- 点击GrieView中的一个view,弹出菜单,且是单击后弹出,不是长按弹出。
- Android读取web service的返回值(int)
- android studio为什么不能现在引用开源库
- 12月英语总结