2016-09-07 59 views
0

在我的應用程序中,我從MySQL數據庫中獲取訂單詳細信息,並基於db中的行數動態地在應用中的LinearLayout中添加TextView(如果存在有6行,然後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); 
} 
} 
+0

你能回到以前的屏幕,如果你沒有叫你'的getData()'? – SripadRaj

+0

的getData()是好的。是的,我能夠回去即工具欄是可見的,直到的getData() –

+0

爲什麼你使用'LinearLayout',而不是直接的'ListView'或'RecyclerView'?看起來你正在阻止'getData()'的UI線程。將加載數據從DB移動到'AsyncTask'。 – j2ko

回答

1

中刪除此代碼不需要的話:

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

setContentView(this.scrollView); 

移動從數據庫讀取到AsyncTask - 因爲你是阻止打開呼叫UI線程getData()Application凍結durring加載。

還需要考慮使用適當的容器,像ListViewRecycleView

+0

我不知道如何使用RecyclerView或ListView –

相關問題