在“线性布局”中动态添加“文本”视图中的工具栏

时间:2016-09-07 07:18:42

标签: android mysql android-xml

在我的应用程序中,我从mysql数据库中获取订单详细信息,并根据db中的行数我在app TextView中动态添加LinearLayout(如果有的话)行然后6个文本视图将显示在应用程序中) 在执行工具栏时,Activity禁用了工具栏,因此我无法返回到我的父活动 我不确定,但我认为使用setLayoutParams是罪魁祸首。

请帮忙!

public class OrderHistory extends AppCompatActivity  {

private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;

private ProgressDialog loading;
private ScrollView scrollView;
private LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_history);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    getData(); //get data from the db

}

private void getData() {
   //getting data from db
}

private void showJSON(String response){

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Constants.JSON_ARRAY);
        this.scrollView = (ScrollView) findViewById(R.id.scrollableContents);
        this.linearLayout = (LinearLayout) findViewById(R.id.linear);
        this.linearLayout.setOrientation(LinearLayout.VERTICAL);
        TextView[] t1 = new TextView[result.length()];
        TextView[] t2 = new TextView[result.length()];
        TextView[] t3 = new TextView[result.length()];
        TextView[] t4 = new TextView[result.length()];
        TextView[] t5 = new TextView[result.length()];
        ImageView[] img = new ImageView[result.length()];


        if(result.length()!=0) {

            for (int i = 0; i < result.length(); i++) {
                JSONObject collegeData = result.getJSONObject(i);


                int orderTotal = Integer.parseInt(collegeData.getString(Constants.KEY_AMOUNT).split("\\ ")[0]) - 49;
                int total = orderTotal + 49;

                switch (collegeData.getString(Constants.KEY_CCAvenueOrderStatus)) {
                    case "Success":
                        t1[i] = new TextView(this);
                        t1[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    //when this is execute the TOOLBAR disappears #i guess
                        String success = (i + 1) + "." + "Payment of ₹" + collegeData.getString(Constants.KEY_AMOUNT) + " was received by Horoscope Daily !";
                        t1[i].setTextColor(getResources().getColor(R.color.white));
                        t1[i].setTextSize(17);
                        t1[i].setText(Html.fromHtml("<h4>" + success + "</h4>" + "Your transaction was successfull<br> Order Number<br>" + "<b>" + collegeData.getString(Constants.KEY_ORDERID) + "</b>" + "<br>" + collegeData.getString(Constants.KEY_DATE) + "<br>"));
                        this.linearLayout.addView(t1[i]);

                        break;


                    case "Failure":
                        t1[i] = new TextView(this);
                        t1[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        String failure = (i + 1) + "." + "Payment of ₹" + collegeData.getString(Constants.KEY_AMOUNT) + " failed !";
                        t1[i].setTextColor(getResources().getColor(R.color.white));
                        t1[i].setTextSize(17);
                        t1[i].setText(Html.fromHtml("<h4>" + failure + "</h4>" + "Your payment has been declined by your bank.Please contact your bank for any queries.If money has been deducted from your account,your bank will inform us within 48 hrs and we will refund the same<br><br> Order Number<br>" + "<b>" + collegeData.getString(Constants.KEY_ORDERID) + "</b>" + "<br>" + collegeData.getString(Constants.KEY_DATE) + "<br>"));
                        this.linearLayout.addView(t1[i]);

                        break;


                    case "Aborted":
                        t1[i] = new TextView(this);
                        t1[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        String aborted = (i + 1) + "." + "Payment of ₹" + collegeData.getString(Constants.KEY_AMOUNT) + " failed !";
                        t1[i].setTextColor(getResources().getColor(R.color.white));
                        t1[i].setTextSize(17);
                        t1[i].setText(Html.fromHtml("<h4>" + aborted + "</h4>" + "Your payment has been declined by your bank as the OTP(one time password) entered is incorrect.Please try again with the correct OTP or contact your bank for any queries.<br><br> Order Number<br>" + "<b>" + collegeData.getString(Constants.KEY_ORDERID) + "</b>" + "<br>" + collegeData.getString(Constants.KEY_DATE) + "<br>"));
                        this.linearLayout.addView(t1[i]);
                        break;


                    default:
                }


                t3[i] = new TextView(this);
                t3[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                t3[i].setTextColor(getResources().getColor(R.color.white));
                t3[i].setTextSize(17);
                t3[i].setText(Html.fromHtml("<h4>You have below Items in your order</h4> <br>" + collegeData.getString(Constants.KEY_ITEM) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "₹ " + orderTotal + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + collegeData.getString(Constants.KEY_RATTI) + " Ratti" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + collegeData.getString(Constants.KEY_QUANTITY) + " Quantity" + "<br>"));
                this.linearLayout.addView(t3[i]);

                switch (collegeData.getString(Constants.KEY_ITEM)) {
                    case "Coral":
                        String uri = "@drawable/ic_coral_moonga";  // where myresource (without the extension) is the file
                        int imageResource = getResources().getIdentifier(uri, null, getPackageName());
                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        Drawable res = getResources().getDrawable(imageResource);
                        img[i].setImageDrawable(res);
                        this.linearLayout.addView(img[i]);


                        break;
                    case "Opal":

                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_opal", null, getPackageName())));
                        this.linearLayout.addView(img[i]);

                        break;
                    case "Emerald":
                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_emerald_panna", null, getPackageName())));
                        this.linearLayout.addView(img[i]);


                        break;
                    case "Pearl":
                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_pearl_moti", null, getPackageName())));
                        this.linearLayout.addView(img[i]);

                        break;

                    case "Ruby":
                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_ruby_manikya", null, getPackageName())));
                        this.linearLayout.addView(img[i]);

                        break;


                    case "Yellow Sapphire":
                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_yellow_sapphire_pikhraj", null, getPackageName())));
                        this.linearLayout.addView(img[i]);

                        break;

                    case "Blue Sapphire":
                        img[i] = new ImageView(this);
                        img[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        img[i].getLayoutParams().height = 200;
                        img[i].getLayoutParams().width = 200;
                        img[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_blue_sapphire", null, getPackageName())));
                        this.linearLayout.addView(img[i]);

                        break;


                    default:

                        break;
                }


                t4[i] = new TextView(this);
                t4[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                t4[i].setTextColor(getResources().getColor(R.color.white));
                t4[i].setTextSize(17);
                t4[i].setText(Html.fromHtml("<br><h4>Payment Details</h4> CC Tracking ID <br>" + collegeData.getString(Constants.KEY_CCAvenueTacking_id) + "<br>"));
                this.linearLayout.addView(t4[i]);

                t5[i] = new TextView(this);
                t5[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                t5[i].setText(Html.fromHtml("<h4>Summary</h4> <br>" + "Order Total" + "&nbsp;&nbsp;&nbsp;&nbsp;" + "₹ " + orderTotal + "<br>" + "Shipping" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "₹ 49" + "<br>" + "Total" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "₹ " + total + "<br>"));
                t5[i].setTextColor(getResources().getColor(R.color.white));
                t5[i].setTextSize(17);
                this.linearLayout.addView(t5[i]);


                View v = new View(this);
                v.setLayoutParams(new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        5
                ));
                v.setBackgroundColor(Color.parseColor("#B3B3B3"));

                LinearLayout.LayoutParams margin = (LinearLayout.LayoutParams) v.getLayoutParams();
                margin.setMargins(0, 50, 0, 50);
                v.setLayoutParams(margin);


                this.linearLayout.addView(v);


            }
            if (this.scrollView.getParent() != null)
                ((ViewGroup) this.scrollView.getParent()).removeView(this.scrollView);
            setContentView(this.scrollView);

        }
        else{
            Toast.makeText(getApplicationContext(),
                    "No Order History\n"+"Our database indicate that you don't have any orders yet !", Toast.LENGTH_SHORT).show();
            // hide the progress dialog
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
  //  textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}
}

1 个答案:

答案 0 :(得分:1)

删除不需要的代码:

AsyncTask

将数据从数据库移至getData() - 因为您在调用Application时阻止了UI线程,并且pdftotext在加载时冻结。

另外,您需要考虑使用适当的容器,例如ListViewRecycleView