2017-07-20 4 views
0

私は新しいアンドロイドユーザーです。 I私は上記のデータを取得し、以下のようにフラグメント内のリストを生成したいJSON http://localhost/FindIt/all_items.phpAndroidはjsonを取り出してリストを作成します

[ 
     { 
      "id":"1", 
      "name":"Mukisa Ivan", 
      "title":"Makerere University ID", 
      "description":"This is a lost a ID picked at night near Olympia Hostel Gate", 
      "date":"2017-05-01", 
      "reward":"10,000", 
      "location":"Makerere Kikoni", 
      "photo":"", 
      "category":"Id", 
      "type":"Found", 
      "userId":"1", 
      "email":"[email protected]", 
      "phone":"+256706330511", 
      "userType":"Founder" 
     }, 
     { 
      "id":"3", 
      "name":"Kalule Tom", 
      "title":"UACE Results Slip", 
      "description":"Lost My UACE results slip in areas of Wandegeya", 
      "date":"2017-03-07", 
      "reward":"5,000", 
      "location":"Wandegeya", 
      "photo":"", 
      "category":"Document", 
      "type":"Lost", 
      "userId":"2", 
      "email":"[email protected]", 
      "phone":"+256706330511", 
      "userType":"Loser" 
     } 
    ] 

を返すPHPスクリプトとMySQLデータベースがあります: -

package com.example.kenneth.lostfound; 

    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 

    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.List; 

    public class Fragment2 extends Fragment { 

     /** 
     * Use this factory method to create a new instance of 
     * this fragment using the provided parameters. 
     * 
     * @return A new instance of fragment Fragment2. 
     */ 
     public static Fragment2 newInstance() { 
      return new Fragment2(); 
     } 

     public Fragment2() { 
      // Required empty public constructor 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setHasOptionsMenu(true); 
     } 

     @Override 
     public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){ 
      inflater.inflate(R.menu.fragment2, menu); 
     } 

     @Override 
     public boolean onOptionsItemSelected (MenuItem item){ 
      int id = item.getItemId(); 
      if (id == R.id.action_refresh){ 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      View view = inflater.inflate(R.layout.fragment_fragment2, container, false); 

      String[] lostItemArray = { 
        "National ID - UCU Main Gate - 20/2/2016", 
        "UACE Results slip - Kampala Rd Taxi - 23/2/2016", 
        "Driving Permit - Ham Towers Makerere- 10/2/2016", 

      }; 

      List<String> lost_items = new ArrayList<String>(
        Arrays.asList(lostItemArray) 
      ); 

      ArrayAdapter<String> mLostFoundAdapter = new ArrayAdapter<String>(
        getActivity(), 
        R.layout.list_item_lostfound, 
        R.id.list_item_lostfound, 
        lost_items 
      ); 

      ListView listView = (ListView) view.findViewById(
        R.id.listview_lostfound 
      ); 
      listView.setAdapter(mLostFoundAdapter); 

      return view; 
     } 
    } 

を私はデータをフェッチしたいですjsonからリストを更新するか、リストのView要素に値を設定します。私はアンドロイドスタジオを使用しています

+0

まず、[Volley](https://developer.android.com/training/volley/request.html)やその他の方法を使用します。 [HttpUrlConnection](https://developer.android.com/reference/java/net/HttpURLConnection.html)、[Retrofit](https://github.com/square/retrofit)、[OkHttp ](https://github.com/square/okhttp)また、 'NetworkOnMainThreadException'を防ぐための非同期呼び出しを行います。次に、[jsonschema2pojo](http://www.jsonschema2pojo.org/)のようなコンバータを使用して、JsonをPojoクラスに変換します –

答えて

0

Volley,Retrofitのような任意のネットワークライブラリを使用して、サーバーからjson応答を取得できます。

私は、ボレーサンプルと改造サンプルのリンクを提供しています。

  1. ボレー

    Volley sample 1

    Volley library to handle to webservice

  2. 補強

    Retrofit sample

JSONのパースに来て210

も、これは便利ですgson

  1. Logan Square

    2. Gson

希望、高速な解析Logan Squareに使用されているいくつかのライブラリがあります:)

関連する問題