android - 在线性布局中动态添加的 TextView 中获取工具栏

标签 android mysql android-xml

在我的应用程序中,我从 mysql 数据库中获取订单详细信息,并根据数据库中的行数,在我的 LinearLayout 中动态添加 TextView应用程序(如果有 6 行,则应用程序中将显示 6 个 TextView ) 在执行此操作时,工具栏已从 Activity 中禁用,因此我无法返回到我的父 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);
}
}

最佳答案

删除不需要的代码:

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

setContentView(this.scrollView);

将读取从数据库转移到AsyncTask - 因为您在调用getData()时阻塞了UI线程,并且Application在加载期间卡住。

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

关于android - 在线性布局中动态添加的 TextView 中获取工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39363551/

相关文章:

java - 安卓;按钮声音在默认按钮声音后播放

php - 如何向 MySql 数据库中插入值? PHP v5.5,libmysql - mysqlnd 5.0.11-dev

mysql - Web2py,多个Ajax调用和存储在MySQL中的 session 变量

android - 微调器在 android Activity 中占用其下方的整个空间

java - 如何在动画之前和之后执行 Action

android - 是否可以将 Amazon EC2 用于 Android 同步到云后端

mysql - MSSQL 中的等效 SQL 查询

android-edittext - 如何将Edittext设置为多行?

java - 在 Java 文件而不是 xml 中定义颜色常量

android - 具有负值和正值的 NumberPicker