ListView中不显示数据

时间:2017-12-18 08:47:00

标签: android json listview android-fragments

美好的一天,我需要你的帮助,如何解决我在片段内JSON response显示ListView的问题。用户将选择一张卡片放入recycler viewer,并且每张卡片都包含内部数据。并且该数据将是API所需的参数,然后将返回JSON response。我想在listview

中显示回复

这是JSON响应

{
    "sl_details": {
        "error": false,
        "sl_row": [
            {
                "tr_type": "RJ",
                "credit": "sample 1",
                "tr_date": "2017-11-30"
            },
            {
                "tr_type": "RJ",
                "credit": "sample 2",
                "tr_date": "2017-11-15"
            },
            {
                "tr_type": "CKV",
                "credit": "sample 3",
                "tr_date": "2017-11-06"
        ]
    }
}

在recylcer查看器中按卡后检查Logcat是否成功获取数据

  

D / SLDTLFragment:获取SLDTL:RJ

     

D / SLDTLFragment:获取SLDTL:样本1

     

D / SLDTLFragment:获取SLDTL:2017-11-30

     

D / SLDTLFragment:获取SLDTL:RJ

     

D / SLDTLFragment:获取SLDTL:样本2

     

D / SLDTLFragment:获取SLDTL:2017-11-15

     

D / SLDTLFragment:获取SLDTL:CKV

     

D / SLDTLFragment:获取SLDTL:样本3

     

D / SLDTLFragment:获取SLDTL:2017-11-06

预期输出

enter image description here

实际输出

enter image description here

碎片代码

public class SLDTLFragment extends Fragment {

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

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

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

        String br_code = getArguments().getString("br_code");
        String client_id = getArguments().getString("client_id");
        String slc = getArguments().getString("slc");
        String sle = getArguments().getString("sle");
        String slt = getArguments().getString("slt");
        String ref_no = getArguments().getString("ref_no");

        Log.d("SLDTLFragment","br code: "+br_code);
        Log.d("SLDTLFragment","client id: "+client_id);
        Log.d("SLDTLFragment","slc: "+slc);
        Log.d("SLDTLFragment","sle: "+sle);
        Log.d("SLDTLFragment","slt: "+slt);
        Log.d("SLDTLFragment","ref_no: "+ref_no);

        // Inflate the layout for this fragment
        View listview =  inflater.inflate(R.layout.fragment_sldtl, container, false);

        SLDTLData sl_data= new SLDTLData();

        //final String[] sldtl_data= new String[]{br_code, client_id, slc, slt, ref_no};

        final String[] sldtl_data= new String[]{sl_data.getSldtl_trans_type(), sl_data.getSldtl_credit_debit(), sl_data.getSldtltrans_date()};

        //Passing data to getSLDTL method..
        getSLDTL(br_code, client_id, slc, slt, ref_no);

        ListAdapter sldtl= new SLDTLAdapter(getActivity().getApplicationContext(), sldtl_data); // I got stack up here

        ListView sl = (ListView)listview.findViewById(R.id.fragment_sldtl_mainListView);
        sl.setAdapter(sldtl);

        return listview;
    }

    private void getSLDTL(final String br_code, final String client_id,
                          final String slc, final String slt, final String ref_no){

        final List<SLDTLData> sl= new ArrayList<>();

        // Tag used to cancel the request
        String tag_string_req = "req_sldtl";

        StringRequest strReq= new StringRequest(Request.Method.POST,
                AppConfig.URL_SL_DETAILS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("SLDTLFragment onResponse", "SL Details Response: " + response.toString());

                try{
                    JSONObject jObj= new JSONObject(response.toString());
                    //                    boolean error= jObj.getBoolean("error");
                    if(!jObj.isNull("sl_details")){
                        JSONObject accountsObject = (JSONObject) jObj.get("sl_details");
                        // access individual json object thru jsonObject.get("FIELD_NAME")
                        Log.d("LoginActivity", "SL Details -error attribute             : " + accountsObject.get("error").toString());
                        JSONArray slArray = accountsObject.optJSONArray("sl_row");
                        // Check if its login data i.e. user present
                        if (slArray != null) {
                            JSONArray array = accountsObject.getJSONArray("sl_row");
                            // access individual json array thru jsonObject.getJSONArray("FIELD_NAME")
                            Log.d("SLDTLFragment", "SL Details -sl_row array           : " + accountsObject.getJSONArray("sl_row").toString());

                            for (int index=0; index < array.length(); index++) {

                                SLDTLData sl_data= new SLDTLData();

                                JSONObject object = (JSONObject)array.get(index);

                                String tr_type= object.getString("tr_type");
                                // Log.d("SLDTLFragment", "SL Details -sle_code attribute       : " + tr_type);
                                String credit_debit= object.getString("credit");
                                // Log.d("SLDTLFragment", "SL Details -sle_code attribute       : " + credit_debit);
                                String tr_date= object.getString("tr_date");
                                // Log.d("SLDTLFragment", "SL Details -sle_code attribute       : " + tr_date);

                                // Log.d("SLDTLFragment", "SL Details -------------------------------------------------------------------------------------");

                                sl_data.setSldtl_trans_type(tr_type);
                                sl_data.setSldtl_credit_debit(credit_debit);
                                sl_data.setSldtltrans_date(tr_date);

                                sl.add(sl_data);

                                sl_data.getSldtl_trans_type();
                                sl_data.getSldtl_credit_debit();
                                sl_data.getSldtltrans_date();

                                Log.d("SLDTLFragment", "Get SLDTL: "+ sl_data.getSldtl_trans_type());
                                Log.d("SLDTLFragment", "Get SLDTL: "+ sl_data.getSldtl_credit_debit());
                                Log.d("SLDTLFragment", "Get SLDTL: "+ sl_data.getSldtltrans_date());
                            }
                        }
                    }else{
                        // a new JSON string that doesn't have sl_summ as member variable so display it and write new handler code
                        Log.d("SLDTLFragment", "Unknown JSON String          : " + jObj.toString());
                    }
                }catch(JSONException e){
                    e.printStackTrace();
                }

            }//end onResponse
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Changepass Error: " + error.getMessage());
            }
        }){
            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("br_code", br_code);
                params.put("client_id", client_id);
                params.put("slc_code", slc);
                params.put("slt_code", slt);
                params.put("ref_number", ref_no);

                return params;
            }
        };
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }
}

