2017-10-08 152 views
-1

我有一個對象的項目列表。我使用for循環創建了按照列表大小的動態按鈕。如何切換數據點擊按鈕?

我正在顯示一些數據時,它的第一個項目的循環。現在/我想在點擊按鈕時切換數據。

按鈕將具有該項目的編號,如果列表中有三個項目,則三個按鈕在那裏命名爲一,二,三。

所以如果我先點擊第一項我要顯示第一項的數據,如果第二項點擊第二項的數據。

我試圖做到這一點,但我無法得到結果。

@Override 
public void doPostExecute(List<Payment> list1) 
{ 

    list.addAll(list1); 

    for(final Payment payment : list) { 

     i++; 

     Button myButton = new Button(getActivity()); 

     LinearLayoutCompat.LayoutParams lp = new LinearLayoutCompat.LayoutParams(60,30); 
     linear_buttons.addView(myButton, lp); 

     myButton.setText(String.valueOf(i)); 
     myButton.setBackgroundResource(R.drawable.edit_button); 

     myButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (payment.getPaidAmount() != null && !payment.getPaidAmount().equals("null")) 
        txt_paid.setText("$" + payment.getPaidAmount()); 

       if (payment.getProductStatus() != null && !payment.getProductStatus().equals("null")) { 
        if (payment.getProductStatus().equals("1")) { 
         txt_status.setText(getString(R.string.paid)); 
         button_complain.setVisibility(View.VISIBLE); 
         button_more.setVisibility(View.GONE); 

        } else if (payment.getProductStatus().equals("0")) { 
         txt_status.setText(getString(R.string.unPaid)); 
         button_complain.setVisibility(View.GONE); 
         button_more.setVisibility(View.GONE); 
        } else if (payment.getProductStatus().equals("5")) { 
         txt_status.setText(getString(R.string.processing)); 
         button_complain.setVisibility(View.VISIBLE); 
         button_more.setVisibility(View.GONE); 
        } else if (payment.getProductStatus().equals("6")) { 
         button_complain.setVisibility(View.GONE); 
         button_more.setVisibility(View.VISIBLE); 
         txt_status.setText(getString(R.string.releasedStatus)); 
        } else if (payment.getProductStatus().equals("8")) { 
         txt_status.setText(getString(R.string.disputeStatus)); 
         button_complain.setVisibility(View.GONE); 
         button_more.setVisibility(View.VISIBLE); 
        } else if (payment.getProductStatus().equals("9")) { 
         txt_status.setText(getString(R.string.disputeResolvedStatus)); 
         button_complain.setVisibility(View.GONE); 
         button_more.setVisibility(View.VISIBLE); 
        } else if (payment.getProductStatus().equals("2")) { 
         txt_status.setText(getString(R.string.completedStatus)); 
         button_complain.setVisibility(View.GONE); 
         button_more.setVisibility(View.VISIBLE); 
        } 

        if (payment.getTpAmount() != null && !payment.getTpAmount().equals("null")) { 

         String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTpAmount())); 
         txt_tp_amount.setText("$" + amount); 
        } 

        if (payment.getCommission() != null && !payment.getCommission().equals("null")) { 

         String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getCommission())); 
         txt_comission.setText("$" + amount); 
        } 

        if (payment.getTax() != null && !payment.getTax().equals("null")) { 

         String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTax())); 
         txt_tax.setText("$" + amount); 
        } 

        if (payment.getPayeEmail() != null && !payment.getPayeEmail().equals("null")) { 

         txt_payee_email.setText(payment.getPayeEmail()); 
        } 
        if (payment.getPayerEmail() != null && !payment.getPayerEmail().equals("null")) { 

         txt_payer_email.setText(payment.getPayerEmail()); 
        } 

        if (payment.getPayerName() != null && !payment.getPayerName().equals("null")) { 

         txt_payer_name.setText(payment.getPayerName()); 
        } 

        if (payment.getVendorId() != null && !payment.getVendorId().equals("null")) { 

         txt_vendor_id.setText(payment.getVendorId()); 
        } 
       } 
      } 
     }); 

     if(i==1) { 

      if (payment.getPaidAmount() != null && !payment.getPaidAmount().equals("null")) 
       txt_paid.setText("$" + payment.getPaidAmount()); 

      if (payment.getProductStatus() != null && !payment.getProductStatus().equals("null")) { 
       if (payment.getProductStatus().equals("1")) { 
        txt_status.setText(getString(R.string.paid)); 
        button_complain.setVisibility(View.VISIBLE); 
        button_more.setVisibility(View.GONE); 

       } else if (payment.getProductStatus().equals("0")) { 
        txt_status.setText(getString(R.string.unPaid)); 
        button_complain.setVisibility(View.GONE); 
        button_more.setVisibility(View.GONE); 
       } else if (payment.getProductStatus().equals("5")) { 
        txt_status.setText(getString(R.string.processing)); 
        button_complain.setVisibility(View.VISIBLE); 
        button_more.setVisibility(View.GONE); 
       } else if (payment.getProductStatus().equals("6")) { 
        button_complain.setVisibility(View.GONE); 
        button_more.setVisibility(View.VISIBLE); 
        txt_status.setText(getString(R.string.releasedStatus)); 
       } else if (payment.getProductStatus().equals("8")) { 
        txt_status.setText(getString(R.string.disputeStatus)); 
        button_complain.setVisibility(View.GONE); 
        button_more.setVisibility(View.VISIBLE); 
       } else if (payment.getProductStatus().equals("9")) { 
        txt_status.setText(getString(R.string.disputeResolvedStatus)); 
        button_complain.setVisibility(View.GONE); 
        button_more.setVisibility(View.VISIBLE); 
       } else if (payment.getProductStatus().equals("2")) { 
        txt_status.setText(getString(R.string.completedStatus)); 
        button_complain.setVisibility(View.GONE); 
        button_more.setVisibility(View.VISIBLE); 
       } 

       if (payment.getTpAmount() != null && !payment.getTpAmount().equals("null")) { 

        String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTpAmount())); 
        txt_tp_amount.setText("$" + amount); 
       } 

       if (payment.getCommission() != null && !payment.getCommission().equals("null")) { 

        String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getCommission())); 
        txt_comission.setText("$" + amount); 
       } 

       if (payment.getTax() != null && !payment.getTax().equals("null")) { 

        String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTax())); 
        txt_tax.setText("$" + amount); 
       } 

       if (payment.getPayeEmail() != null && !payment.getPayeEmail().equals("null")) { 

        txt_payee_email.setText(payment.getPayeEmail()); 
       } 
       if (payment.getPayerEmail() != null && !payment.getPayerEmail().equals("null")) { 

        txt_payer_email.setText(payment.getPayerEmail()); 
       } 

       if (payment.getPayerName() != null && !payment.getPayerName().equals("null")) { 

        txt_payer_name.setText(payment.getPayerName()); 
       } 

       if (payment.getVendorId() != null && !payment.getVendorId().equals("null")) { 

        txt_vendor_id.setText(payment.getVendorId()); 
       } 
      } 
     } 

    } 

} 

