1

我试图做一个应用程序,在第一个活动中,用户可以标记几个复选框,从 1 到 7 或 8(这取决于最终版本)。当用户标记了他的选项并单击“搜索”按钮时,我想显示一个列表,根据标记的项目显示不同的对象。

我的对象是自定义对象“地面”,它有许多特征,如字符串或 int-boolean(1 或 0),存储在数据库 sqlite 中。在我的主要活动中,我使用了很多 if else 来了解检查了多少复选框,我通过 extras 将这些信息放入意图中,在我的第二个活动(列表)中,我检索了这个 extras,我做另一个 if else 知道应用程序必须显示什么列表。我认为这种方法非常初级,因为我必须在这两个活动上做很多 if else 。我试图在第一个活动中创建一个返回列表的 dbquery,并通过意图发送此列表,但我遵循了很多教程,并且总是有一个正确数量的对象列表,但总是相同的覆盖对象!

有什么想法吗?

先感谢您!

第一项活动

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"});
            }
4

0 回答 0