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

标签 java android json listview listadapter

使用 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/

相关文章:

java - J2CA0081E Websphere 6 异常

java - EasyMock:期望集合中的元素数量

android - 风格没有得到应用

json - 配置单元中的 Where 子句覆盖 JSON 数据

javascript - 如何使用此 ajax 函数发布多个输出?

python - 如何将 django/python 中的模型对象列表序列化为 JSON

java - Jersey 单元测试 : in memory container vs grizzly

android - 像我的 Android 手机上的其他应用一样显示共享菜单

android - 当阿拉伯文本带有数字时,阿拉伯文本出现反向

java - ffmpeg 准确地填充和延迟音频文件