我該如何做到這一點?請幫忙..謝謝。

回答

0

您正在使用變量i作爲全局變量並在循環內使用i++。因此在onClick方法中,if(i == 1)要麼始終爲假,要麼始終爲真。

在您創建按鈕的for循環中創建變量local並使用myButton.setId(i);

現在裏面OnClickListener可以使用int id = v.getId();,並使用if(id == 1)

+0

獲取數據如何將此開關是否行得通呢?我不知道數組的大小和將創建多少個按鈕將基於數據,所以如何檢查交換機中的正確對象? – Sid

+0

如何在開關的情況下? – Sid

+0

@Sid你的代碼在問題中發生了什麼,以及你想要做什麼? –

0

您可以設置一個onClickLister到該行的按鈕,這將有該行的數據參考。使用意圖來啓動詳細的活動,並將點擊的項目的數據作爲額外添加。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

//Inflate your row 

button.setOnClickListener(new OnClickListener(){ 
    @Override 
    //On click function 
    public void onClick(View view) { 
     HashMap<String, String> item = contactsList.get(position); 
     //Create the intent to start another activity 
     Intent intent = new Intent(view.getContext(), DetailActivity.class); 
     intent.putExtra("data", item); 
     view.getContext().startActivity(intent); 
    } 
}); 
}); 
在接收活動

,您可以從意向

HashMap<String, String> data = getIntent().getExtras().getParcelableExtra("data"); 
+0

我沒有任何意圖,我在相同的片段本身顯示數據 – Sid