1

I am having trouble receiving the position or the value of a clicked ListViewItem in a SecondActivity.

My MainActivity:

public class MainActivity extends Activity {

String[] ingredients;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ingredients = getResources().getStringArray(R.array.ingredients);

    final ListView ingredientList = (ListView) findViewById(R.id.ingredientList);
    ArrayAdapter <String> listAdapter = new ArrayAdapter <String> (this,android.R.layout.simple_list_item_1, ingredients);
    ingredientList.setAdapter(listAdapter);

    ingredientList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView <?> parent, View view, int pos, long id) {

            int itemPosition     = pos;
            String  itemName    = (String) ingredientList.getItemAtPosition(pos);

            // THIS TOAST WORKS FINE: Toast.makeText(getApplicationContext(), "Position: " + itemPosition + "  ListItem: " + itemName, Toast.LENGTH_LONG).show();

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}

In the SecondActivity I want to display the value or the name of the clicked ListItem in a TextView like a title and I want to have another TextView and put in a text about the clicked ListViewItem.

Right now I got this:

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    TextView titleView = (TextView) findViewById(R.id.textView);
    TextView descriptionView = (TextView) findViewById(R.id.textView2);

} 

I hope you can help me! Thank you!!

2 Answers2

0

PutExtra in firstActivity and getExtra in the secondActivity is the solution. check : How to use putExtra() and getExtra() for string data

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152
  • your're welcome. In your case your just wanted to pass a String. Imagine that you want to pass an object. [Parcelable is designed for that purpose](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents). – Ali Jun 23 '14 at 14:08
  • Thank you! Do you know how to receive the position? –  Jun 23 '14 at 14:13
  • in MainActivity : `intent.putExtra("position", pos);` and in SecondActivity : `int pos=getIntent().getExtras().getInt("position");` – Ali Jun 23 '14 at 14:15
0
In your main activity use this : 

 Intent intent = new Intent(MainActivity.this, SecondActivity.class);
 intent.putExtra("position", itemPosition);
 intent.putExtra("name", itemName);
 startActivity(in);

and in SecondActivity.java get these two values like this :

 String itemNm=getIntent().getStringExtra("name");
 int pos=getIntent().getExtras().getInt("position");

hope this will help you.
Imran Betara
  • 649
  • 3
  • 8