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

//私はこのようにしたい私はここに警告メッセージを作成しますプレイリストが見つかりません

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

        }
        }
            }

rがコンテンツを持っている場合は表示され、コンテンツがない場合は「リストが見つかりません」というアラートを設定したいのですが、どうすればそれを達成できますか? 私はelseを使ってみましたが、成功しませんでした。

4

4 に答える 4

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
于 2012-06-22T06:46:31.587 に答える
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{

ダイアログを作成してアラートを表示し、ダイアログを作成するためのリストが空であることをユーザーに伝えることができます。最適なリンクを参照できます。

   }

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

        }

        }
            }

この説明がうまくいくことを願っています..感謝します

于 2012-06-22T06:49:26.983 に答える
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));
    }
于 2012-06-22T06:45:57.950 に答える
0

そのためのカーソルを確認する必要があります。カーソルに少なくとも1つのデータがある場合は、最初のレコードに移動しようとします。最初のレコードがある場合は値をcursor.moveToFirst()返しboolean、Trueの場合はFalseを返します。それを直接使用して、カーソルがデータをフェッチしたかどうかを確認できますまたは以下のようではありません。

if(cursor.moveToFirst())
{
   //Populate Contents
}
else
{
  //No Contents Found
}
于 2012-06-22T06:48:33.113 に答える