使用PHP从Android应用程序查询我的SQL数据库,但有时搜索不会返回任何结果,因为搜索条件没有匹配。
这是我的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;
}
}
{
"listings": [
""
]
}
当JSON为空时,我希望输出某种消息,说“找不到结果”。我已经搜索了一下并尝试了JSON.length()= 0或setEmptyView等方法但没有成功。我只是想知道是否有人对没有返回结果的最佳响应方式有什么想法,如果有的话如何实现它。
提前感谢您的回复,如果您需要更多信息,请在下方发表评论。感谢
答案 0 :(得分:1)
检查JSONArray的大小,如果它不超过0,则显示祝酒词。
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,则可以使用以下布局 设置列表视图的可见性,并将textview的可见性设置为可见。
<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);
}
}
}
}
答案 1 :(得分:1)
通常,ListView
会显示在ListActivity
中。这样的活动包含一个机制来处理这种情况:
或者,您的自定义视图可以包含列表视图为空时要显示的任何类型的另一个视图对象。这个“空列表”通知符必须有一个id“android:id / empty”。请注意,当存在空视图时,如果没有要显示的数据,将隐藏列表视图。
(http://developer.android.com/reference/android/app/ListActivity.html)
基本上,您为列表添加了@android:id/list
ListView,为@android:id/empty
消息添加了No result found
TextView。 ListActivity处理2之间的切换。
根据您使用的方法,例如setListAdapter
,我假设您确实在使用ListActivity
。因此,您可以轻松集成@android:id/empty
机制。
答案 2 :(得分:0)
您可以检查第一个元素
的实施例强>
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;
...
修改强>
EX2:
...
JSONArray json = jParser.getJSONFromUrl(url);
if(json.get(0).toString().length() == 0){
//Oops!
showToast();
return null;
}
...
祝你好运