适配器代码

public class SLDTLAdapter extends ArrayAdapter<String> {

    public SLDTLAdapter(@NonNull Context context, String[] sldtl_data) {
        super(context, R.layout.fragment_sldtl_row, sldtl_data);
    }

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

        LayoutInflater li= LayoutInflater.from(getContext());
        View v= li.inflate(R.layout.fragment_sldtl_row, parent, false);

        String sldtlItem= getItem(position);
        TextView sl_type= (TextView) v.findViewById(R.id.sl_desc);
        TextView trans_date= (TextView) v.findViewById(R.id.transDate);
        TextView trans_amount = (TextView) v.findViewById(R.id.trans_amount);
        TextView debitCredit= (TextView) v.findViewById(R.id.debitCredit);

        trans_date.setText(sldtlItem); // I want this to be the actual data in json response
        debitCredit.setText("Credit"); // I want this to be the actual data in json response

        return v;
    }

}

4 个答案:

答案 0 :(得分:0)

在充气时,请尝试将ListView附加到container。像这样:

View listview =  inflater.inflate(R.layout.fragment_sldtl, container, true);

最后一个参数设置为true而不是false

答案 1 :(得分:0)

而不是

ListAdapter sldtl= new SLDTLAdapter(getActivity().getApplicationContext(), sldtl_data);

使用

SLDTLAdapter sldtl= new SLDTLAdapter(getActivity().getApplicationContext(), sldtl_data);

