-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 = 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 {      



        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{

//i want like this i develope alert message here playlist not found

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

        }
        }
            }

I need help on if the value r having content then display, if not have content then I want to set alert "no list found" how can I achieve that? i have try using else but not success.

Ilaiya Raja
  • 13
  • 1
  • 5

4 Answers4

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
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
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));
    }
Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

You just need to check the Cursor for that, If cursor has atleast one data then try to move to the first record, as cursor.moveToFirst() returns boolean value if have first record then True else False, you can use that directly to check if the Cursor have fetched data or not as below.

if(cursor.moveToFirst())
{
   //Populate Contents
}
else
{
  //No Contents Found
}
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • Hello, I have been marking that you have been really very well active in Android Tag and answering so frequently and that's very nice, Thanks and Congrates to you, I want to suggest you to try giving solutions with explainations cause your answer with correct sol are manytime shown in Review as Low Quality so please improve that also :) Keep Doing The Great Work.!! – MKJParekh Jun 22 '12 at 07:23
  • i posted my full source, its not working please give solution – Ilaiya Raja Jun 22 '12 at 09:17
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{

you can show Alert her by creating dialog and tell user that list is empty for creating dialog you can refer best Link..

   }

just added checking for arraylist contains elements or not if not then you can display alert through Toast or dialog as i provided link for it. --------------------------Here is your problems solution-------------------------------------------

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();

        }

        }
            }

Hope this explanation works for you.. appreciate

MobileEvangelist
  • 2,583
  • 1
  • 25
  • 36