2016-07-15 13 views
0

出力を示していません。私はクラスGetUserを作成しました:JSON配列はcorretlyリストビューで

package com.gmakerorganisation.glocator.Fragments; 

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

import com.android.volley.RequestQueue; 
import com.gmakerorganisation.glocator.Config; 
import com.gmakerorganisation.glocator.GetAllUsers; 
import com.gmakerorganisation.glocator.R; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 


public class Sendrequest extends Fragment { 
    private RequestQueue requestQueue; 
    ArrayList<HashMap<String, String>> UserList; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_USERNAME = "username"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_PHONE = "phone"; 
    private static final String TAG_PROFILE = "profile"; 
    GetAllUsers webreq = new GetAllUsers(); 
    ListView listView; 
    public Sendrequest() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v=inflater.inflate(R.layout.fragment_sendrequest, container, false); 
     listView=(ListView)v.findViewById(R.id.list); 
     new GetStudents().execute(); 

     return v; 
    } 

    private class GetStudents extends AsyncTask<Void, Void, Void> { 

     // Hashmap for ListView 

     ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 


     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      GetAllUsers webreq = new GetAllUsers(); 

      // Making a request to url and getting response 
      String jsonStr = webreq.makeWebServiceCall(Config.USERS_URL, GetAllUsers.GET); 

      Log.d("Response: ", "> " + jsonStr); 

      UserList = ParseJSON(jsonStr); 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      /** 
      * Updating parsed JSON data into ListView 
      li * */ 

      ListAdapter adapter = new SimpleAdapter(
        getActivity().getApplicationContext(), UserList, 
        R.layout.list_item, new String[]{TAG_NAME, TAG_PHONE}, new int[]{R.id.name, 
        R.id.mobile}); 
      listView.setAdapter(adapter); 
     } 

    } 

    private ArrayList<HashMap<String, String>> ParseJSON(String json) { 
     if (json != null) { 
      try { 
       // Hashmap for ListView 
       ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>(); 



       // Getting JSON Array node 
       JSONArray userrs = new JSONArray(json); 

       // looping through All Students 
       for (int i = 0 ; i < userrs.length(); i++) { 
        JSONObject c = userrs.getJSONObject(i); 

        String id = c.getString(TAG_ID); 
        String username = c.getString(TAG_USERNAME); 
        String name = c.getString(TAG_NAME); 
        String phone = c.getString(TAG_PHONE); 
        String profile = c.getString(TAG_PROFILE); 



        // tmp hashmap for single student 
        HashMap<String, String> user = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        user.put(TAG_ID, id); 
        user.put(TAG_USERNAME, username); 
        user.put(TAG_NAME, name); 
        user.put(TAG_PHONE, phone); 
        user.put(TAG_PROFILE, profile); 

        // adding student to students list 
        studentList.add(user); 
       } 
       return studentList; 
      } catch (JSONException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      return null; 
     } 
    } 
} 

私はのUserListで正しいデータを取得していますので、私はリストビューに問題があると思います。マイR.layout.list_item

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <!-- Name Label --> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:text="Name" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 


    <!-- Mobile number label --> 
    <TextView 
     android:id="@+id/mobile" 
     android:layout_width="220dp" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:text="Phone: " /> 

</LinearLayout> 

そして、私のJSONはこれです:

UserList = ParseJSON(jsonStr); 
mAdapter.notifyDataSetChanged(); 

あなたがそのように初期化アダプター付:あなたもあなたのアダプタを更新するために、あなたのリストを更新

[{"id":"1","username":"gmaker","name":"SHUBHAM SHARMA","phone":"9711979977","profile":"http:\/\/glocator.esy.es\/profilepicture\/1."},{"id":"4","username":"somnath","name":"","phone":"9582223881","profile":""},{"id":"7","username":"shitij","name":"","phone":"9650154839","profile":""}]   
+0

あなたが尋ねていることを理解するのは本当に難しいですか、問題はどういうものなのですか。 –

答えて

0

使用mAdapter.notifyDataSetChanged()

mAdapter = new SimpleAdapter(
        getActivity().getApplicationContext(), UserList, 
        R.layout.list_item, new String[]{TAG_NAME, TAG_PHONE}, new int[]{R.id.name, 
        R.id.mobile}); 
listView.setAdapter(adapter); 
+0

私は基本アダプターなどを拡張できないためにフラグメントを使用しているため、あなたはもっと詳しく説明できますか?だから私のリスト全体を表示する方法はありますか – Shubham