我试图做一个应用程序,在第一个活动中,用户可以标记几个复选框,从1到7或8(取决于最终版本)。当用户标记了他的选项并点击“搜索”按钮时,我想显示一个列表,显示不同的对象,具体取决于标记的项目。
我的对象是自定义对象“ground”,它有许多特性,比如字符串或int-boolean(1或0),存储在数据库sqlite中。 在我的主要活动中,我使用了很多if if来知道检查了多少复选框,我通过附加功能将这些信息置于意图中,并且在我的第二个活动(列表)中,我检索了这些额外内容,如果不知道我还要做其他的应用必须显示的列表。 我认为这种方法很简陋,因为我必须在两个活动上做很多其他事情。 我试图在第一个活动中创建一个返回列表的dbquery,并通过intent发送这个列表,但是我已经遵循了很多教程,并且我总是有一个正确数量的对象列表,但总是相同的被覆盖的对象!
有关做什么的任何想法?
提前谢谢!
第一项活动
public class MainActivity extends Activity {
Button bafegeix, bcerca, bneteja;
ListView llista;
RadioGroup grup;
RadioButton casa, terreny;
CheckBox cbaigua, cblavabos, cbcobert, cbdutxes, cbcuina, cbadap;
protected Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prova__db);
bcerca = (Button) findViewById (R.id.cerca);
bneteja = (Button) findViewById (R.id.neteja);
grup = (RadioGroup)findViewById (R.id.grup);
casa = (RadioButton)findViewById (R.id.casa);
terreny = (RadioButton)findViewById (R.id.terreny);
cbaigua = (CheckBox)findViewById (R.id.aigua);
cblavabos = (CheckBox)findViewById (R.id.lavabos);
cbcobert = (CheckBox)findViewById (R.id.cobert);
cbdutxes = (CheckBox)findViewById (R.id.dutxes);
cbcuina = (CheckBox)findViewById (R.id.cuina);
cbadap = (CheckBox)findViewById (R.id.adaptada);
bcerca.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(terreny.isChecked()){
Intent iterreny = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert", "cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if (cbaigua.isChecked()&& cblavabos.isChecked()&&cbcobert.isChecked()&&cbdutxes.isChecked()) {
Intent iterreny = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert","cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
} else if(cbaigua.isChecked()&&cblavabos.isChecked()&&cbcobert.isChecked()){
Intent iterreny = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert","cobert");
startActivity(iterreny);
}else if(cblavabos.isChecked()&&cbcobert.isChecked()&&cbdutxes.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("cobert", "cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if(cbaigua.isChecked()&&cbcobert.isChecked()&&cbdutxes.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("cobert", "cobert");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if(cbaigua.isChecked()&&cblavabos.isChecked()&&cbdutxes.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
iterreny.putExtra("dutxes", "dutxes");
startActivity(iterreny);
}else if (cbaigua.isChecked()&&cblavabos.isChecked()){
Intent iterreny2 = new Intent(getApplicationContext(), LlistaTerrenys.class);
iterreny.putExtra("aigua", "aigua");
iterreny.putExtra("lavabos", "lavabos");
startActivity(iterreny);
第二次活动
公共类LlistaTerrenys扩展了Activity {
ListView llista;
TextView numelements;
ArrayList<Terreny>list;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_list_contactos);
llista = (ListView)findViewById(R.id.listView1);
numelements = (TextView)findViewById(R.id.numelements);
Bundle extras = getIntent().getExtras();
String aigua = extras.getString("aigua");
String lavabos = extras.getString("lavabos");
String cobert = extras.getString("cobert");
String dutxes = extras.getString("dutxes");
String seleccio = extras.getString("noseleccio");
if (aigua!=null && lavabos!=null && cobert!=null && dutxes!=null) {
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
mydb.getAllDataTerrenyTotesCaracteristiques();
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyTotesCaracteristiques());
llista.setAdapter(aa);
} else if(aigua!=null && lavabos!=null && cobert!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaLavabosCobert());
llista.setAdapter(aa);
}else if(lavabos!=null && cobert!=null && dutxes!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyLavabosCobertDutxes());
llista.setAdapter(aa);
}else if(aigua!=null && cobert!=null && dutxes!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaCobertDutxes());
llista.setAdapter(aa);
}else if(aigua!=null && lavabos!=null && dutxes!=null){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaLavabosDutxes());
llista.setAdapter(aa);
}else if (aigua!=null && lavabos!=null ){
final UtilidadesSQL mydb = new UtilidadesSQL(getApplicationContext());
AdapterTerreny aa = new AdapterTerreny(this, mydb.getAllDataTerrenyAiguaLavabos());
llista.setAdapter(aa);
这里有从SQL中检索数据的不同方法
public Cursor getAllDataTerrenyAigua () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where aigua = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyLavabos () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where lavabos = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyCobert () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where cobert = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyDutxes () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where dutxes = ?");
return db.rawQuery(buildSQL, new String[]{"1"});
}
public Cursor getAllDataTerrenyTotesCaracteristiques () {
SQLiteDatabase db = getReadableDatabase();
String buildSQL = ("select * from terrenys where aigua=? AND cobert=? AND dutxes=? AND lavabos=?");
return db.rawQuery(buildSQL, new String[]{"1","1","1","1"});
}