数据未显示在ListView中,但在打开信使应用程序和交互数据时将显示

时间:2017-12-27 03:43:06

标签: android json listview

美好的一天。我想问一下为什么我的listView在从服务器获取JSON响应后不显示数据。我认为数据没有获取但是当我看到日志猫时,它已成功获取但未在我的listView中显示。

但是当我在messenger聊天时,我回到了我的应用程序,显示了数据。起初,我不知道数据突然出现的原因。所以我想也许它被推迟了。所以我再试一次,等待几分钟但没有任何反应没有显示数据。希望你能帮我解决这个问题。

我等了几分钟,这就是结果。

enter image description here logcat的

12-25 21:00:48.809 7264-7264/com.sample.test D/MyAdapter: Cardview Elements
12-25 21:00:48.809 7264-7264/com.sample.test D/CardView Clicked: br code:  0
12-25 21:00:48.809 7264-7264/com.sample.test D/CardView Clicked: mem id:   202
12-25 21:00:48.809 7264-7264/com.sample.test D/CardView Clicked: sle code: 11
12-25 21:00:48.809 7264-7264/com.sample.test D/CardView Clicked: slc code: 13
12-25 21:00:48.809 7264-7264/com.sample.test D/CardView Clicked: slt code: 99
12-25 21:00:48.809 7264-7264/com.sample.test D/CardView Clicked: ref num:  531
12-25 21:00:48.822 7264-7269/com.sample.test I/art: Do partial code cache collection, code=23KB, data=30KB
12-25 21:00:48.823 7264-7269/com.sample.test I/art: After code cache collection, code=23KB, data=30KB
12-25 21:00:48.823 7264-7269/com.sample.test I/art: Increasing code cache capacity to 128KB
12-25 21:00:48.836 7264-7264/com.sample.test D/InputTransport: Input channel constructed: fd=66
12-25 21:00:48.836 7264-7264/com.sample.test D/ViewRootImpl@9eefd6b[Toast]: setView = android.widget.LinearLayout{7bab0c8 V.E...... ......I. 0,0-0,0} touchMode=true
12-25 21:00:48.852 7264-7264/com.sample.test D/AbsListView: Get MotionRecognitionManager
12-25 21:00:48.855 7264-7264/com.sample.test D/MotionRecognitionManager: mSContextService = com.samsung.android.hardware.context.ISemContextService$Stub$Proxy@e0baee3
12-25 21:00:48.858 7264-7264/com.sample.test D/MotionRecognitionManager: motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@d4d0e0
12-25 21:00:48.858 7264-7264/com.sample.test D/MotionRecognitionManager: motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@d4d0e0
12-25 21:00:48.861 7264-7347/com.sample.test D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-25 21:00:48.863 7264-7347/com.sample.test I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-25 21:00:48.863 7264-7347/com.sample.test I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-25 21:00:48.901 7264-7281/com.sample.test D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [705x132]-format:1
12-25 21:00:48.909 7264-7264/com.sample.test D/AbsListView:  onsize change 
12-25 21:00:48.914 7264-7264/com.sample.test V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@440563f nm : com.sample.test ic=null
12-25 21:00:48.914 7264-7264/com.sample.test I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
12-25 21:00:48.917 7264-7264/com.sample.test D/InputTransport: Input channel constructed: fd=80
12-25 21:00:48.917 7264-7264/com.sample.test D/InputTransport: Input channel destroyed: fd=81
12-25 21:00:48.917 7264-7264/com.sample.test D/ViewRootImpl@9eefd6b[Toast]: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
12-25 21:00:50.256 7264-7264/com.sample.test D/SLDTLFragment onResponse: SL Details Response: {"sl_details":{"error":false,"sl_row":[{"tr_type":"RJ","credit":"94.17","tr_date":"2017-12-11"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-11-30"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-11-15"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-10-31"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-10-15"},{"tr_type":"GJ","credit":"0.00","tr_date":"2017-10-09"}]}}
12-25 21:00:50.256 7264-7264/com.sample.test D/LoginActivity: SL Details -error attribute             : false
12-25 21:00:50.257 7264-7264/com.sample.test D/SLDTLFragment: SL Details -sl_row array           : [{"tr_type":"RJ","credit":"94.17","tr_date":"2017-12-11"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-11-30"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-11-15"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-10-31"},{"tr_type":"RJ","credit":"94.17","tr_date":"2017-10-15"},{"tr_type":"GJ","credit":"0.00","tr_date":"2017-10-09"}]
12-25 21:00:50.826 7264-7264/com.sample.test D/ViewRootImpl@9eefd6b[Toast]: dispatchDetachedFromWindow
12-25 21:00:50.846 7264-7264/com.sample.test D/InputTransport: Input channel destroyed: fd=66

当我打开信使

记录猫

12-25 20:46:26.498 30768-30768/com.sample.test D/ViewRootImpl@19e90b4[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
12-25 20:46:26.612 30768-30768/com.sample.test D/InputTransport: Input channel destroyed: fd=80

点击聊天框后

记录猫

12-25 21:01:19.336 7264-7264/com.sample.test D/ViewRootImpl@8eff987[MainActivity]: MSG_RESIZED: ci=Rect(0, 72 - 0, 750) vi=Rect(0, 72 - 0, 750) or=1
12-25 21:01:19.370 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{49a666a V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/sl_desc}
12-25 21:01:19.370 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{ee175b V.ED..... ......ID 0,0-0,0 #7f0800c5 app:id/trans_amount}
12-25 21:01:19.370 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{bdfdbf8 V.ED..... ......ID 0,0-0,0 #7f0800c4 app:id/transDate}
12-25 21:01:19.379 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{6d7c10d V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/sl_desc}
12-25 21:01:19.379 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{3bd58c2 V.ED..... ......ID 0,0-0,0 #7f0800c5 app:id/trans_amount}
12-25 21:01:19.379 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{b616d3 V.ED..... ......ID 0,0-0,0 #7f0800c4 app:id/transDate}
12-25 21:01:19.387 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{b2c5f3c V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/sl_desc}
12-25 21:01:19.387 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{b5104c5 V.ED..... ......ID 0,0-0,0 #7f0800c5 app:id/trans_amount}
12-25 21:01:19.387 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{6513e1a V.ED..... ......ID 0,0-0,0 #7f0800c4 app:id/transDate}
12-25 21:01:19.395 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{4d7b327 V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/sl_desc}
12-25 21:01:19.395 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{f9759d4 V.ED..... ......ID 0,0-0,0 #7f0800c5 app:id/trans_amount}
12-25 21:01:19.396 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{ec8077d V.ED..... ......ID 0,0-0,0 #7f0800c4 app:id/transDate}
12-25 21:01:19.402 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{a0c87be V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/sl_desc}
12-25 21:01:19.402 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{3a5e01f V.ED..... ......ID 0,0-0,0 #7f0800c5 app:id/trans_amount}
12-25 21:01:19.402 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{ee40f6c V.ED..... ......ID 0,0-0,0 #7f0800c4 app:id/transDate}
12-25 21:01:19.403 7264-7264/com.sample.test D/AbsListView:  onsize change 

再次打开应用程序后

12-25 21:03:45.502 7264-7264/com.sample.test I/onResume: User is active. cancel timer
12-25 21:03:45.531 7264-7281/com.sample.test D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
12-25 21:03:45.550 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{1156b90 V.ED..... ......ID 24,53-64,106 #7f0800a3 app:id/sl_desc}
12-25 21:03:45.550 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{e44dc89 V.ED..... ......ID 928,49-1008,98 #7f0800c5 app:id/trans_amount}
12-25 21:03:45.550 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{979d48e V.ED..... ......ID 24,106-204,155 #7f0800c4 app:id/transDate}
12-25 21:03:45.551 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{ac82ccb V.ED..... ......ID 24,53-64,106 #7f0800a3 app:id/sl_desc}
12-25 21:03:45.551 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{4e31ca8 V.ED..... ......ID 928,49-1008,98 #7f0800c5 app:id/trans_amount}
12-25 21:03:45.551 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{95e93c1 V.ED..... ......ID 24,106-204,155 #7f0800c4 app:id/transDate}
12-25 21:03:45.551 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{a2313f2 V.ED..... ......ID 24,53-64,106 #7f0800a3 app:id/sl_desc}
12-25 21:03:45.551 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{2f6a43 V.ED..... ......ID 928,49-1008,98 #7f0800c5 app:id/trans_amount}
12-25 21:03:45.551 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{962d8c0 V.ED..... ......ID 24,106-204,155 #7f0800c4 app:id/transDate}
12-25 21:03:45.552 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{7ee3cb5 V.ED..... ......ID 24,53-64,106 #7f0800a3 app:id/sl_desc}
12-25 21:03:45.552 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{fed2f4a V.ED..... ......ID 928,49-1008,98 #7f0800c5 app:id/trans_amount}
12-25 21:03:45.552 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{9a1debb V.ED..... ......ID 24,106-204,155 #7f0800c4 app:id/transDate}
12-25 21:03:45.559 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{c7c4184 V.ED..... ......ID 24,53-64,106 #7f0800a3 app:id/sl_desc}
12-25 21:03:45.560 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{cf64d6d V.ED..... ......ID 928,49-1008,98 #7f0800c5 app:id/trans_amount}
12-25 21:03:45.561 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{fc45da2 V.ED..... ......ID 24,106-204,155 #7f0800c4 app:id/transDate}
12-25 21:03:45.576 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans type: android.support.v7.widget.AppCompatTextView{2ac768f V.ED..... ......ID 0,0-0,0 #7f0800a3 app:id/sl_desc}
12-25 21:03:45.576 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans amount: android.support.v7.widget.AppCompatTextView{1283d1c V.ED..... ......ID 0,0-0,0 #7f0800c5 app:id/trans_amount}
12-25 21:03:45.576 7264-7264/com.sample.test D/SLDTL Adapter GetView: Trans date: android.support.v7.widget.AppCompatTextView{68bd25 V.ED..... ......ID 0,0-0,0 #7f0800c4 app:id/transDate}
12-25 21:03:45.577 7264-7264/com.sample.test D/AbsListView:  onsize change 
12-25 21:03:45.600 7264-7264/com.sample.test D/ViewRootImpl@8eff987[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
12-25 21:03:45.601 7264-7264/com.sample.test V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@71710c6 nm : com.sample.test ic=null
12-25 21:03:45.601 7264-7264/com.sample.test I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
12-25 21:03:45.611 7264-7264/com.sample.test D/InputTransport: Input channel constructed: fd=74

数据在我的listView中成功显示。

enter image description here

这是 SLDTLFragment.java

的代码
public class SLDTLFragment extends Fragment {

    private List<SLDTLData> sldtlDataList=  new ArrayList<SLDTLData>();
    private SLDTLAdapter adapter;
    private ProgressDialog pDialog;

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

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

        pDialog = new ProgressDialog(this.getContext());
        pDialog.setCancelable(false);
    }

    @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 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);

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

        View rootView= inflater.inflate(R.layout.fragment_sldtl, container, false);

        //List
        ListView listView= (ListView)rootView.findViewById(R.id.fragment_sldtl_ListView);

        sldtlDataList= new ArrayList<>();
        adapter= new SLDTLAdapter(getActivity(), sldtlDataList);
        listView.setAdapter(adapter);

        return rootView;
    }

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

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

        pDialog.setMessage("Retrieving sl details ...");
        showDialog();

        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());
                hideDialog();

                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 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 -------------------------------------------------------------------------------------");

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

                                sldtlDataList.add(data);

                                data.getSldtl_trans_type();
                                data.getSldtl_credit_debit();
                                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, "SLDTL response 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);
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }
    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

SLDTLAdapter.java

public class SLDTLAdapter extends BaseAdapter {

        private Activity activity;
        private LayoutInflater inflater;
        private List<SLDTLData> sl_data;


     public SLDTLAdapter(Activity activity, List<SLDTLData>sl_data){
         this.activity= activity;
         this.sl_data= sl_data;
     }

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

        Log.d("SLDTL Adapter GetView","");

//        if (inflater == null)     //
//            inflater = (LayoutInflater) activity
//                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//
//        if (ConvertView == null)
//            ConvertView = inflater.inflate(R.layout.fragment_sldtl_row, null);

        ConvertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_sldtl_row, null);

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

        SLDTLData sl= sl_data.get(position);

        sl_type.setText(sl.getSldtl_trans_type());
        trans_amount.setText(sl.getSldtl_credit_debit());
        trans_date.setText(sl.getSldtltrans_date());


        Log.d("SLDTL Adapter GetView","Trans type: "    +sl_type);
        Log.d("SLDTL Adapter GetView","Trans amount: "  +trans_amount);
        Log.d("SLDTL Adapter GetView","Trans date: "    +trans_date);

        return ConvertView;
    }

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

    @Override
    public Object getItem(int i) {
        return sl_data.get(i);
    }

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

}

2 个答案:

答案 0 :(得分:1)

在将项目添加到列表中后添加adapter.notifyDatasetChanged,如

for (int index=0; index < array.length(); index++) {
                                SLDTLData 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 -------------------------------------------------------------------------------------");

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

                                sldtlDataList.add(data);

                                data.getSldtl_trans_type();
                                data.getSldtl_credit_debit();
                                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());
                            }
adapter.notifyDataSetChanged(); //<----- this will refresh list immediately

答案 1 :(得分:0)

这种问题出现在线程中,因为你在前台线程的listView中设置你的适配器而你正在后台线程中的List中添加你的数据所以解决这类问题就是设置你的listView适配器获取数据后如下:

// other stuf before json calling
    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());
                    hideDialog();

                    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 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 -------------------------------------------------------------------------------------");

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

                                    sldtlDataList.add(data);

                                    data.getSldtl_trans_type();
                                    data.getSldtl_credit_debit();
                                    data.getSldtltrans_date();
                                    callListAdapter();

    //                                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, "SLDTL response 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;
                }
            };

        public void callListAdapter(){
        adapter= new SLDTLAdapter(getActivity(), sldtlDataList);
            listView.setAdapter(adapter);
            }
// other stuf

试试吧。如果有任何问题只是评论。谢谢。