2012-06-22 47 views
-2
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.artistname); 

    Bundle extras = getIntent().getExtras(); 
    String val = extras.getString("artistname"); 

    //Toast.makeText(getApplicationContext(), val, Toast.LENGTH_LONG).show(); 


    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    try{ 

    //http post 
    HttpClient httpclient = new DefaultHttpClient(); 
    //HttpPost httppost = new HttpPost("http://192.168.0.5/staging/android/get_search_result.php?keyword=Chinna_Mani&format=json"); 

HttpPost httppost =新HttpPost( 「http://192.168.0.5/staging/android/get_search_result.php?keyword=」 + VAL +」 &格式= JSON & FLG = 1" );機器人列表視圖適配器光標

//HttpPost httppost = new HttpPost("http://192.168.0.5/staging/android/ilaiyarajawebservice.php?format=xml"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 
    } 
    catch(Exception e){ 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
    } 

    //Convert response to string 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); 

     sb = new StringBuilder(); 

     String line = null; 

     while ((line = reader.readLine()) != null) 
     { 
     sb.append(line + "\n"); 
     } 

     is.close(); 

     result = sb.toString(); 
    } 
    catch(Exception e) 
    { 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
    } 
    //END Convert response to string 
    try{ 
      JSONArray jArray = new JSONArray(result); 
      JSONObject json_data=null; 
      for(int i=0;i<jArray.length();i++) 
      { 
       json_data = jArray.getJSONObject(i); 


       r.add(json_data.getString("artistfirstname")); 


       // r.add(json_data.getString("file_name")+json_data.getString("artist_name") +json_data.getString("album_name")); 

      }            
     }        



    catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

      finally {  



     Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); 



     if(null != cursor) 
     { 

       cursor.moveToFirst(); 

       setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_list_item, r)); 

      } 
      else{ 

//我想這樣我在這裏警告下發展信息播放清單中找不到

    System.out.println("playlist not found"); 

     } 
     } 
      } 

我需要幫助。如果有值r內容則顯示,如果沒有內容,那麼我想設置警報「找不到列表」我如何實現這一目標?我嘗試使用其他但不成功。

+0

提高您的問題... .... –

+0

我張貼我的完整源代碼,它的工作不正常,請給解決方案 –

回答

0
if(null != cursor) 
{ 
    //other work...... 

    if (cursor.getCount() > 0) 
    {   
     //show alert 

    } 
    else 
    { 
     cursor.moveToFirst(); 
     setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_list_item, r)); 
    } 
+0

您好,我想評論一下,我在ViputShah的答案中做了同樣的事情。您請檢查並保持良好的工作..! – MKJParekh

+0

謝謝MKJParekh我會記住,對於未來......但我認爲這個問題很簡單,這就是爲什麼我沒有發現任何額外的東西:) –

+0

發生了很多次,我們試圖回答簡單的問題通過一個簡短而甜蜜的解決方案,所有人都明白。 (但記住問題的事情是很難,然後只有他問了問題) – MKJParekh

1
if(null != cursor && cursor.getCount()>0)//check cursor is not null and having atleast 1 data 
{ 
    cursor.moveToFirst(); 
    setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_list_item, r));   

    //do your operation with content 
} 
else 
    //show alert here i.e. no list found 
+0

不是其他部分執行,如果只是 –

+0

檢查您的數據庫/遊標,他們是否有記錄? –

+0

我發佈了完整的源代碼,請發現並給出解決方案 –

0

你只需要檢查遊標,如果遊標有ATLEAST一個數據,然後嘗試,如果有第一個記錄,然後移動到第一條記錄,如cursor.moveToFirst()回報boolean值TRUE,否則假,你可以直接使用它來檢查遊標是否已經獲取數據,如下所示。

if(cursor.moveToFirst()) 
{ 
    //Populate Contents 
} 
else 
{ 
    //No Contents Found 
} 
+0

您好,我一直在評價您在Android Tag中的活躍程度非常高,並且非常頻繁地回答,這非常好,謝謝並向您致意,我想建議您嘗試給解決方案與解釋導致你的答案與正確的溶膠很多時候在評論中顯示爲低質量,所以請改善:)同時做好偉大的工作。 – MKJParekh

+0

我張貼我的完整源代碼,它不工作,請給解決方案 –

0
if(r.size()>0){  
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); 

    if(null != cursor) 
    { 
     cursor.moveToFirst(); 
    setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_list_item, r));   
//here I need help the value are having content means display, if not have content means I want set alert no list found how? I will try else but not success. 



    } 
}else{ 

你可以通過創建對話框顯示警告她,並告訴用戶該列表是空的,用於創建對話框,您可以參考best Link..

} 

只是增加檢查ArrayList包含的元素或沒有,如果不那麼你可以通過吐司或對話框顯示警報,因爲我提供了它的鏈接。 --------------------------這是您的問題解決方案------------------ -------------------------

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.artistname); 

    Bundle extras = getIntent().getExtras(); 
    String val = extras.getString("artistname"); 

    //Toast.makeText(getApplicationContext(), val, Toast.LENGTH_LONG).show(); 


    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    try{ 

    //http post 
    HttpClient httpclient = new DefaultHttpClient(); 
    //HttpPost httppost = new HttpPost("http://192.168.0.5/staging/android/get_search_result.php?keyword=Chinna_Mani&format=json"); 
HttpPost httppost = new HttpPost("http://192.168.0.5/staging/android/get_search_result.php?keyword="+val+"&format=json&flg=1"); 

    //HttpPost httppost = new HttpPost("http://192.168.0.5/staging/android/ilaiyarajawebservice.php?format=xml"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 
    } 
    catch(Exception e){ 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
    } 

    //Convert response to string 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); 

     sb = new StringBuilder(); 

     String line = null; 

     while ((line = reader.readLine()) != null) 
     { 
     sb.append(line + "\n"); 
     } 

     is.close(); 

     result = sb.toString(); 
    } 
    catch(Exception e) 
    { 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
    } 
    //END Convert response to string 
    try{ 
      JSONArray jArray = new JSONArray(result); 
      JSONObject json_data=null; 
      for(int i=0;i<jArray.length();i++) 
      { 
       json_data = jArray.getJSONObject(i); 


       r.add(json_data.getString("artistfirstname")); 


       // r.add(json_data.getString("file_name")+json_data.getString("artist_name") +json_data.getString("album_name")); 

      }            
     }        



    catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

      finally {  

if(r.size()>0){ 

     Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); 



     if(null != cursor) 
     { 

       cursor.moveToFirst(); 

       setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_list_item, r)); 

      } 
}else{ 

Toast.makeToast(getApplicationcontext(),"no Records ...", Toast.SHORT).show(); 

     } 

     } 
      } 

希望這個解釋對你的作品..欣賞

+0

我嘗試但只有在條件只執行時才工作。我不知道我認爲web服務有任何改變? –

+0

我發佈了我的完整源代碼,它不工作,請給解決方案 –

+0

什麼是r和它在代碼中的定義..? – MobileEvangelist