0

I am making an app to search a database and i have a part where i type in a search detail and the name of the possible results are displayed on buttons in a new activity. It works fine first time round but if i press back from that activity then try to search for something different then the last button results but the old results are still there with the new ones.

public class search_page extends Activity implements OnClickListener {
    static int number;
    static int[] numberArray = new int[8];
    static int looped;
    static int typeFound = 0;
    TextView editText1;
    Button search_button, search_button2 ; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_page);

        editText1 = (EditText) findViewById(R.id.editText1);
        search_button = (Button) findViewById(R.id.search_button);
        search_button2 = (Button) findViewById(R.id.search_button2);
        search_button.setOnClickListener(this);
        search_button2.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.search_button:
            sqlStuff search1 = new sqlStuff(search_page.this);
            boolean found = false;
            String Systname = editText1.getText().toString();

            search1.open();
            String[] IDSysNames = search1.getIDSysName();
            search1.close();
            for(int i = 0; i < IDSysNames.length; i++) {
                if(Systname.equalsIgnoreCase(IDSysNames[i].toString())) {
                    found = true;
                    number = i;
                }
            }

            if(found==true) {
                Intent search = new Intent("com.MC.ChemPal.RESULT");
                startActivity(search);
            }
            else {
                Dialog d = new Dialog(this);
                d.setTitle("result not found");
                TextView tv = new TextView(this);
                d.setContentView(tv);
                d.show();
            }
            break;

        case R.id.search_button2:
            boolean found2 = false;
            boolean found3 = false;
            sqlStuff search2 = new sqlStuff(search_page.this);
            search2.open();
            String entry = editText1.getText().toString();
            String[] IDSysNames2 = search2.getIDSysName();
            String[] IDGroup = search2.getIDGroup();
            String[] IDMP = search2.getIDMP();
            String[] IDBP = search2.getIDBP();
            String[] IDComname = search2.getIDComname();
            String[] IDElement = search2.getIDElement();
            String[] IDMolarmass = search2.getIDMOLARMASS();
            search2.close();

            for(int i = 0; i < IDSysNames2.length; i++) {
                if(entry.equalsIgnoreCase(IDSysNames2[i].toString())) {
                  found2 = true;
                  found3 = true;
                  typeFound = 1; 
                  numberArray[looped] = i;
                }
                if(entry.equalsIgnoreCase(IDGroup[i].toString())) {
                  found2 = true;
                  found3 = true;
                  typeFound = 2;
                  numberArray[looped] = i;
                }
                if(entry.equalsIgnoreCase(IDMP[i].toString())) {
                  found2 = true;
                  found3 = true;
                  typeFound = 3;
                  numberArray[looped] = i;
                }
                if(entry.equalsIgnoreCase(IDBP[i].toString())) {
                  found2 = true;
                  found3 = true;
                  typeFound = 4;
                  numberArray[looped] = i;
                }

                if(entry.equalsIgnoreCase(IDComname[i].toString())) {
                  found2 = true;
                  found3 = true;
                  typeFound = 5;
                  numberArray[looped] = i;
                }

                if(IDElement[i].toString().contains(entry)) {
                  found2 = true;
                  found3 = true;
                  typeFound = 6;
                  numberArray[looped] = i;
                }

                if(entry.equalsIgnoreCase(IDMolarmass[i].toString())) {
                  found2 = true;
                  found3 = true;
                  typeFound = 7;
                  numberArray[looped] = i;
                }

                if(found2 == true) {
                    looped++;
                }
                found2 = false;

            }
            if (found3==true) {
                Intent searching2 = new    Intent("com.MC.ChemPal.SEARCHLIST"); 
                startActivity(searching2);
            }
            else {
                Dialog d = new Dialog(this);
                d.setTitle("result not found");
                TextView tv = new TextView(this);
                d.setContentView(tv);
                d.show();
            }

            break;
        }
    }

    public static int returnNum() {
        return number;
    }

    public static int[] returnNumArray() {
        return numberArray;
    }

    public static int returnlooped() {
       return looped;
    }
}

That activity then links to this one.

