gpt4 book ai didi

php - 如何对从 phpMyAdmin 获取数据的 ListView 进行排序

转载 作者:行者123 更新时间:2023-11-29 23:22:09 24 4
gpt4 key购买 nike

我有一个简单的 Android 应用程序,它从 phpMyAdmin DB 获取数据并将其显示在 ListView 中。在我的数据库中,我有两列,第一列称为 place_name,第二列称为 Numbers_of_visits。我可以毫无问题地从数据库获取数据。

但是我在尝试根据最高访问次数(即数据库中的)对 ListView 进行排序时遇到问题,因此访问次数最高的地方应该位于顶部,依此类推。

请大家帮我做到这一点

这是我的 Jvav 代码

        package com.example.ems_project;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class PlacesListView extends Activity {
private String jsonResult;
private String url = "http://ibrahimaldosari.t15.org/listview.php";
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main2);
listView = (ListView) findViewById(R.id.listview);
accessWebService();
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}

catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}

catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}

@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task

public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("placestable");

for (int i = 0; i < jsonMainNode.length(); i++) {


JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
int number = jsonChildNode.optInt("visit");

String outPut = name + "---" + number;
employeeList.add(createEmployee("places", outPut));

}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),Toast.LENGTH_SHORT).show();
}

SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, android.R.layout.simple_list_item_1, new String[] { "places" }, new int[] { android.R.id.text1 });

listView.setAdapter(simpleAdapter);

}

private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}

这是我正在使用的 php 文件

<?php
$host="mysql.freehostingnoads.net"; //replace with database hostname
$username="u746805016_ibrr1"; //replace with database username
$password="113281188"; //replace with database password
$db_name="u746805016_ibrr1"; //replace with database name

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from placestable";
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['placestable'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>

最佳答案

您可以通过两种方式实现此目的。1. 在服务器端,将选择查询稍微更改为如下所示:

$sql = "从placestable中选择* ORDER BY访问DESC";

或者2. 通过在应用程序端对此进行排序。但随后我将创建带有字段的 Employee 类:姓名和访问,并实现 Comparable 接口(interface)。

关于php - 如何对从 phpMyAdmin 获取数据的 ListView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com