gpt4 book ai didi

java - SQL查询没有返回结果怎么办?

转载 作者:行者123 更新时间:2023-12-02 06:09:24 31 4
gpt4 key购买 nike

使用 PHP 从 Android 应用程序查询我的 SQL 数据库,但有时搜索不会返回任何结果,因为搜索条件没有给出匹配项。

这是我的进度任务:

public class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(GetData.this);
dialog.setMessage("Searching Database");
dialog.show();
}
protected Boolean doInBackground(final String... args) {

JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONArray json = jParser.getJSONFromUrl(url);

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

try {
JSONObject c = json.getJSONObject(i);
String Listing_ID = c.getString(lblListing_ID);
String Event_ID = c.getString(lblEvent_ID);
String Venue_ID = c.getString(lblVenue_ID);
String Start_Date = c.getString(lblStart_Date);
String End_Date = c.getString(lblEnd_Date);
String Frequency = c.getString(lblFrequency);

HashMap<String, String> map = new HashMap<String, String>();

// Add child node to HashMap key & value
map.put(lblListing_ID, Listing_ID);
map.put(lblEvent_ID, Event_ID);
map.put(lblVenue_ID, Venue_ID);
map.put(lblStart_Date, Start_Date);
map.put(lblEnd_Date, End_Date);
map.put(lblFrequency, Frequency);
jsonlist.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}

protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}

ListAdapter adapter = new SimpleAdapter(GetData.this, jsonlist,
R.layout.list_item, new String[] { lblListing_ID , lblEvent_ID,
lblVenue_ID, lblStart_Date, lblEnd_Date, lblFrequency }, new int[] {
R.id.Listing_ID, R.id.Event_ID, R.id.Venue_ID,
R.id.Start_Date, R.id.End_Date, R.id.Frequency });
setListAdapter(adapter);
// select single ListView item
lv = getListView();
}
}
}

这是我的 JSONParser 类:

public class JSONParser {

static InputStream iStream = null;
static JSONArray jarray = null;
static String json = "";

public JSONParser() {
}

public JSONArray getJSONFromUrl(String url) {

StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// Parse String to JSON object
try {
JSONObject object = new JSONObject( builder.toString());
jarray = object.getJSONArray("listings");

} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}



// return JSON Object
return jarray;

}

}

当搜索没有返回结果时,PHP 返回此 JSON 数据:

{
"listings": [
""
]
}

当 JSON 为空时,我希望输出某种消息“未找到结果”。我进行了一些搜索并尝试了 JSON.length() = 0 或 setEmptyView 等方法,但没有成功。我只是想知道是否有人对没有返回结果时最好的响应方式有任何想法,如果有的话如何实现。

预先感谢您的任何回复,如果您需要更多信息,请在下面评论。谢谢

最佳答案

通常,ListView显示在ListActivity中。这样的 Activity 包含嵌入的机制来准确处理这种情况:

Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:id/empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.

(在 http://developer.android.com/reference/android/app/ListActivity.html 中)

基本上,你输入 @android:id/list ListView 用于您的列表,以及 @android:id/empty TextView 为No result found信息。 ListActivity 处理两者之间的切换。

编辑

来自您使用的方法,例如setListAdapter ,我假设您确实使用 ListActivity 。因此,您可以轻松集成@android:id/empty机制。

关于java - SQL查询没有返回结果怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22024876/

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