2016-12-05 11 views
0

私はAndroidとjavaで非常に新しいです。Android onclicklistener to JSONArray gridview

JSONには3つのデータがあります。

名、 画像、 URL

私は(URLデータからURLをクリックして開く)JSON配列項目のクリック可能にできるようにGridViewで私のフラグメントをしたいです。

どのようにしてアレイリストにonclicklistenerを追加できますか?

は、以下に設定私のコード

public class OneFragment extends Fragment{ 

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

    //Web api url 
    public static final String DATA_URL = "https://www.xxxx.com/test/json.php"; 

    //Tag values to read from json 
    public static final String TAG_IMAGE_URL = "image"; 
    public static final String TAG_NAME = "name"; 
    public static final String TAG_URL = "url"; 

    //GridView Object 
    private GridView gridView; 

    //ArrayList for Storing image urls and titles 
    private ArrayList<String> images; 
    private ArrayList<String> names; 
    private ArrayList<String> url; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


    } 

    private void getData(){ 
     //Showing a progress dialog while our app fetches the data from url 

     //final ProgressDialog loading = ProgressDialog.show(getActivity(), "Please wait...","Fetching data...",false,false); 

     //Creating a json array request to get the json from our api 
     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         //Dismissing the progressdialog on response 
         //loading.dismiss(); 

         //Displaying our grid 
         showGrid(response); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       } 
     ); 

     //Creating a request queue 
     RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); 
     //Adding our request to the queue 
     requestQueue.add(jsonArrayRequest); 
    } 


    private void showGrid(JSONArray jsonArray){ 
     //Looping through all the elements of json array 
     for(int i = 0; i<jsonArray.length(); i++){ 
      //Creating a json object of the current index 
      JSONObject obj = null; 
      try { 
       //getting json object from current index 
       obj = jsonArray.getJSONObject(i); 
       //getting image url and title from json object 
       images.add(obj.getString(TAG_IMAGE_URL)); 
       names.add(obj.getString(TAG_NAME)); 
       url.add(obj.getString(TAG_URL)); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
     //Creating GridViewAdapter Object 
     GridViewAdapter gridViewAdapter = new GridViewAdapter(getActivity(),images,names); 

     //Adding adapter to gridview 
     gridView.setAdapter(gridViewAdapter); 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     //setContentView(R.layout.activity_main); 

     View view = inflater.inflate(R.layout.fragment_json, container, false); 
     // Inflate the layout for this fragment 
     //return inflater.inflate(R.layout.fragment_one, container, false); 

     images = new ArrayList<>(); 
     names = new ArrayList<>(); 

     //Calling the getData method 
     getData(); 

     gridView = (GridView) view.findViewById(R.id.gridView); 

     return view; 


    } 

} 
+0

、以下のコードを参照してください。私が行ったことは、リスナーをアダプタに追加することです。リスナーを呼び出すと、リスナが実装されます。 –

答えて

1

GridViewからsetOnItemClickListenerです。 onItemClickの中で、アイテムがクリックされたときにクリックイベントを処理します。

はあなたのポストにGridViewAdapterクラスをしてください追加してくださいすることができ

private void showGrid(JSONArray jsonArray){ 
    ... 
    //Adding adapter to gridview 
    gridView.setAdapter(gridViewAdapter); 

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      String selectedImage = images.get(position); 
      String selectedName = names.get(position); 

      // do your stuff here 
     } 
    }); 
} 
+0

ありがとうございました。あなたのコードを試してみましたが、(i)でエラーが発生しました。 String selectedImage = images.get(i); 文字列selectedName = names.get(i); –

+0

それはタイプミスでした。編集を確認してください。 –

+0

は魅力的に機能します! ご協力いただきありがとうございます –