public class searchlist extends Activity implements OnClickListener {
    static int buttonPress = 0;
    int loops = 0;

    public void onCreate(Bundle savedinstance){
        super.onCreate(savedinstance);
        setContentView(R.layout.searchlist);

        Button[] mybuttons = new Button[10];

        mybuttons[0] = (Button) findViewById(R.id.search1);
        mybuttons[1] = (Button) findViewById(R.id.search2);
        mybuttons[2] = (Button) findViewById(R.id.search3);
        mybuttons[3] = (Button) findViewById(R.id.search4);
        mybuttons[4] = (Button) findViewById(R.id.search5);
        mybuttons[5] = (Button) findViewById(R.id.search6);
        mybuttons[6] = (Button) findViewById(R.id.search7);
        mybuttons[7] = (Button) findViewById(R.id.search8);
        mybuttons[8] = (Button) findViewById(R.id.search9);
        mybuttons[9] = (Button) findViewById(R.id.search10);
        int i = 0;

       if(!mybuttons[0].getText().equals("-"))
       {
        mybuttons[0].setText("-");
        mybuttons[0].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[1].getText().equals("-"))
       {
        mybuttons[1].setText("-");
        mybuttons[1].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[2].getText().equals("-"))
       {
        mybuttons[2].setText("-");
        mybuttons[2].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[3].getText().equals("-"))
       {
        mybuttons[3].setText("-");
        mybuttons[3].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[4].getText().equals("-"))
       {
        mybuttons[4].setText("-");
        mybuttons[4].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[5].getText().equals("-"))
       {
        mybuttons[5].setText("-");
        mybuttons[5].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[6].getText().equals("-"))
       {
        mybuttons[6].setText("-");
        mybuttons[6].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[7].getText().equals("-"))
       {
        mybuttons[7].setText("-");
        mybuttons[7].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[8].getText().equals("-"))
       {
        mybuttons[8].setText("-");
        mybuttons[8].setVisibility(View.INVISIBLE);
       }
       if(!mybuttons[9].getText().equals("-"))
       {
        mybuttons[9].setText("-");
        mybuttons[9].setVisibility(View.INVISIBLE);
       }


        sqlStuff searching = new sqlStuff(searchlist.this);

        searching.open();
        String[] IDSysNames = searching.getIDSysName();
        loops = search_page.returnlooped();
        int[] teacup = search_page.returnNumArray();
        searching.close();


        for(i=0; i < loops; i++ )
        {
            if(IDSysNames[teacup[i]] != null)
            {
            mybuttons[i].setText(IDSysNames[teacup[i]]);
            }
        }

        if(!mybuttons[0].getText().equals("-"))
        {
        mybuttons[0].setOnClickListener(this);
        mybuttons[0].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[1].getText().equals("-"))
        {
        mybuttons[1].setOnClickListener(this);
        mybuttons[1].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[2].getText().equals("-"))
        {
        mybuttons[2].setOnClickListener(this);
        mybuttons[2].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[3].getText().equals("-"))
        {
        mybuttons[3].setOnClickListener(this);
        mybuttons[0].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[4].getText().equals("-"))
        {
        mybuttons[4].setOnClickListener(this);
        mybuttons[4].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[5].getText().equals("-"))
        {
        mybuttons[5].setOnClickListener(this);
        mybuttons[5].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[6].getText().equals("-"))
        {
        mybuttons[6].setOnClickListener(this);
        mybuttons[6].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[7].getText().equals("-"))
        {
        mybuttons[7].setOnClickListener(this);
        mybuttons[7].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[8].getText().equals("-"))
        {
        mybuttons[8].setOnClickListener(this);
        mybuttons[8].setVisibility(View.VISIBLE);
        }
        if(!mybuttons[9].getText().equals("-"))
        {
        mybuttons[9].setOnClickListener(this);
        mybuttons[9].setVisibility(View.VISIBLE);
        }
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {

        case 
        R.id.search1:
         buttonPress = 0;
         Intent search = new Intent("com.MC.ChemPal.RESULT2");
         startActivity(search);
         break;

        case 
        R.id.search2:
            buttonPress = 1;
            Intent search2 = new Intent("com.MC.ChemPal.RESULT2");
            startActivity(search2);

            break;

        case 
        R.id.search3:
            buttonPress = 2;
            Intent search3 = new Intent("com.MC.ChemPal.RESULT2");
            startActivity(search3);
            break;


        case 
        R.id.search4:
            buttonPress=3;
            Intent search4 = new Intent("com.MC.ChemPal.RESULT2");
            startActivity(search4);
            break;


        case 
        R.id.search5:
            buttonPress=4;
            Intent search5 = new Intent("com.MC.ChemPal.RESULT2");
         startActivity(search5);
            break;

        case 
        R.id.search6:
            buttonPress=5;
            Intent search6 = new Intent("com.MC.ChemPal.RESULT2");
         startActivity(search6);
            break;


        case 
        R.id.search7:
            buttonPress=6;
            Intent search7 = new Intent("com.MC.ChemPal.RESULT2");
           startActivity(search7);
            break;

        case
        R.id.search8:
            buttonPress=7;
            Intent search8 = new Intent("com.MC.ChemPal.RESULT2");
            startActivity(search8);
            break;

        case
        R.id.search9:
            buttonPress=8;
            Intent search9 = new Intent("com.MC.ChemPal.RESULT2");
            startActivity(search9);
            break;

        case
        R.id.search10:
            buttonPress=9;
            Intent search10 = new Intent("com.MC.ChemPal.RESULT2");
            startActivity(search10);
            break;

        }

    }

