This code simply has 2 buttons "Samsung" and "Apple" that start another activity listing a bunch of devices. How can I pass the respective company name (e.g Samsung) to the child activity? Do I have to make the array global to do it? I only know how to pass the position
I know how to pass it in one function but I have two in this case, one setting up the custom list adapter, and the other is just an onClick function. And this uses arrays rather than standalone strings so I'm not sure how to deal with that
MainActivity.java
package org.turntotech.navigatesample;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ListActivity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TurnToTech", "Project Name - NavigateSample");
String[] data = { "Samsung", "Apple" };
int[] icons = { R.drawable.samsung_logo, R.drawable.opo };
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data, icons));
/* setOnItemClickListener() Register a callback to be invoked when an item
* in this AdapterView has been clicked.
*/
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(), ChildActivity.class);
// Add extended data to the intent.
intent.putExtra("POSITION", position);
/*
* Launch a new activity. You will not receive any information about when
* the activity exits. This implementation overrides the base version,
* providing information about the activity performing the launch.
*/
startActivity(intent);
}
}
ChildActivity.java
package org.turntotech.navigatesample;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
public class ChildActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[][] data = {
{ "Galaxy Tab", "Galaxy Smart Phones", "Galaxy Gear" },
{ "iPhone", "iPad", "iPod" } };
int[][] icons = {
{ R.drawable.gala, R.drawable.duos, R.drawable.star },
{ R.drawable.a, R.drawable.b, R.drawable.c }, };
Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", 0);
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data[position],
icons[position]));
}
}