0

i have two activity files in my code, and the first activity file loads the layout search, and the second file loads layout list. I have a textbox in layout search and enter some text. I want to use this text in my second activity file but i can not reach it since it is in the layout search. How can i do this? Here the first activity file, here there is an EditText item called searchedText, and i want to use it in the second activity file.

public class SearchActivity extends Activity{

public EditText searchedText;
public RadioGroup radioGroup;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

}

public void onStart(){
    super.onStart();
    searchedText = (EditText) findViewById(R.id.searchText);
    Button searchinSearchButton = (Button)findViewById(R.id.searchInSearch);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

    searchinSearchButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            String searched=searchedText.getText().toString();
            Intent myIntent = new Intent(v.getContext(),   
                            SearchListActivity.class);
            startActivityForResult(myIntent, 1);

        } 

    });
}

}

And here is the second activity file:

public class SearchListActivity extends Activity{

public DatabaseAdapter db;
public ArrayList<String> myList;
public ListView listview;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    db = new DatabaseAdapter(this);
    myList = new ArrayList<String>();

    getContacts();

    // Example of retrieving tweets of the user "mashable" and adding them to   
myList

    /*
    ArrayList<Tweet> tweets= new ArrayList<Tweet>();
    tweets.addAll(Twitter.getTimeline("mashable", 10));
    for(Tweet t: tweets){
        myList.add(t.username +  ": " + t.message + " Tweet id: "+ t.id);
    }
    */
    printList();

}

public void printList(){

    listview = (ListView)findViewById(R.id.contactcListView);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);

    listview.setAdapter(adapter);
}
public void getContacts() {

    db.open();
    Cursor c = db.getContactbyName("y");
    if (c.moveToFirst()) {
        do {
            DisplayContact(c);

        } while (c.moveToNext());
    }
    db.close();
}

public void DisplayContact(Cursor c) {

    String entry = "";
    // if you add another attribute to your table, you need to change 3 into x
    for (int i=1; i<5;i++){
        entry += c.getString(i) + "\n";
    }
    myList.add(entry);
}

}

In this second activity file, you can see the getContacts() method. There, i search by Cursor c = db.getContactbyName("y"); but instead of "y", i want to search whatever user enters the texbox, whic is in the 1st activity file called searchedText. How can i do this?

Thanks

Jave
  • 31,598
  • 14
  • 77
  • 90

1 Answers1

0

Send the text as an extra in your Intent when you start your second activity.

searchinSearchButton.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        String searched=searchedText.getText().toString();
        Intent myIntent = new Intent(v.getContext(),   
                        SearchListActivity.class);
        myIntent.putExtra("SEARCH_STRING",searched);
        startActivityForResult(myIntent, 1);

    } 

});

And in your onCreate get the extra. You could use Intent.getStringExtra(String name) In other words:

  mySearched = getIntent().getStringExtra("SEARCH_STRING");

Just make sure to see if anything is null before using it.

David Olsson
  • 8,085
  • 3
  • 30
  • 38
  • 1
    @bigO When an answer solves your problem, please mark it as accepted by clicking the tick-mark to the left of the answer instead of adding "solved" to the title. – Jave Mar 26 '13 at 12:44
  • Thanks, i know i just had to wait for a few minutes because system did not let me accept it –  Mar 26 '13 at 12:48