将动态项添加到linearlayout会显示逆向顺序android

时间:2016-05-11 06:17:07

标签: android android-layout

我将动态视图扩展为线性布局,但反向显示。

 try {
            JSONArray jsonArray = new JSONArray(tooteet.getMeasureJson());
            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                Measure measureData = new Measure();
                measureData.id = jsonObject.optString("id");;
                measureData.tooteetId = jsonObject.optString("tooteetId");
                measureData.laneId = jsonObject.optString("laneId");
                measureData.startDate = jsonObject.optString("startDate");
                measureData.endDate = jsonObject.optString("endDate");
                measureData.description = jsonObject.optString("text");
                measureData.value = jsonObject.optDouble("value");
                measureData.measureTypeId = jsonObject.optInt("measureTypeId");
                measureData.description = jsonObject.optString("text");
                measureData.isTimeSet =jsonObject.optBoolean("isTimeSet");
                mMeasureList.add(measureData);
                addMeasureView(measureData, i);
            }
        } catch (Exception e) {
            Log.d("FeedMeasure", "Exception: "+e.toString());
        }

我获取tooteet.getMeasureJson()的日志值是

onCreate -- tooteet.getMeasureJson(): [{"id":"3fb2af41-201d-4aca-9479-42af6cca5947","tooteetId":"3d923a95-d8d8-4478-b336-c995cc77407d","laneId":"00000000-0000-0000-0000-000000000000","value":11111,"text":"","measureTypeId":1,"isTimeSet":false},{"id":"ecab9659-7eb5-417a-8f5e-f769629957ae","tooteetId":"3d923a95-d8d8-4478-b336-c995cc77407d","laneId":"00000000-0000-0000-0000-000000000000","value":22222,"text":"","measureTypeId":1,"isTimeSet":false}]

