【问题标题】:android listview adapter cursor [closed]android listview适配器光标[关闭]
【发布时间】:2012-06-22 06:42:04
【问题描述】:
  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 但没有成功。

【问题讨论】:

  • 我发布了我的完整源代码,它不起作用请给出解决方案

标签: android cursor android-listview adapter


【解决方案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

【讨论】:

  • not come else part if only
  • 检查你的数据库/光标,是否有记录?
  • 我发布完整源代码,请查找并给出解决方案
【解决方案2】:

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

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

【讨论】:

  • 您好,我一直在标记您在 Android Tag 中非常活跃并且回复如此频繁,这非常好,感谢并祝贺您,我想建议您尝试提供解决方案解释会导致您的正确 sol 答案在 Review 中多次显示为低质量,所以请也改进它:) 继续做伟大的工作。!!
  • 我发布了我的完整源代码,它不起作用请给出解决方案
【解决方案3】:
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));
    }

【讨论】:

  • 您好,我想在这里发表评论,就像我在 ViputShah 的回答中所做的一样。请您检查一下并继续努力..!
  • 谢谢 MKJParekh,我会记住,未来......但我认为这个问题很简单,这就是为什么我没有发现任何额外的东西要写:)
  • 是的,这种情况发生了很多次,我们试图通过一个简短而甜蜜的解决方案来简单地回答简单的问题,所有人都理解它。 (但请记住,对于发问者来说,事情很难,只有他问过问题)
  • 我尝试但仅在仅执行条件时才起作用。我不知道我认为网络服务有什么变化?
【解决方案4】:
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 是否包含元素的检查,如果没有,那么您可以通过 Toast 或对话框显示警报,因为我为其提供了链接。 --------------------------这是您的问题解决方案------------------- ------------------------------------

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

        }

        }
            }

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

【讨论】:

  • 我尝试但仅在仅执行条件时才起作用。我不知道我认为网络服务有什么变化?
  • 我发布了我的完整源代码,它不起作用请给出解决方案
  • 什么是 r 以及它在您的代码中定义的位置..??
  • 答案已编辑看看只是检查 ArrayList 的大小
  • 我没有给你哪个代码?只需对您的代码进行一些更改,它就会按照我的建议正常工作..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
相关资源
最近更新 更多