0

I am trying to pass a value on my ListView to my second activity using Intents. I am not sure how to pass the text value to the second activity my Intent leads to. Right now my Intent is able to connect to the second activity on tap but it doesn't pass the string of the tapped value.

I feel that I need to pass something into my launchEditItem() but I am not sure what. These are the two functions I am dealing with right now.

private void launchEditItem() {
    Intent i = new Intent(this, EditItemActivity.class);
    i.putExtra("itemOnList", );     // list item into edit text
    startActivity(i);
}

private void setupEditItemListener() {          // on click, run this function to display edit page
    lvItems.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
            launchEditItem();
        }
    });
}

I'm not sure what value to place into the i.putExtra(), but I think I need to pass an argument into the launchEditItem().

This is what is currently in my second Activity:

public class EditItemActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_item);
        Intent i = getIntent();
        String ItemToEdit = i.getStringExtra("itemOnList");
        // place into EditText using ItemToEdit
    }

I'm also not sure how to place this String value (ItemToEdit) into an EditText box. I'm new to android dev so I'm learning as much as I can thank you!

* EDIT *

I guess I'm a bit too vague in my question. Here is the entire code of the small app I am working on

public class ToDoActivity extends Activity {
    private ArrayList<String> todoItems;        
    private ArrayAdapter<String> todoAdapter;       // declare array adapter which will translate the piece of data to teh view
    private ListView lvItems;                   // attach to list view
    private EditText etNewItem;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do);
        etNewItem = (EditText) findViewById(R.id.etNewItem);
        lvItems = (ListView) findViewById(R.id.lvItems);        // now we have access to ListView
        //populateArrayItems();                 // call function
        readItems();        // read items from file
        todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);   //create adapter
        lvItems.setAdapter(todoAdapter);        // populate listview using the adapter 
        //todoAdapter.add("item 4");
        setupListViewListener();
        setupEditItemListener();

    }

    private void launchEditItem() {
        Intent i = new Intent(this, EditItemActivity.class);
        i.putExtra("itemOnList", );     // list item into edit text
        startActivity(i);
    }

    private void setupEditItemListener() {          // on click, run this function to display edit page
        lvItems.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
                launchEditItem();
            }

        });
    }

    private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
                todoItems.remove(pos);
                todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view
                writeItems();   
                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.to_do, menu);
        return true;
    }

    public void onAddedItem(View v) {
        String itemText = etNewItem.getText().toString();
        todoAdapter.add(itemText);  // add to adapter
        etNewItem.setText("");      //clear edit text
        writeItems();       //each time to add item, you want to write to file to memorize
    }

    private void readItems() {
        File filesDir = getFilesDir();  //return path where files can be created for android
        File todoFile = new File(filesDir, "todo.txt");
        try {
            todoItems = new ArrayList<String>(FileUtils.readLines(todoFile));   //populate with read
        }catch (IOException e) {    // if files doesn't exist  
            todoItems = new ArrayList<String>();
        }
    }

    private void writeItems() {
        File filesDir = getFilesDir();  //return path where files can be created for android
        File todoFile = new File(filesDir, "todo.txt");
        try {
            FileUtils.writeLines(todoFile, todoItems);  // pass todoItems to todoFile
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Liondancer
  • 15,721
  • 51
  • 149
  • 255

5 Answers5

5
    String[] link_list;
   int currenttrack=0;

        link_list=new String[]{
            "W-TE_Ys4iwM",//1
            "oozgmH3ZP14",//2
            "o_v9MY_FMcw",//3
            "36mCEZzzQ3o",//4

}

First activity

list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            currenttrack=arg2;


            Intent videoIntent=new Intent(MainActivity.this,VideoView.class);
            videoIntent.putExtra("filename", link_list[currenttrack]);
            startActivity(videoIntent);
        }
    });

In your Second Activity

    // getting intent data

    // get intent data
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    filename = extras.getString("filename");
    Log.e("File Name", filename);

and your done :)

Jeffy Lazar
  • 1,903
  • 13
  • 20
2

In your current Activity, create a new Intent:

Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}
coder
  • 13,002
  • 31
  • 112
  • 214
2

I am not sure if i understood where is the value. Well if the value is in EditText do something like:

private void launchEditItem(String text) {
    Intent i = new Intent(this, EditItemActivity.class);
    i.putExtra("itemOnList", text);     // list item into edit text
    startActivity(i);
}

private void setupEditItemListener() {          // on click, run this function to display edit page
    lvItems.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
            EditText editView = (EditText) item.findById(R.id.ItemToEdit);
            String text = editView != null ? editView.getText().toString() : "";
            launchEditItem(text);
        }
    });
}
betorcs
  • 2,771
  • 22
  • 28
1

1 - I have some doubt with:

i.putExtra("itemOnList", );

You'd better pass a value:

i.putExtra("itemOnList", "something");

2 - To valorize a control, you must obtain a reference to it first. Something like:

EditText edt =
    (EditText) findViewById(R.id.activity_edit_item_my_EditText); // or whatever id you assigned to it  (it MUST HAVE AN ID)

Do it AFTER setContentView().

Then you can set it's text like:

edt.setText(ItemToEdit); // Now it should contain "something"

Just as simple as that

[EDIT]

If you aren't sure what to pass so is to pass in the "tapped" value in the ListView into the putExtra, modify your listview click handler code:

list.setOnItemClickListener
(
    new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub
            Intent videoIntent = new Intent(MainActivity.this, VideoView.class);
            videoIntent.putExtra("filename", (((TextView) v).getText().toString());
            startActivity(videoIntent);
        }
    }
);

It should work immediately.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I wasn't sure what to pass so I left it blank. What I am trying to do is to pass in the "tapped" value in the ListView into the `putExtra` – Liondancer Dec 09 '13 at 11:56
0

01: Current Activity

String pass_value ="value";

Intent intent = new Intent(getApplicationContext(),NewActivity.class);
intent.putExtra("var_name",pass_value);
startActivity(intent);

02: New Activity

String value = getIntent().getExtras().getString("var_name");
hexhad
  • 1,139
  • 12
  • 14