    public static int getButtonPress() {
        return buttonPress;
    }

     public void onResume(){
    super.onResume();
    setContentView(R.layout.searchlist);

  Button[] mybuttons = new Button[10];
   onResume();
   mybuttons[0] = (Button) findViewById(R.id.search1);
   mybuttons[1] = (Button) findViewById(R.id.search2);
   mybuttons[2] = (Button) findViewById(R.id.search3);
   mybuttons[3] = (Button) findViewById(R.id.search4);
   mybuttons[4] = (Button) findViewById(R.id.search5);
   mybuttons[5] = (Button) findViewById(R.id.search6);
   mybuttons[6] = (Button) findViewById(R.id.search7);
   mybuttons[7] = (Button) findViewById(R.id.search8);
   mybuttons[8] = (Button) findViewById(R.id.search9);
   mybuttons[9] = (Button) findViewById(R.id.search10);
   int i = 0;


   if(!mybuttons[0].getText().equals("-"))
   {
    mybuttons[0].setText("-");
    mybuttons[0].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[1].getText().equals("-"))
   {
    mybuttons[1].setText("-");
    mybuttons[1].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[2].getText().equals("-"))
   {
    mybuttons[2].setText("-");
    mybuttons[2].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[3].getText().equals("-"))
   {
    mybuttons[3].setText("-");
    mybuttons[3].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[4].getText().equals("-"))
   {
    mybuttons[4].setText("-");
    mybuttons[4].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[5].getText().equals("-"))
   {
    mybuttons[5].setText("-");
    mybuttons[5].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[6].getText().equals("-"))
   {
    mybuttons[6].setText("-");
    mybuttons[6].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[7].getText().equals("-"))
   {
    mybuttons[7].setText("-");
    mybuttons[7].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[8].getText().equals("-"))
   {
    mybuttons[8].setText("-");
    mybuttons[8].setVisibility(View.INVISIBLE);
   }
   if(!mybuttons[9].getText().equals("-"))
   {
    mybuttons[9].setText("-");
    mybuttons[9].setVisibility(View.INVISIBLE);
   }


    sqlStuff searching = new sqlStuff(searchlist.this);

    searching.open();
    String[] IDSysNames = searching.getIDSysName();
    loops = search_page.returnlooped();
    int[] teacup = search_page.returnNumArray();
    searching.close();


    for(i=0; i < loops; i++ )
    {
        if(IDSysNames[teacup[i]] != null)
        {
        mybuttons[i].setText(IDSysNames[teacup[i]]);
        }
    }

    if(!mybuttons[0].getText().equals("-"))
    {
    mybuttons[0].setOnClickListener(this);
    mybuttons[0].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[1].getText().equals("-"))
    {
    mybuttons[1].setOnClickListener(this);
    mybuttons[1].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[2].getText().equals("-"))
    {
    mybuttons[2].setOnClickListener(this);
    mybuttons[2].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[3].getText().equals("-"))
    {
    mybuttons[3].setOnClickListener(this);
    mybuttons[0].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[4].getText().equals("-"))
    {
    mybuttons[4].setOnClickListener(this);
    mybuttons[4].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[5].getText().equals("-"))
    {
    mybuttons[5].setOnClickListener(this);
    mybuttons[5].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[6].getText().equals("-"))
    {
    mybuttons[6].setOnClickListener(this);
    mybuttons[6].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[7].getText().equals("-"))
    {
    mybuttons[7].setOnClickListener(this);
    mybuttons[7].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[8].getText().equals("-"))
    {
    mybuttons[8].setOnClickListener(this);
    mybuttons[8].setVisibility(View.VISIBLE);
    }
    if(!mybuttons[9].getText().equals("-"))
    {
    mybuttons[9].setOnClickListener(this);
    mybuttons[9].setVisibility(View.VISIBLE);
    }
    }   

 }
  • Too much code and a vague question. Why can't you just reset everything in `onResume()`? – Simon Apr 29 '13 at 18:34
  • What if you had 1000 buttons instead of 10? Use the `for` loop for your buttons. – vortexwolf Apr 29 '13 at 19:00
  • Basically for more detail. What i'm getting is if i find for example 2 search results then mybuttons[0] and mybuttons[1] become visible and with correct setText. That is fine but when i press back and try to make another search then it my buttons[2] and mybuttons[3] become visible and setText and mybuttons[0] and mybuttons[1]have the same text and remain visible and that is what im trying to correct. – user1844616 Apr 29 '13 at 19:29

