Streetboy
Streetboy

Reputation: 4401

Passing parameters to other activities android

I have a ListView with some info, when i press on LisView row i want to go to other activity and pass row argument to it.

For now i have this:

@Override protected void onListItemClick(ListView l, View v, int position, long id) {

Thread my_files_thread = new Thread(){

    //start thread
    public void run (){
        try{

            startActivity(new Intent("android.app.reader.FILES"));


        }catch (Exception e) {
                // TODO: handle exception
            e.printStackTrace();
            }finally{

                //finish();
            }
    }

};// Brackets means that thread is closing

my_files_thread.start();

}

Files class:

public class Files extends Activity{

    public String name = null;

    @Override
       public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.files);  

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        name = name;
    }
}

So i want to call setter and then open that activity. I am able to do this ? Or there are other way i should do.

Thanks.

Upvotes: 0

Views: 1047

Answers (2)

AndroDev
AndroDev

Reputation: 3274

First of all I don't understand why you are using thread in onListItemClick. I don't think any use of it.

Use the following code to start a new activity from onListItemClick

Intent intent = new Intent("android.app.reader.FILES");  
intent.putExtra("whatever", "value to pass");
startActivity(intent);

And retrieve this value in Files activity using following code:

public void onCreate(Bundle saveInstanceState) {
    Intent fromIntent = getIntent(); //returns intent which started this activity
    String myValue = fromIntent.getStringExtra("whatever"); //returns the value passed by old activity
...
}

Upvotes: 2

Shashank Kadne
Shashank Kadne

Reputation: 8101

Use putExtra to add the argument to the intent.

Intent i= new Intent("android.app.reader.FILES");
    i.putExtra("arg", position);
    startActivity(i);

In the destination Activity use

Intent i1 = getIntent();
int pos = i1.getIntExtra("arg")

to get the value.

Upvotes: 3

Related Questions