答案 2 :(得分:0)

获得响应后,在for循环中,添加列表中的所有项目通知适配器更新列表。下面是代码:

for(){ sldtl.add(sldtl_data); } yourAdpterName.notifyDataSetChanged();`

答案 3 :(得分:0)

首先你对这个变量感到困惑&#34; sldtl_data&#34;

final String[] sldtl_data= new String[]{sl_data.getSldtl_trans_type(), sl_data.getSldtl_credit_debit(), sl_data.getSldtltrans_date()};

这里s1_data.getSldtl_trans_type()返回空,因为没有设置数据到sl_data。

第二件事:在这里你将数据设置为sldtl_data变量,即SLDLData Class。

for (int index=0; index < array.length(); index++) {
SLDTLData sldtl_data= new SLDTLData();
..
}

注意:两个对象都是同一个名称,但不是问题

<强>解决方案

1.全球推荐

final List<SLDTLData> sldtl= new ArrayList<SLDTLData>(); 

删除

final List<SLDTLData> sldtl= new ArrayList<>();  from getSLDTL method.

2.更改您的适配器类

public class SLDTLAdapter extends ArrayAdapter<String> {
private List<Datas> mDataset;

public SLDTLAdapter(@NonNull Context context, String[] sldtl_data) {
    super(context, R.layout.fragment_sldtl_row, sldtl_data);
}

public class SLDTLAdapter extends ArrayAdapter<SLDTLData> {
private ArrayList<SLDTLData> dataset;
Context mContext;

public SLDTLAdapter(@NonNull Context context, ArrayList<SLDTLData> data) {
    super(context, R.layout.fragment_sldtl_row, data);
    this.dataSet = data;
    this.mContext=context;
}

在getview()方法中

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

SLDTLData sld_data = getItem(position);
trans_date.setText(sld_data.getSldtltrans_date());
...
}
SLDTLFragment类中的

最后更改此代码

ListAdapter sldtl= new SLDTLAdapter(getActivity().getApplicationContext(), sldtl_data);

SLDTLAdapter sldtladapter = new SLDTLAdapter(getActivity().getApplicationContext(), sldtl); // sldtl is your arraylist.

然后移除

final String[] sldtl_data= new String[]{sl_data.getSldtl_trans_type(), sl_data.getSldtl_credit_debit(), sl_data.getSldtltrans_date()};

此代码。

<强>已更新/被编辑

更改适配器

public class SLDTLAdapter extends ArrayAdapter<SLDTLData> {
private ArrayList<SLDTLData> dataset;
int layoutresourceid;
Context mContext;

public SLDTLAdapter(@NonNull Context context,int LayoutResourceId, ArrayList<SLDTLData> data) {
    super(context, R.layout.fragment_sldtl_row, data);
    this.dataSet = data;
    this.mContext=context;
    this.layoutresourceid = LayoutResourceId;
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent){


LayoutInflater li= LayoutInflater.from((Activity)mContext);
//LayoutInflater li= LayoutInflater.from(mContext);
    View v= li.inflate(layoutresourceid, parent, false);

    TextView sl_type= (TextView) v.findViewById(R.id.sl_desc);
    TextView trans_date= (TextView) v.findViewById(R.id.transDate);
    TextView trans_amount = (TextView) v.findViewById(R.id.trans_amount);
    TextView debitCredit= (TextView) v.findViewById(R.id.debitCredit);

SLDTLData sld_data = getItem(position);
trans_date.setText(sld_data.getSldtltrans_date());
sl_type.setText(sld_data.getSldtl_trans_type()); 
trans_amount.setText(sld_data.getSldtl_credit_debit());
debitCredit.setText("Credit"); 

 return v;

}

片段

SLDTLAdapter sldtladapter = new SLDTLAdapter(getActivity().getApplicationContext(), R.Layout.fragment_sldtl_row, sldtl); // sldtl is your arraylist.

希望它有所帮助。!