我有一个物品清单。我正在使用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 :(得分:0)
您正在使用变量i作为全局变量并在循环内使用i++
。因此,在onClick
方法中,if(i == 1)
始终为false,或始终为true。
将变量设为本地,并在创建按钮的for循环中使用myButton.setId(i);
。
现在在OnClickListener
内,您可以使用int id = v.getId();
并使用if(id == 1)
答案 1 :(得分: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");