获取数据并附加后,ListView不更新

时间:2014-09-18 13:30:37

标签: android android-listfragment android-json

我有一个ListFragment,在类文件中,我检索数据库数据并将其转换为JSON。它工作正常,我甚至记录结果,它显示了结果,但没有任何事情发生在日志之后,它只是不会告诉我任何事情。我的代码如下:

public void loadItems(){
        SharedPreferences pref = getActivity().getSharedPreferences("myId", Context.MODE_PRIVATE);
        String getId = pref.getString("int1", "no data");
        Uri uri = Uri.parse("content://" + getString(R.string.AUTORITY_ORDER) + "/orders");
        final Cursor cursor = getActivity().getContentResolver().query(uri, null, "order_id = " + getId, null, null);
        JSONArray array = new JSONArray();
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            while(!cursor.isAfterLast()){
                JSONObject object = new JSONObject();
                try {
                    object.put("destination_address", cursor.getString(0));
                    object.put("destination_code", cursor.getString(1));
                    object.put("destination_email", cursor.getString(2));
                    object.put("destination_name", cursor.getString(3));
                    object.put("destination_phone", cursor.getString(4));
                    object.put("_id", cursor.getString(5));
                    object.put("order_delivered", cursor.getString(6));
                    object.put("order_id", cursor.getString(7));
                    object.put("order_picked", cursor.getString(8));
                    object.put("order_type", cursor.getString(9));
                    object.put("source_address", cursor.getString(10));
                    object.put("source_code", cursor.getString(11));
                    object.put("source_email", cursor.getString(12));
                    object.put("source_name", cursor.getString(13));
                    object.put("source_number", cursor.getString(14));
                    object.put("source_phone", cursor.getString(15));
                    array.put(object);
                    Log.d("ARRAYTEST", String.valueOf(array.length()));
                    cursor.moveToNext();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            parseJsonFeed(array);
        }
    }

    public void parseJsonFeed(JSONArray array){
        Log.d("jsonArr", array.toString());
        try {

            JSONObject itemss = new JSONObject(array.toString());
            Log.d("DEtailCheck", itemss.toString());
            LinearLayout layout1, layout2;
            layout1 = (LinearLayout) getActivity().findViewById(R.id.names1);
            layout2 = (LinearLayout) getActivity().findViewById(R.id.names2);
            SharedPreferences pref = getActivity().getSharedPreferences("myCode", Context.MODE_PRIVATE);
            Editor edit = pref.edit();
            edit.putString("key1", itemss.getString("source_code"));
            edit.putString("key11", itemss.getString("destination_code"));
            edit.putString("key5", itemss.getString("order_picked"));
            edit.commit();
            String sourceName, sourceAdrress, destinationName, destinationAddress;
            sourceAdrress = itemss.getString("source_address");
            sourceName = itemss.getString("source_name");
            destinationAddress = itemss.getString("destination_address");
            destinationName = itemss.getString("destination_name");
            Log.d("ddsList", sourceName + " " + sourceAdrress + " " + destinationName + " " + destinationAddress);
            TextView txt1 = (TextView) getActivity().findViewById(R.id.name2);
            TextView txt2 = (TextView) getActivity().findViewById(R.id.address2);
            TextView txt3 = (TextView) getActivity().findViewById(R.id.name1);
            TextView txt4 = (TextView) getActivity().findViewById(R.id.address1);

            if (sourceAdrress.isEmpty()) {
                layout1.setVisibility(View.GONE);
                txt1.setText(destinationName);
                txt2.setText(destinationAddress);
            } else if (destinationAddress.isEmpty()) {
                layout2.setVisibility(View.GONE);
                txt3.setText(sourceName);
                txt4.setText(sourceAdrress);
            } else {
                txt1.setText(destinationName);
                txt2.setText(destinationAddress);
                txt3.setText(sourceName);
                txt4.setText(sourceAdrress);
            }
            String check = itemss.getString("order_picked");
            String type = itemss.getString("order_type");
            Log.d("ORDER_TYPE:", type + " " + check);
            if (type.matches("1")) {
                Button change = (Button) getActivity().findViewById(R.id.buttonClose);
                change.setText("Delivery");
            } else if (type.matches("2")) {
                Button change = (Button) getActivity().findViewById(R.id.buttonClose);
                change.setText("PickUp");
            } else if (check.matches("true") && type.matches("3")) {
                Button change = (Button) getActivity().findViewById(R.id.buttonClose);
                change.setText("Close Delivery");
            }

        } catch (JSONException e) {
            // TODO: handle exception
        }

    }

其中 Log.d(“jsonArr”,array.toString()); 是出现的日志,之后没有任何事情发生,没有错误,没有警告。

阵列是这样的:

[{"order_delivered":"false","source_number":"540DB0FA07A1F","destination_phone":"787438743","order_id":"5","source_phone":"7347477","destination_email":"hghsh@nff.com","source_code":"963483","order_picked":"false","destination_code":"711652","destination_address":"73, Layi-Oyekanmi shhsje","source_email":"hdhhds@hhff.com","_id":"2","destination_name":"hjjfdj","source_name":"gshd","source_address":"Sabo, Yaba hjhgshs","order_type":"3"}]

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

似乎抛出了JSONException。

替换

// TODO: handle exception

有这样的事情:

Log.e("ARRAYTEST", "Error", e);

然后运行您的应用程序并检查日志。