我在另一个布局中对布局进行了四次膨胀,现在我想在布局中为几个控件添加点击监听器。当我这样做时,它只更改最后一个布局的值,而不是更改为相应的布局。 / p>
公共类DRSDetail扩展了Activity {
TextView changeCode;
Button generate, fromVessel, toVessel;
String strFromVessel, strToVessel, stroverTime, stroffShoreTime,
stronShoreTime;
Spinner spinnerValues;
Date dateOne, dateTwo;
EditText overTime, offShoreTime, onShoreTime;
ArrayList<String> dataValues = new ArrayList<String>();
View layout_number;
int idValue = 10;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drsdetail);
changeCode = (TextView) findViewById(R.id.changeCode);
generate = (Button) findViewById(R.id.generate);
LinearLayout parent = (LinearLayout) findViewById(R.id.parentLayout);
loadSpinnerData();
for (i = 0; i < 4; i++) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout row = new LinearLayout(this);
layout_number = inflater.inflate(R.layout.layoutinflate, parent,
false);
spinnerValues = (Spinner) layout_number
.findViewById(R.id.spinnerValues);
fromVessel = (Button) layout_number.findViewById(R.id.fromVessel);
toVessel = (Button) layout_number.findViewById(R.id.toVessel);
overTime = (EditText) layout_number.findViewById(R.id.overTimeEdit);
offShoreTime = (EditText) layout_number
.findViewById(R.id.offShoreEdit);
onShoreTime = (EditText) layout_number
.findViewById(R.id.onShoreEdit);
row.addView(layout_number);
parent.addView(row);
assignClickListener(fromVessel);
ArrayAdapter<String> adapterDataValues = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, dataValues);
adapterDataValues
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerValues.setAdapter(adapterDataValues);
}
}
private void assignClickListener(final View vessel) {
fromVessel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(DRSDetail.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0,
int selectedHour, int selectedMinute) {
fromVessel.setText(selectedHour + ":"
+ selectedMinute);
strFromVessel = fromVessel.getText().toString();
System.out.println("strFromVessel"
+ strFromVessel);
}
}, hour, minute, true);// Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
};
private void loadSpinnerData() {
dataValues.clear();
try {
System.out.println("in try");
DBHelper dbHelper = new DBHelper(DRSDetail.this);
dbHelper.openDataBase();
SQLiteDatabase db = dbHelper.getReadableDatabase();
// project number
Cursor crData = db.rawQuery("SELECT DRS_ID FROM DRS_TBL", null);
if (crData.getCount() > 0) {
while (crData.moveToNext()) {
dataValues.add(crData.getString(0));
System.out.println("value of cr.getString "
+ crData.getString(0));
}
}
crData.close();
} catch (Exception e) {
System.out.println("exception");
}
}
答案 0 :(得分:0)
每次循环都会重新分配变量fromVessel
,因此在最后一次循环迭代中,它被分配给最终视图。每次调用OnClickListener
时都会使用该视图 - 无论哪个视图调用了单击侦听器。
您需要的是一个闭包,以便您可以参考每个不同的视图。您无法创建临时变量,因为它必须被声明为final。
你可以做一些事情,比如将setOnClickListener
分解为它自己的功能范围:
private void assignClickListener(final View vessel) {
vessel.setOnClickListener(new OnClickListener() {
// Time picker stuff...
});
});