我在这里使用以下方法添加度量视图

 private void addMeasureView(final Measure measure, int position) {
        Log.d("ss","adding measure data value ________________"+measure.value+" position __________"+position);

        final View parent = getLayoutInflater().inflate(R.layout.view_measure_tooteet_item, mDisplayContainer, false);

        final TextView txtDescription, txtValues, txtStartDateTime, txtEndDateTime, labelTaxIncluded, labelTaxColon;
        final ImageView imgEdit, imgDelete;
        final LinearLayout  lnrDescription, lnrStartLayout, lnrEndLayout;
        final View mViewDivider;
        txtDescription = (TextView) parent.findViewById(R.id.txt_description);
        txtValues =  (TextView) parent.findViewById(R.id.values);
        txtStartDateTime = (TextView) parent.findViewById(R.id.start_date_and_time);
        txtEndDateTime = (TextView) parent.findViewById(R.id.end_date_and_time);
        mViewDivider = (View) parent.findViewById(R.id.view_divider);
        imgEdit =  (ImageView) parent.findViewById(R.id.edit);
        imgDelete =  (ImageView) parent.findViewById(R.id.delete);
        lnrDescription = (LinearLayout) parent.findViewById(R.id.lnr_description);
        lnrStartLayout = (LinearLayout) parent.findViewById(R.id.lnr_start_layout);
        lnrEndLayout = (LinearLayout) parent.findViewById(R.id.lnr_end_layout);

        if(tooteet.isOwner(getUserPreference())){
            imgDelete.setVisibility(View.VISIBLE);
            imgEdit.setVisibility(View.VISIBLE);
        }else{
            imgDelete.setVisibility(View.GONE);
            imgEdit.setVisibility(View.GONE);
        }

        if(measure.getValue() > 0) {
            txtValues.setVisibility(View.VISIBLE);
            if (measure.getValue() % 1 == 0) {
                txtValues.setText("" + (int) measure.getValue()+ " "+MeasureTypeSelector.getMeasureTypeById(FeedMeasureDetailsActivity.this, measure.getMeasureTypeId()));
            } else {
                txtValues.setText("" + measure.getValue()+ " "+ MeasureTypeSelector
                        .getMeasureTypeById(FeedMeasureDetailsActivity.this, measure.getMeasureTypeId()));
            }
        }else{
            txtValues.setVisibility(View.GONE);
        }

        if(!TextUtils.isEmpty(measure.getDescription())){
            lnrDescription.setVisibility(View.VISIBLE);
            txtDescription.setText(measure.getDescription());
        }
        else{
            lnrDescription.setVisibility(View.GONE);
        }

        if(!TextUtils.isEmpty(measure.getStartDate())) {
            lnrStartLayout.setVisibility(View.VISIBLE);
            txtStartDateTime.setText("" + DateConversion.getDateAndTime(measure.getStartDate(), "MMMM dd, yyyy hh:mm a"));
        }
        else{
            lnrStartLayout.setVisibility(View.GONE);
        }

        if(!TextUtils.isEmpty(measure.getEndDate())) {
            lnrEndLayout.setVisibility(View.VISIBLE);
            txtEndDateTime.setText("" + DateConversion.getDateAndTime(measure.getEndDate(), "MMMM dd, yyyy hh:mm a"));
        }else{
            lnrEndLayout.setVisibility(View.GONE);
        }
//
//        if(position < mMeasureList.size()){
//            mViewDivider.setVisibility(View.VISIBLE);
//        }else{
//            mViewDivider.setVisibility(View.GONE);
//        }

        imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int pos = (Integer) v.getTag();
                AlertDialog.Builder builder = AlertUtils.getBuilder(FeedMeasureDetailsActivity.this);
                builder.setTitle(R.string.delete);
                builder.setMessage(R.string.delete_tooteet_measure_tuple);
                builder.setPositiveButton(R.string.yes_caps, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (!BDevice.isInternetConnected(FeedMeasureDetailsActivity.this)) {
                            AlertUtils.showNetworkAlert(FeedMeasureDetailsActivity.this);
                            return;
                        }

                        final Dialog pd = UiUtils.getSpinnerDialog(FeedMeasureDetailsActivity.this, getString(R.string.loading));
                        pd.show();

                        getDairyLineApi().deleteMeasureTooteet(mMeasureList.get(pos).getId(), tooteet.getLaneId(), new ResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, String content) {
                                dismiss();
                                AlertDialog.Builder builder = AlertUtils.getBuilder(FeedMeasureDetailsActivity.this);
                                builder.setMessage(R.string.deleted_successfully);
                                builder.setPositiveButton(R.string.ok_caps, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mDisplayContainer.removeView(parent);
                                        mMeasureList.remove(pos);
                                        tooteet.setMeasureJson(Measure.getMeasureDetailJSON(mMeasureList));
                                        mTooteetManager.updateMeasureTooteet(tooteet, tooteet.getId());
                                    }
                                });
                                builder.create().show();
                            }

                            @Override
                            public void onFailure(int statusCode, String content) {
                                dismiss();
                                if (!TextUtils.isEmpty(content)) {
                                    AlertUtils.showAlert(FeedMeasureDetailsActivity.this, content);
                                }
                            }

                            private void dismiss() {
                                if (pd != null && !isFinishing()) {
                                    pd.dismiss();
                                }
                            }
                        });
                    }
                });
                builder.setNegativeButton(R.string.no_caps, null);
                builder.create().show();
            }
        });

        imgEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int pos = (Integer) v.getTag();
                MeasureTooteetSelector measureTooteetSelector = new MeasureTooteetSelector();
                measureTooteetSelector.openMeasureDetailSelector(FeedMeasureDetailsActivity.this, mMeasureList.get(pos),
                        new MeasureTooteetSelector.OnMeasureDetailSelectListener() {
                            @Override
                            public void onMeasureSelect(final Measure measureData) {
                                if (!BDevice.isInternetConnected(FeedMeasureDetailsActivity.this)) {
                                    AlertUtils.showNetworkAlert(FeedMeasureDetailsActivity.this);
                                    return;
                                }

                                final Dialog pd = UiUtils.getSpinnerDialog(FeedMeasureDetailsActivity.this, getString(R.string.loading));
                                pd.show();


                                if (measureData != null) {
                                    mMeasureList.set(pos, measureData);
                                }

                                getDairyLineApi().updateMeasureTooteet(mMeasureList.get(pos), new ResponseHandler() {
                                    @Override
                                    public void onSuccess(int statusCode, String content) {
                                        dismiss();
                                        AlertDialog.Builder builder = AlertUtils.getBuilder(FeedMeasureDetailsActivity.this);
                                        builder.setMessage(R.string.updated_successfully);
                                        builder.setPositiveButton(R.string.ok_caps, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {

                                                if (measureData != null) {
                                                    mMeasureList.set(pos, measureData);
                                                    tooteet.setMeasureJson(Measure.getMeasureDetailJSON(mMeasureList));
                                                    mTooteetManager.updateMeasureTooteet(tooteet, tooteet.getId());
                                                    mActionToSend = ACTION_MEASURE_UPDATE;
                                                    if (measureData.getValue() % 1 == 0) {
                                                        txtValues.setText("" + (int) measureData.getValue()+ " "+MeasureTypeSelector.getMeasureTypeById(FeedMeasureDetailsActivity.this, measureData.getMeasureTypeId()));
                                                    } else {
                                                        txtValues.setText("" + measureData.getValue()+ " "+ MeasureTypeSelector
                                                                .getMeasureTypeById(FeedMeasureDetailsActivity.this, measureData.getMeasureTypeId()));
                                                    }

                                                    Log.d("TAG", "measureData.getStartDate(): "+measureData.getStartDate());
                                                    if(!TextUtils.isEmpty(measureData.getStartDate()) && !measureData.getStartDate().equalsIgnoreCase("-1")) {
                                                        lnrStartLayout.setVisibility(View.VISIBLE);
                                                        txtStartDateTime.setText("" + DateConversion.getDateAndTimeWithoutGMT(measureData.getStartDate(), "MMMM dd, yyyy hh:mm a"));
                                                    }
                                                    else{
                                                        lnrStartLayout.setVisibility(View.GONE);
                                                    }

                                                    Log.d("TAG", "measureData.getEndDate(): "+measureData.getEndDate());
                                                    if(!TextUtils.isEmpty(measureData.getEndDate())&& !measureData.getStartDate().equalsIgnoreCase("-1")) {
                                                        lnrEndLayout.setVisibility(View.VISIBLE);
                                                        txtEndDateTime.setText("" + DateConversion.getDateAndTimeWithoutGMT(measureData.getEndDate(), "MMMM dd, yyyy hh:mm a"));
                                                    }else{
                                                        lnrEndLayout.setVisibility(View.GONE);
                                                    }

                                                    if(!TextUtils.isEmpty(measureData.getDescription())){
                                                        lnrDescription.setVisibility(View.VISIBLE);
                                                        txtDescription.setText(measureData.getDescription());
                                                    }
                                                    else{
                                                        lnrDescription.setVisibility(View.GONE);
                                                    }
                                                }

                                            }
                                        });
                                        builder.create().show();
                                    }

                                    @Override
                                    public void onFailure(int statusCode, String content) {
                                        dismiss();
                                        if (!TextUtils.isEmpty(content)) {
                                            AlertUtils.showAlert(FeedMeasureDetailsActivity.this, content);
                                        }
                                    }

                                    private void dismiss() {
                                        if (pd != null && !isFinishing()) {
                                            pd.dismiss();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onCancel() {
                            }
                        });

            }
        });

        imgEdit.setTag(position);
        imgDelete.setTag(position);
        addView(parent);
    }

