2017-07-20 13 views
0

私はリストを表示するためにAPIを使用してアプリケーションを開発しており、リストビューにはデータが含まれています。しかし、私がプロジェクトを実行すると、活動は空白の結果を示し、logcatは 'program "(つまり配列名)メッセージの値が表示されません。 次のコードの結果を表示するにはどうすればよいですか?あなたはJSONExceptionを取得しているアクティビティが空白に表示されますか?

public class MainActivity extends Activity { 
     // Declare Variables 
     JSONObject jsonobject; 
     JSONArray jsonarray; 
     ListView listview; 
     ListViewAdapter adapter; 
     ProgressDialog mProgressDialog; 
     ArrayList<HashMap<String, String>> arraylist; 

     static String AID = "asanaid"; 
     static String ANAME = "asananame"; 
     static String DURATION = "duration"; 
     static String IMGURL = "imgeurl"; 
     static String IMGVERSION = "imgeversion"; 
     static String AUDIOURL = "audiourl"; 
     static String AUDIOVERSION = "audioversion"; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Get the view from listview_main.xml 
      setContentView(R.layout.activity_relaxation_lv); 
      // Execute DownloadJSON AsyncTask 
      new DownloadJSON().execute(); 
     } 

     // DownloadJSON AsyncTask 
     private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       // Create a progressdialog 
       mProgressDialog = new ProgressDialog(MainActivity.this); 
       // Set progressdialog title 
       mProgressDialog.setTitle("Android JSON Parse Tutorial"); 
       // Set progressdialog message 
       mProgressDialog.setMessage("Loading..."); 
       mProgressDialog.setIndeterminate(false); 
       // Show progressdialog 
       mProgressDialog.show(); 
      } 

      @Override 
      protected Void doInBackground(Void... params) { 
       // Create an array 
       arraylist = new ArrayList<HashMap<String, String>>(); 
       // Retrieve JSON Objects from the given URL address 
       jsonobject = JSONfunctions 
         .getJSONfromURL("http://www....."); 

       try { 
        // Locate the array name in JSON 
        jsonarray = jsonobject.getJSONArray("program"); 

        for (int i = 0; i < jsonarray.length(); i++) { 
         HashMap<String, String> map = new HashMap<String, String>(); 
         jsonobject = jsonarray.getJSONObject(i); 
         // Retrive JSON Objects 
         map.put("asanaid", jsonobject.getString("asanaid")); 
         map.put("asananame", jsonobject.getString("asananame")); 
         map.put("duration", jsonobject.getString("duration")); 
         map.put("imgeurl", jsonobject.getString("imgeurl")); 
         map.put("imgeversion", jsonobject.getString("imgeversion")); 
         map.put("audiourl", jsonobject.getString("audiourl")); 
         map.put("audioversion", jsonobject.getString("audioversion")); 

         // Set the JSON Objects into the array 
         arraylist.add(map); 
        } 
       } catch (JSONException e) { 
        Log.e("Error", e.getMessage()); 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void args) { 
       // Locate the listview in listview_main.xml 
       listview = (ListView) findViewById(R.id.listview); 
       // Pass the results into ListViewAdapter.java 
       adapter = new ListViewAdapter(MainActivity.this, arraylist); 
       // Set the adapter to the ListView 
       listview.setAdapter(adapter); 
       // Close the progressdialog 
       mProgressDialog.dismiss(); 
      } 
     } 
    } 


    public class ListViewAdapter extends BaseAdapter{ 

    // Declare Variables 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<HashMap<String, String>> data; 
    ImageLoader imageLoader; 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

public ListViewAdapter(Context context, 
     ArrayList<HashMap<String, String>> arraylist) { 
    this.context = context; 
    data = arraylist; 
    imageLoader = new ImageLoader(context); 
} 

    @Override 
    public int getCount() { 
    return data.size(); 
} 

    @Override 
    public Object getItem(int position) { 
    return null; 
} 

    @Override 
    public long getItemId(int position) { 
    return 0; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    // Declare Variables 
    TextView tvAname, tvId, imgUrl, imgVersion, audioUrl, audioVersion, duration; 
    ImageView imgPose; 

    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View itemView = inflater.inflate(R.layout.relaxationlv_single_item, parent, false); 
    // Get the position 
    resultp = data.get(position); 

    // Locate the TextViews in listview_item.xml 
    tvAname = (TextView) itemView.findViewById(R.id.lv_aname); 
    tvAname.setText(resultp.get(MainActivity.ANAME)); 

    tvId = (TextView)itemView.findViewById(R.id.lv_aid); 
    tvId.setText(resultp.get(MainActivity.AID)); 

    duration = (TextView)itemView.findViewById(R.id.lv_duration); 
    duration.setText(resultp.get(MainActivity.DURATION)); 

    imgVersion = (TextView)itemView.findViewById(R.id.lv_imgversion); 
    imgVersion.setText(resultp.get(MainActivity.IMGURL)); 

    audioVersion = (TextView)itemView.findViewById(R.id.lv_audioversion); 
    audioVersion.setText(resultp.get(MainActivity.AUDIOVERSION)); 

    audioUrl= (TextView)itemView.findViewById(R.id.lv_audiourl); 
    audioUrl.setText(resultp.get(MainActivity.AUDIOURL)); 

    imgPose = (ImageView)itemView.findViewById(R.id.lv_imgurl); 
    imageLoader.DisplayImage(resultp.get(MainActivity.IMGURL), imgPose); 

    // Capture ListView item click 
    itemView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Get the position 
      resultp = data.get(position); 
      Intent intent = new Intent(context, RelaxationLvAudioPlayerActivity1.class); 
      intent.putExtra("asanaid", resultp.get(MainActivity.AID)); 
      intent.putExtra("asananame", resultp.get(MainActivity.ANAME)); 
      intent.putExtra("duration", resultp.get(MainActivity.DURATION)); 
      intent.putExtra("imgeurl", resultp.get(MainActivity.IMGURL)); 
      intent.putExtra("imgeversion", resultp.get(MainActivity.IMGVERSION)); 
      intent.putExtra("audiourl", resultp.get(MainActivity.AUDIOURL)); 
      intent.putExtra("audioversion", resultp.get(MainActivity.AUDIOVERSION)); 
      context.startActivity(intent); 

     } 
    }); 
    return itemView; 
    } 
} 
+0

