访问匿名内部 class 中的非最终值
Accessing non-final values inside anonymous inner class
我有这 5 个 Edittext 字段,我想使用一个按钮将这些值写入数据库。对于按钮 Clicklistener,我使用了匿名内部 class 但只允许使用最终值,这意味着我在写入数据库时会得到空字段。如何以非最终方式访问这些字段?有什么办法吗?
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
String areaofyard = yardarea.getText().toString();
EditText brick = (EditText)view.findViewById(R.id.brick);
final String brickused = brick.getText().toString();
EditText rcc = (EditText)view.findViewById(R.id.rcc);
final String rccused = rcc.getText().toString();
EditText overhead = (EditText)view.findViewById(R.id.overhead);
final String ovhh = overhead.getText().toString();
EditText underground = (EditText)view.findViewById(R.id.underground);
final String ung = underground.getText().toString();
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
如何将获取字符串改为放在 onClickListener 中?
public void onViewCreated(View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
EditText brick = (EditText)view.findViewById(R.id.brick);
EditText rcc = (EditText)view.findViewById(R.id.rcc);
EditText overhead = (EditText)view.findViewById(R.id.overhead);
EditText underground = (EditText)view.findViewById(R.id.underground);
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String areaofyard = yardarea.getText().toString();
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String ung = underground.getText().toString();
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
final EditText brick = (EditText)view.findViewById(R.id.brick);
final EditText rcc = (EditText)view.findViewById(R.id.rcc);
final EditText overhead = (EditText)view.findViewById(R.id.overhead);
final EditText underground = (EditText)view.findViewById(R.id.underground);
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String areaofyard = yardarea.getText().toString();
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String ung = underground.getText().toString();
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
您可以只将 EditText 对象声明为最终对象,也可以将它们声明为全局变量,并在按钮的 onclick 中使用上面发布的相同代码。
尝试将 EditText 声明为全局
EditText yardarea , brick , rcc, overhead , underground;
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
yardarea = (EditText)view.findViewById(R.id.yardarea);
brick = (EditText)view.findViewById(R.id.brick);
rcc = (EditText)view.findViewById(R.id.rcc);
overhead = (EditText)view.findViewById(R.id.overhead);
underground = (EditText)view.findViewById(R.id.underground);
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String areaofyard = yardarea.getText().toString();
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String ung = underground.getText().toString();
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
You should get the value from edittext inside onClick
final EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
final EditText brick = (EditText)view.findViewById(R.id.brick);
final EditText rcc = (EditText)view.findViewById(R.id.rcc);
final EditText overhead = (EditText)view.findViewById(R.id.overhead);
final EditText underground = (EditText)view.findViewById(R.id.underground);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String areaofyard = yardarea.getText().toString();
String ung = underground.getText().toString();
// Do your database operation with these value
}
});
我有这 5 个 Edittext 字段,我想使用一个按钮将这些值写入数据库。对于按钮 Clicklistener,我使用了匿名内部 class 但只允许使用最终值,这意味着我在写入数据库时会得到空字段。如何以非最终方式访问这些字段?有什么办法吗?
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
String areaofyard = yardarea.getText().toString();
EditText brick = (EditText)view.findViewById(R.id.brick);
final String brickused = brick.getText().toString();
EditText rcc = (EditText)view.findViewById(R.id.rcc);
final String rccused = rcc.getText().toString();
EditText overhead = (EditText)view.findViewById(R.id.overhead);
final String ovhh = overhead.getText().toString();
EditText underground = (EditText)view.findViewById(R.id.underground);
final String ung = underground.getText().toString();
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
如何将获取字符串改为放在 onClickListener 中?
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState);
EditText yardarea = (EditText)view.findViewById(R.id.yardarea); EditText brick = (EditText)view.findViewById(R.id.brick); EditText rcc = (EditText)view.findViewById(R.id.rcc); EditText overhead = (EditText)view.findViewById(R.id.overhead); EditText underground = (EditText)view.findViewById(R.id.underground); Button button = (Button) view.findViewById(R.id.register); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String areaofyard = yardarea.getText().toString(); String brickused = brick.getText().toString(); String rccused = rcc.getText().toString(); String ovhh = overhead.getText().toString(); String ung = underground.getText().toString(); DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext()); databaseAccess.open(); databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung ); databaseAccess.close(); } }); }
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
final EditText brick = (EditText)view.findViewById(R.id.brick);
final EditText rcc = (EditText)view.findViewById(R.id.rcc);
final EditText overhead = (EditText)view.findViewById(R.id.overhead);
final EditText underground = (EditText)view.findViewById(R.id.underground);
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String areaofyard = yardarea.getText().toString();
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String ung = underground.getText().toString();
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
您可以只将 EditText 对象声明为最终对象,也可以将它们声明为全局变量,并在按钮的 onclick 中使用上面发布的相同代码。
尝试将 EditText 声明为全局
EditText yardarea , brick , rcc, overhead , underground;
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
yardarea = (EditText)view.findViewById(R.id.yardarea);
brick = (EditText)view.findViewById(R.id.brick);
rcc = (EditText)view.findViewById(R.id.rcc);
overhead = (EditText)view.findViewById(R.id.overhead);
underground = (EditText)view.findViewById(R.id.underground);
Button button = (Button) view.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String areaofyard = yardarea.getText().toString();
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String ung = underground.getText().toString();
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
databaseAccess.open();
databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
databaseAccess.close();
}
});
}
You should get the value from edittext inside onClick
final EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
final EditText brick = (EditText)view.findViewById(R.id.brick);
final EditText rcc = (EditText)view.findViewById(R.id.rcc);
final EditText overhead = (EditText)view.findViewById(R.id.overhead);
final EditText underground = (EditText)view.findViewById(R.id.underground);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String brickused = brick.getText().toString();
String rccused = rcc.getText().toString();
String ovhh = overhead.getText().toString();
String areaofyard = yardarea.getText().toString();
String ung = underground.getText().toString();
// Do your database operation with these value
}
});