我在addMeasureView中的日志如下:

adding measure data value ________________11111.0 position __________0
adding measure data value ________________22222.0 position __________1

但是当我在布局中将其视为此订单时

 adding measure data value ________________22222.0 
adding measure data value ________________11111.0 

请建议我。

这是我用于getValue()

的模型类
import com.kwypesoft.lanes.create_tooteet.LocalTooteetCreator;
import com.kwypesoft.lanes.utils.DateConversion;
import com.kwypesoft.lanes.utils.TextUtils;

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

import java.io.Serializable;
import java.util.ArrayList;

public class Measure implements Serializable{

    //    "id": "398627f1-9392-4b3f-8741-903fbcbbd3be",
//            "tooteetId": "ab36f69e-a0c8-4f31-aa8d-9b4038a76d57",
//            "laneId": "00000000-0000-0000-0000-000000000000",
//            "startDate": "2016-04-26T08:00:00",
//            "endDate": "2016-04-27T10:00:00",
//            "value": 125.6500000000000,
//            "measureTypeId": 20
    public String id;
    public String tooteetId;
    public String laneId;
    public String startDate;
    public String endDate;
    public String description;
    public double value;
    public int measureTypeId;
    public boolean isTimeSet;

    public Measure() {
    }

    public Measure(JSONArray jsonArray) {
        try {
            for(int i =0; i<jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                id = jsonObject.optString("id");
                tooteetId = jsonObject.optString("tooteetId");
                laneId = jsonObject.optString("laneId");
                startDate = jsonObject.optString("startDate");
                endDate = jsonObject.optString("endDate");
                description = jsonObject.optString("text");
                value = jsonObject.optDouble("value");
                measureTypeId = jsonObject.optInt("measureTypeId");
                isTimeSet = jsonObject.optBoolean("isTimeSet");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static String getMeasureJSON(ArrayList<LocalTooteetCreator.MeasureData> data) {

        JSONArray jsonArray = new JSONArray();

        for (LocalTooteetCreator.MeasureData items : data) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", items.value);
                jsonObject.put("text", items.description);
                jsonObject.put("measureTypeId", items.measureTypeId);
                if(items.startDate != -1){
                    jsonObject.put("startDate", DateConversion.getDateWithTFromMilliSeconds(items.startTime, items.startDate));
                }

                if(items.endDate != -1){
                    jsonObject.put("endDate", DateConversion.getDateWithTFromMilliSeconds(items.endTime, items.endDate));
                }
                jsonObject.put("isTimeSet", items.isTimeSet);
                jsonArray.put(jsonObject);

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
        return jsonArray.toString();

    }

    public static String getMeasureDetailJSON(ArrayList<Measure> data) {

        JSONArray jsonArray = new JSONArray();

        for (Measure items : data) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", items.id);
                jsonObject.put("tooteetId", items.tooteetId);
                jsonObject.put("laneId", items.laneId);
                if(!TextUtils.isEmpty(items.startDate) && !items.getStartDate().equalsIgnoreCase("-1")){
                    jsonObject.put("startDate", items.startDate);
                }

                if(!TextUtils.isEmpty(items.endDate) && !items.getStartDate().equalsIgnoreCase("-1")){
                    jsonObject.put("endDate", items.endDate);
                }

                jsonObject.put("text", items.description);
                jsonObject.put("value", items.value);
                jsonObject.put("measureTypeId", items.measureTypeId);
                jsonObject.put("isTimeSet", items.isTimeSet);
                jsonArray.put(jsonObject);

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
        return jsonArray.toString();

    }

    public String getId() {
        return id;
    }

    public String getTooteetId() {
        return tooteetId;
    }

    public String getLaneId() {
        return laneId;
    }

    public String getStartDate() {
        return startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public double getValue() {
        return value;
    }

    public int getMeasureTypeId() {
        return measureTypeId;
    }

    public boolean getIsTimeSet() {
        return isTimeSet;
    }

    public String getDescription() {
        return description;
    }

    public boolean isTimeSet() {
        return isTimeSet;
    }

}

1 个答案:

答案 0 :(得分:1)

您好我在addview方法中犯了一个错误。在我的addview方法之前

mDisplayContainer.addView(view, mDisplayContainer.getChildCount() - 1);

现在我改变了

mDisplayContainer.addView(view);

它为我工作。非常感谢你的评论