1 Answers1

0

You changed the state of the buttons within the view. Moving to another activity and back won't reset the view. It maintains the states within your app, which would make not knowing the state far more of a problem.

Where you're setting the states programatically, I would suggest an override on onResume to set the states of your buttons to where you want them. This will be called when your activity is initially started, if it is restarted, and each time your activity is brought to the foreground.

Refer to: Android life cycle activities

Additional information: You posted 2 activities (search_page and searchlist) where search_page clearly calls searchlist. You mentioned that your problem is hitting the back button. The problem isn't actually hitting the back -- that's obviously working, it is what the activity does when it resumes.

From your last comment it looks to me like you're making the buttons visible if they have text in them, so the problem isn't making them visible -- that's working, the problem is that the wrong buttons have text.

When should you clear them? When you return to search_page? If so, make your onResume in your search_page clear the text in all buttons.

Community
  • 1
  • 1
Dave
  • 300
  • 1
  • 7
  • i basically copied the code into a onResume() underneath and it causes crashing. – user1844616 Apr 29 '13 at 19:29
  • Which activity (search_page or searchlist) did you change? I thought you were on search_page, which then goes to searchlist, so your problem is returning to search_page...no? The activity you're returning to is the one you need to change. – Dave Apr 29 '13 at 20:15
  • i'm making a search on search_page then im going to searchlist then back to search_page again and making a different search which then goes to searchlist again – user1844616 Apr 29 '13 at 20:22
  • For more detail. What i'm getting is if i find for example 2 search results then mybuttons[0] and mybuttons[1] become visible and with correct setText. That is fine but when i press back and try to make another search then it my buttons[2] and mybuttons[3] become visible and setText and mybuttons[0] and mybuttons[1]have the same text and remain visible and that is what im trying to correct – user1844616 Apr 29 '13 at 20:26
  • i've added the onResume which i placed in the code above but it now errors when i try to even search once. – user1844616 Apr 29 '13 at 21:19
  • another problem is mybutton[3] doesnt become visible ever for some reason. – user1844616 Apr 29 '13 at 21:44
  • also now tried to reset values when onpause() is triggered but still no luck – user1844616 Apr 29 '13 at 22:48