使用Volley在ListView中解析Json数据

时间:2018-01-22 07:02:34

标签: listview android-volley android-json

我在listview中获取了数据,但没有按照我期望的方式获取。我想获取listview中的数据,但它正在使用上面的静态视图like this..

制作列表

任何想法,我怎么得到它? 是否还与布局有关?

这是我的主要活动:

private List<UserInfo> list = new ArrayList<UserInfo>();
private ListView listView;
private Adapter adapter;

    listView = findViewById(R.id.list_view);
    adapter = new Adapter(this, list);
    listView.setAdapter(adapter);

    textView1 = findViewById(R.id.Number1);
    textView2 = findViewById(R.id.ServiceName1);
    textView3 = findViewById(R.id.Amount1);

    requestQueue = Volley.newRequestQueue(this);

    jsonParse();

  }

    public void jsonParse() {

    final String fetchurl = ("https://xelwel.com.np/hamrosewaapp/api/get_organization_list");

    final StringRequest request = new StringRequest(Request.Method.POST, fetchurl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONObject jsonObject = new JSONObject(response);

                JSONArray jsonArray = jsonObject.getJSONArray("org_list");

                UserInfo info = new UserInfo();

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject patient = jsonArray.getJSONObject(i);

                    info.setId(patient.getString("orga_orgid"));
                    info.setName(patient.getString("orga_organame"));

                    list.add(info);
                    progressbar.setVisibility(View.GONE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            adapter.notifyDataSetChanged();
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("api_key", "123456789");
            return params;
        }
    };

    requestQueue.add(request);
}

这是我的Adapter类:

private Activity activity;
private LayoutInflater inflater;
private List<UserInfo> DataList;

public Adapter(Activity activity, List<UserInfo> dataitem) {

    this.activity = activity;
    this.DataList = dataitem;
}

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

@Override
public Object getItem(int location) {
    return DataList.get(location);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.item_layout, null);

    TextView id = convertView.findViewById(R.id.Number1);
    TextView name = convertView.findViewById(R.id.ServiceName1);

    UserInfo m = DataList.get(position);

    id.setText(m.getId());
    name.setText(m.getName());

    return convertView;
}

这是我的项目布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/Textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:padding="10dp"
    android:text="Civil Service Hospital"
    android:textColor="#000"
    android:textSize="25dp"
    android:textStyle="normal" />

<TextView
    android:id="@+id/SerialNo."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Textview"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="10dp"
    android:text="S.No."
    android:textColor="#000"
    android:textSize="20dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/ServiceName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/SerialNo."
    android:layout_alignBottom="@+id/SerialNo."
    android:layout_centerHorizontal="true"
    android:paddingRight="20dp"
    android:text="Service Name"
    android:textColor="#000"
    android:textSize="20dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/Amount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/ServiceName"
    android:layout_alignBottom="@+id/ServiceName"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_toEndOf="@+id/ServiceName"
    android:layout_toRightOf="@+id/ServiceName"
    android:paddingBottom="10dp"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:text="Amount"
    android:textColor="#000"
    android:textSize="20dp"
    android:textStyle="bold" />

    <TextView
        android:id="@+id/Number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/SerialNo."
        android:layout_alignBaseline="@+id/ServiceName1"
        android:layout_alignBottom="@+id/ServiceName1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:paddingLeft="20dp"
        android:text=""
        android:textColor="#000"
        android:textSize="15dp" />

<TextView
    android:id="@+id/ServiceName1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ServiceName"
    android:layout_centerHorizontal="true"
    android:padding="10dp"
    android:text=""
    android:textColor="#000"
    android:textSize="15dp" />

<TextView
    android:id="@+id/Amount1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/Amount"
    android:layout_alignRight="@+id/Amount"
    android:layout_below="@+id/Amount"
    android:layout_marginEnd="11dp"
    android:layout_marginRight="11dp"
    android:padding="10dp"
    android:text=""
    android:textColor="#000"
    android:textSize="15dp" />

请犯下我用这段代码搞错的错误......

2 个答案:

答案 0 :(得分:0)

检查此MainActivity。 UserInfo的对象应该在For循环中创建。

final StringRequest request = new StringRequest(Request.Method.POST, 
fetchurl, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        try {
            JSONObject jsonObject = new JSONObject(response);

            JSONArray jsonArray = jsonObject.getJSONArray("org_list");



            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject patient = jsonArray.getJSONObject(i);
                UserInfo info = new UserInfo();
                info.setId(patient.getString("orga_orgid"));
                info.setName(patient.getString("orga_organame"));

                list.add(info);
                progressbar.setVisibility(View.GONE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        adapter.notifyDataSetChanged();
    }

}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("api_key", "123456789");
        return params;
    }
};

requestQueue.add(request);

答案 1 :(得分:0)

最后我自己得到了答案。我只是检查并将静态视图放在mainActivitylayout和item_layout中的动态视图中,它就会发生。下面是我使用的mainlayout xml文件:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <TextView
        android:id="@+id/Number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/SerialNo."
        android:layout_alignBaseline="@+id/ServiceName1"
        android:layout_alignBottom="@+id/ServiceName1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:paddingLeft="20dp"
        android:text=""
        android:textColor="#000"
        android:textSize="15dp" />

    <TextView
    android:id="@+id/ServiceName1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ServiceName"
    android:layout_centerHorizontal="true"
    android:padding="10dp"
    android:text=""
    android:textColor="#000"
    android:textSize="15dp" />

   <TextView
    android:id="@+id/Amount1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/Amount"
    android:layout_alignRight="@+id/Amount"
    android:layout_below="@+id/Amount"
    android:layout_marginEnd="11dp"
    android:layout_marginRight="11dp"
    android:padding="10dp"
    android:text=""
    android:textColor="#000"
    android:textSize="15dp" />

  </RelativeLayout>

谢谢@Chandan Kushwaha支持我的问题...