我正在Android中编写一个布局,它很有活力。由于我也在使用数据库,所以我决定将它分成不同的类,这意味着我有几个“设置”类。但是,这使我在调用它们时几乎将所有布局元素传递给类。我想知道的是,如果有一种方法可以将它们包装在一个类似Bundle的参数中,那么代码就不会太乱了。编码的一个例子:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_materialtab);
final Spinner spnMaterial = (Spinner) findViewById(R.id.mat_spn_material);
final EditText nome = (EditText) findViewById(R.id.mat_frm_nome);
final EditText modE = (EditText) findViewById(R.id.mat_frm_elasticidade);
final EditText dVol = (EditText) findViewById(R.id.mat_frm_densidade);
final ImageButton atMaterial = (ImageButton) findViewById(R.id.mat_btm_atualizar);
final ImageButton addMaterial = (ImageButton) findViewById(R.id.mat_btm_add);
final ImageButton excMaterial = (ImageButton) findViewById(R.id.mat_btm_exc);
createSpinner(nome, modE, dVol, spnMaterial, atMaterial, excMaterial, 0);
createAddMaterial(nome, modE, dVol, spnMaterial, addMaterial, atMaterial, excMaterial);
createAtMaterial(nome, modE, dVol, spnMaterial, atMaterial, excMaterial);
createExcMaterial(nome, modE, dVol, spnMaterial, atMaterial, excMaterial);
}
public void createSpinner(final EditText nome, final EditText modE, final EditText dVol, final Spinner spnMaterial, final ImageButton atMaterial, final ImageButton excMaterial, final int position) {
final SQLiteDatabase Estrutura = openOrCreateDatabase("Estrutura.db", getBaseContext().MODE_PRIVATE, null);
final Cursor linha = Estrutura.rawQuery("SELECT * FROM MATERIAIS", null);
String[] from = {"_id","nome","modE","dVol"};
int[] to = {R.id.mat_lay_txvid,R.id.mat_lay_txvnome,R.id.mat_lay_txvmodE,R.id.mat_lay_txvdVol};
SimpleCursorAdapter adp = new SimpleCursorAdapter(getBaseContext(), R.layout.sp_material, linha, from, to, 0);
spnMaterial.setAdapter(adp);
spnMaterial.setSelection(position);
spnMaterial.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
linha.moveToPosition(position);
nome.setText(linha.getString(1));
modE.setText(String.valueOf(linha.getDouble(2)));
dVol.setText(String.valueOf(linha.getDouble(3)));
if(position == 0) {isEnabled(false, nome, modE, dVol, atMaterial, excMaterial);} else
{isEnabled(true, nome, modE, dVol, atMaterial, excMaterial);}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
public void createAddMaterial(final EditText nome, final EditText modE, final EditText dVol, final Spinner spnMaterial,final ImageButton addMaterial, final ImageButton atMaterial, final ImageButton excMaterial) {
final SQLiteDatabase Estrutura = openOrCreateDatabase("Estrutura.db", getBaseContext().MODE_PRIVATE, null);
addMaterial.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String sql = "INSERT INTO MATERIAIS(nome,modE,dVol) VALUES('Nome',0,0)";
Estrutura.execSQL(sql);
createSpinner(nome, modE, dVol, spnMaterial, atMaterial, excMaterial, spnMaterial.getAdapter().getCount());
}
});
}
public void createAtMaterial(final EditText nome, final EditText modE, final EditText dVol, final Spinner spnMaterial, final ImageButton atMaterial, final ImageButton excMaterial) {
final SQLiteDatabase Estrutura = openOrCreateDatabase("Estrutura.db", getBaseContext().MODE_PRIVATE, null);
atMaterial.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sql = new StringBuilder();
sql.append("UPDATE MATERIAIS SET ");
sql.append("nome = '"+nome.getText().toString()+"', ");
sql.append("modE = "+modE.getText().toString()+" , ");
sql.append("dVol = "+dVol.getText().toString()+" ");
sql.append("WHERE _id = "+spnMaterial.getSelectedItemId());
Estrutura.execSQL(sql.toString());
createSpinner(nome, modE, dVol, spnMaterial, atMaterial, excMaterial, spnMaterial.getSelectedItemPosition());
}
});
}
public void createExcMaterial(final EditText nome, final EditText modE, final EditText dVol, final Spinner spnMaterial, final ImageButton atMaterial, final ImageButton excMaterial) {
final SQLiteDatabase Estrutura = openOrCreateDatabase("Estrutura.db", getBaseContext().MODE_PRIVATE, null);
excMaterial.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String sql = "DELETE FROM MATERIAIS WHERE _id = "+spnMaterial.getSelectedItemId();
Estrutura.execSQL(sql);
if(spnMaterial.getSelectedItemPosition() == spnMaterial.getAdapter().getCount()-1) {
createSpinner(nome, modE, dVol, spnMaterial, atMaterial, excMaterial, spnMaterial.getSelectedItemPosition()-1);} else {
createSpinner(nome, modE, dVol, spnMaterial, atMaterial, excMaterial, spnMaterial.getSelectedItemPosition()); }
}
});
}
public void isEnabled(boolean is, EditText nome, EditText modE, EditText dVol, ImageButton atMaterial, ImageButton excMaterial) {
nome.setEnabled(is);
modE.setEnabled(is);
dVol.setEnabled(is);
atMaterial.setEnabled(is);
excMaterial.setEnabled(is);
}
}
正如你所看到的,这些课程收到了很多论点,我想让它看起来更好。由于它们有不同的类型,我不确定数组是否是最好的方法。
答案 0 :(得分:1)
简答:不。 你不能直接将视图放在一个包中,我不推荐它。您只能通过bundle传递Serializable或Parcelable对象。尝试序列化View然后稍后检索它将导致问题,如果视图的上下文自您放入束中以来已被破坏或损坏。由于垃圾收集器未经您的同意清理您的视图,您不希望发现随机崩溃。如果您尝试从两个不同的上下文编辑同一个View对象,则会遇到困难。
我有几个“设置”类
这些设置类只是普通的POJO,你可以获得对它的引用吗?然后像普通参数一样传递它。(因为它看起来像你的例子)
Manipulator manipulator = new ();
manipulator.createAddMaterial(customView1, customView2, material1, ...)
我想知道的是,是否有办法将它们包装在一个类似Bundle的参数中
如果您确实需要将视图传递给另一个Activity(实质上是另一个上下文),那么您最好的选择是从视图中提取相关数据并仅传递该视图。
MyViewData myViewData = new MyViewData();
myViewData.setText(view.getText());
myViewData.setMaterial(view.getMaterial());
Intent i = new Intent(this, NextActivity.class);
intent.putExtra(MY_VIEW_DATA, myViewData);
startActivity(intent);
然后您可以在其他活动中收到它,您将获得所需的相关数据。
我想知道的是,是否有办法将它们包装在一个类似Bundle的参数中,所以代码不会太乱。
如果你想保持干净,那么创建一个“霸主”对象,该对象将包含有关你的观点的所有相关数据。它的功能类似于Map,因此您可以检索有关特定视图的信息。在这种情况下,地图的“值”将是MyViewData
个对象。这个霸主对象可以干净地捆绑到Intent中。
例如。
MyViewOverlord mvo = new MyViewOverlord();
mvo.saveDataForView("key1", myViewData1);
mvo.saveDataforView("key2" myViewData2);
intent.putExtra("ViewOverlord", mvo);
(您可以在此处查看类似问题的答案:How can i pass image view between activities in android)