android listview适配器游标

android listview适配器游标,android,cursor,android-listview,adapter,Android,Cursor,Android Listview,Adapter,我需要有关值r是否有内容然后显示的帮助,如果没有内容,则我想设置警报“未找到列表”,如何实现?我尝试过使用else,但没有成功。if(null!=cursor) System.out.println("playlist not found"); } } } { //其他工作。。。。。。 if(cursor.getCount()>0) { //显示警惕 } 其他的 { cursor.m

我需要有关值
r
是否有内容然后显示的帮助,如果没有内容,则我想设置警报“未找到列表”,如何实现?我尝试过使用else,但没有成功。

if(null!=cursor)
                 System.out.println("playlist not  found");

        }
        }
            }
{ //其他工作。。。。。。 if(cursor.getCount()>0) { //显示警惕 } 其他的 { cursor.moveToFirst(); setListAdapter(新的ArrayAdapter(this,R.layout.custom_list_item,R)); }
如果(null!=cursor&&cursor.getCount()>0)//检查游标不为null且至少有1个数据
{
cursor.moveToFirst();
setListAdapter(新的ArrayAdapter(this,R.layout.custom_list_item,R));
//使用内容执行操作
}
其他的
//在此处显示警报,即未找到列表

您只需检查光标,如果光标至少有一个数据,然后尝试移动到第一条记录,如
光标。moveToFirst()
返回
布尔值,如果有第一条记录,则返回True,否则返回False,您可以直接使用该值检查光标是否已获取数据,如下所示

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
刚刚添加了对arraylist是否包含元素的检查。如果不包含元素,则可以通过Toast或dialog显示警报,正如我为其提供的链接一样。 --------------------------这是你的问题解决方案-------------------------------------------

   }
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.artistname);
Bundle extras=getIntent().getExtras();
String val=extras.getString(“艺人名称”);
//Toast.makeText(getApplicationContext(),val,Toast.LENGTH_LONG.show();
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
ArrayList nameValuePairs=新的ArrayList();
试一试{
//http post
HttpClient HttpClient=新的DefaultHttpClient();
//HttpPost HttpPost=新的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+”&format=json&flg=1”);
//HttpPost HttpPost=新的HttpPost(“http://192.168.0.5/staging/android/ilaiyarajawebservice.php?format=xml");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}
捕获(例外e){
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
//将响应转换为字符串
尝试
{
BufferedReader=新的BufferedReader(新的InputStreamReader(即“UTF-8”));
sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}
捕获(例外e)
{
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
//结束将响应转换为字符串
试一试{
JSONArray jArray=新JSONArray(结果);
JSONObject json_data=null;
对于(int i=0;i0){
Cursor Cursor=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null);
如果(null!=光标)
{
cursor.moveToFirst();
setListAdapter(新的ArrayAdapter(this,R.layout.custom_list_item,R));
}
}否则{
Toast.makeToast(getApplicationcontext(),“无记录…”,Toast.SHORT.show();
}
}
}

希望这个解释对你有用..感谢

如果只检查你的数据库/光标,是否有记录,不来其他部分执行?我发布完整的源代码,请查找并给出解决方案你好,我一直在标记你在Android标签中非常活跃,并且经常回答,这很好,谢谢,聪对你来说,我想建议你尝试给出解释性的解决方案,因为你的正确答案在评论中经常显示为低质量,所以请改进:)继续做伟大的工作。!!我发布了我的完整来源,它不起作用,请给出解决方案你好,我想在这里评论我在Viputsah上做的相同的事情回答..请你检查一下,继续做好工作..!谢谢MKJParekh,我会记住,为了将来…但我认为这个问题非常简单,这就是为什么我没有找到任何额外的东西来写:)是的,这发生了很多次,我们试图简单地用一个简短而甜蜜的解决方案来回答这个简单的问题,大家都明白。(但请记住,对于提问者来说,事情很难,只有他提出了问题)我尝试但不工作,只有当条件只执行。我不知道我认为有任何变化的web服务?我尝试但不工作,只有当条件只执行。我不知道我认为有任何变化的web服务?我张贴了我的完整源代码,它不工作,请给出解决方案什么是r和它的定义在您的代码中..答案是编辑的,看看刚刚check ArrayListI的大小没有告诉你哪一个代码?只是对你的代码做一些修改,它会像我建议的那样工作。我发布了我的完整源代码,它不工作,请给出解决方案
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));
    }
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
if(cursor.moveToFirst())
{
   //Populate Contents
}
else
{
  //No Contents Found
}
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{
   }
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();

        }

        }
            }