0

So I have a ListAdapter which has various options and what I want is that when a user clicks on the ListAdapter an activity starts which has an EditText and the EditText should be automatically filled with option which was, clicked on for the ListAdapter.

Currently I have Main.java:

public class Main extends ListActivity{
    String options[] = {"option1","option2"};
    protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setListAdapter(new ArrayAdapter<String>   (Main.this,android.R.layout.simple_list_item_1,options);
    }

    protected void onListItemClick(ListView l,View v, int position, long id){
       super.onListItemClick(l,v,position,id);
       try{
       Class theClass = Class.forname("com.example.SecondActivity");
       Intent oI = new Intent(Main.this,theClass);
       startActivity(oI);
       }catch(ClassNotFoundException e){e.printStackTrace();}
    }

SecondActivity.java (This contains the activity with the edit text):

public class SecondActivity extends Activity{
   protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContextView(R.layout.secondactivity)
      EditText edt = (EditText) findViewById(R.id.edt1);
    }

What all extra do I need to do so that this works. All the help is deeply appreciated.

user987339
  • 10,519
  • 8
  • 40
  • 45
Massa
  • 69
  • 1
  • 8

3 Answers3

0

While starting an activity, you can send more information to that activity like an integer or String value. It can be send in the following way:

Intent addIntent = new Intent();
addIntent.putExtra(String name, String value).

So, you can send the name to your new activity. Then you need to get this value in onCreate function using getExtra() function. and can use to fill the editText.

Below link will give you more insight:

How to use putExtra() and getExtra() for string data

Community
  • 1
  • 1
Sushil
  • 8,250
  • 3
  • 39
  • 71
0

Try:

protected void onListItemClick(ListView l,View v, int position, long id){
   super.onListItemClick(l,v,position,id);
   try{
   Class theClass = Class.forname("com.example.SecondActivity");
   Intent oI = new Intent(Main.this,theClass);

   oI.putExtra("selectedText", options[position]);

   startActivity(oI);
   }catch(ClassNotFoundException e){e.printStackTrace();}
}


public class SecondActivity extends Activity{
protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContextView(R.layout.secondactivity)

  EditText edt = (EditText) findViewById(R.id.edt1);
   if(getIntent()!=null && getIntent().getExtras()!=null)
    {
        String value = getIntent().getStringExtra("selectedText");
        edt.setText(value);
    }
}

let me know if it works ok, I haven't tested it.

Dan Cuc
  • 76
  • 4
0

Solved the problem by making use of putExtra instead of getExtras

Massa
  • 69
  • 1
  • 8