ログを送信してください。 – abhishek

+0

07-20 14:54:27.110 26060-26093/com.sca.itfms.yogapointdb E /エラー:プログラムの値がありません 07-20 14:54:27.110 26060-26093/com.sca.itfms.yogapointdb W/System.err:org.json.JSONException:プログラムの値がありません – Rohu

+0

これはおそらく、あなたのjsonに名前プログラムの要素がないことを意味します。おそらくポイントjsonarray = jsonobject.getJSONArray( "program")で破損します。 –

答えて

0

-

次のフィールドのいずれかがあなたのJSON応答に欠けているかどうかを確認してください -

map.put("asanaid", jsonobject.getString("asanaid")); 
         map.put("asananame", jsonobject.getString("asananame")); 
         map.put("duration", jsonobject.getString("duration")); 
         map.put("imgeurl", jsonobject.getString("imgeurl")); 
         map.put("imgeversion", jsonobject.getString("imgeversion")); 
         map.put("audiourl", jsonobject.getString("audiourl")); 
         map.put("audioversion", jsonobject.getString("audioversion")); 

をあなたはこれがjsonobjectの値を印刷買う確認することができます。 getString( "asaname")などがあります。

また、異なるデータタイプの値である可能性があります。例えば。あなたがStringとして取得しようとしているaudioversionですが、JSONではInteger型です。

+0

jsonレスポンス – Rohu

+0

のコードにフィールドがありません。すべての値を印刷しようとしましたか?場合によっては、値が異なるデータ型になることもあります。 – abhishek

+0

私のコードで私も画像を使用 – Rohu

関連する問題