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 +“&amp; format = json&amp; 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 :(得分: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
答案 1 :(得分: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));
}
答案 2 :(得分:0)
你只需要检查Cursor,如果游标至少有一个数据然后尝试移动到第一个记录,因为cursor.moveToFirst()
返回boolean
值,如果有第一个记录则为True否则为False,您可以直接使用它来检查Cursor是否已获取数据,如下所示。
if(cursor.moveToFirst())
{
//Populate Contents
}
else
{
//No Contents Found
}
答案 3 :(得分: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包含元素的检查,如果没有,那么你可以通过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();
}
}
}
希望这个解释对你有用..欣赏