【问题标题】:What to do when no results are returned from SQL query?SQL查询没有返回结果怎么办?
【发布时间】:2014-02-25 20:00:57
【问题描述】:

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

这是我的 ProgressTask:

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;

}

}

当 serach 没有返回结果时,PHP 会返回这个 JSON 数据:

{
"listings": [
    ""
]
}

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

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

【问题讨论】:

  • 对不起,这个问题可能措辞不当,但我的意思是我如何确定何时发送消息,因为在某些情况下 JSON 不会为空

标签: java android json listview listadapter


【解决方案1】:

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

(可选)您的自定义视图可以包含任何类型的另一个视图对象,以便在列表视图为空时显示。这个“空列表”通知器必须有一个 id “android:id/empty”。请注意,当存在空视图时,列表视图将在没有数据显示时隐藏。

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

基本上,您为列表添加一个@android:id/list ListView,为No result found 消息添加一个@android:id/empty TextView。 ListActivity 处理 2 之间的切换。

编辑

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

【讨论】:

    【解决方案2】:

    检查 JSONArray 的大小,如果它不超过 0 ,则显示 toast。

    JSONObject object = new JSONObject(builder.toString());
    jarray = object.getJSONArray("listings");
    
    if (jarray == null || jarray.length() == 0) {
        Toast.makeText(context, "No results found", duration).show():
    }
    else{
      // existing code to process entries
    }
    

    编辑

    如果您的 json 数组为 null 或其大小 == 0,则您可以使用以下布局 将列表视图的可见性设置为消失,将文本视图的可见性设置为可见。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/screen_background"
        tools:context=".FAQFragment" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ListView>
    
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="No Values to Show"
                android:visibility="gone"
                android:id="@+id/tvMsg" />
    
    </RelativeLayout>
    

    现在在你的 AsyncTask 中:-

    public class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        private  JSONArray json=null;
        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
            JSON = jParser.getJSONFromUrl(url);
    
            if(json!=null && json.length()>0)// returning from doInBackground() if json is null
                return null;
            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();
             }
    
           if(json!=null && json.length()>0){
            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();
            }else{
               tvMsg.setVisibility(View.VISIBLE);  // displaying overlay text when no data is present
             yourListView.setVisibility(View.GONE);
            }
            }
        }
    }
    

    【讨论】:

    • 它不显示吐司只是列表视图应该在的空白屏幕,我假设它是因为它不会将 JSON 读取为空?
    • 我可以将文本视图包含在我已经拥有的存储 JSON 的列表视图中
    • 不,textview 应该只在没有值来填充 listview 时才可见,这样你就可以向用户显示 msg
    • json 在 postExecute 中不可用?
    • 好吧,这是我的错误,现在我已经编辑了 ans,我已经将“json”的范围更改为类级别,现在它应该可以工作了,请检查编辑!!
    【解决方案3】:

    您可以检查第一个元素
    例如:

    JSONArray json = jParser.getJSONFromUrl(url);
    if(json == null){
         //Oops!
         showToast();
         return null;
    }
    ...
    jarray = object.getJSONArray("listings");
    if(jarray.get(0).toString().length() == 0)
    //Oops!
    return null;
    ...
    

    编辑
    例 2:

    ...
    JSONArray json = jParser.getJSONFromUrl(url);
    if(json.get(0).toString().length() == 0){
         //Oops!
         showToast();
         return null;
    }
    ...
    

    祝你好运

    【讨论】:

    • 如果 jarray 为 null 可能会抛出 NullPointerException !!
    • 是的。但至少它的null 不是空数组。这只是他可以检查的例子if(json == null) {doSomthingAndReturn()};我会再举一个例子。
    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2016-08-22
    • 2015-02-25
    • 1970-01-01
    相关资源
    最近更新 更多