2016-09-07 13 views
0

私のアプリでは、mysql dbから注文の詳細を取得しており、dbの行数に基づいて動的にTextViewを私のLinearLayoutに追加しています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

のような適切なコンテナを使用して考える必要があるあなたが戻って前の画面に行くことができますか? – SripadRaj

+0

getData()は問題ありません。つまり、getData()までツールバーが表示されます。 –

+0

なぜ、 'ListView'または' RecyclerView'の代わりに 'LinearLayout'を直接使用していますか? 'getData()'のUIスレッドをブロックしているようです。データをDBから 'AsyncTask'に移動します。 – j2ko

答えて

1

それが必要とされていない、このコードを削除します。AsyncTaskにDBから読み込む

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

setContentView(this.scrollView); 

移動を - あなたはロードをdurring getData()Applicationフリーズへの呼び出しにUIスレッドをブロックしているため。あなたは `のgetData()`と呼ばれていなかった場合

また、あなたがListViewまたはRecycleView

+0

RecyclerViewまたはListViewの使い方は知らない –

関連する問題