0

I am a beginner programmer so please bear with me. I am trying to create an app where the item in the list view affects what will be displayed in the next activity. So far, I have the list activity:

public class Primary extends ListActivity{
private static final String[] items = {"Item1", "Item2", "Item3", "item4", "Item5"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

    TextView heading =(TextView)findViewById(R.id.listViewHeading);
    heading.setText("Primary");
}

public void onListItemClick(ListView parent, View v, int position, long id){

}

and for the second activity, I have this:

public class ImageActivity extends Activity{
TextView heading;
ImageView image;
TextView text;
    public static final String[] headings={"heading 1", "heading 2", "heading 3", "heading 4", "heading 5",};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_activity);

    heading = (TextView)findViewById(R.id.adHeading);
    image = (ImageView)findViewById(R.id.adImage);
    text =(TextView)findViewById(R.id.adText);

    addInfo();
}

private void addInfo() {
    heading.setText(headings[x]);
    image.setImageResource(images[x]);
    text.setText(text[x]);

}

How can i make it so that the heading, image, and text change based on what item in the list view was selected?

drew
  • 131
  • 2
  • 6

4 Answers4

2

In the listview Activity.

Intent i = new Intent(this, ImageActivity.class);
    i.putExtra("data", data);
    startActivity(i);

The next Acitivty onCreate() method.

final String data = getIntent().getStringExtra("data");
Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
0

Use the "extras" feature that are part of an Intent.

When you call start ImageActivity from Primary, you can use a 'extras' to pass information between the two.

See this link for details.

I'll give you a basic example here. When the list item is clicked, put the data that you want ImageActivity to have into the intent using "putExtra".

Intent intent = new Intent(getBaseContext(), ImageActivity.class);
String data = "somedata";
intent.putExtra("DATA", data);
startActivity(intent)

Then, in ImageActivity onCreate, retrieve the data like this:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
String data= extras.getString("DATA"); // matches the tag used in putExtra
}

Once you have retrieved the data, set the necessary views.

Community
  • 1
  • 1
Joel Skrepnek
  • 1,651
  • 1
  • 13
  • 21
0

I think u want to set the heading, image and text in second activity, related to first activity's selected index in list.

just do 1 thing, put following code in 1st activity

    public void onListItemClick(ListView parent, View v, int position, long id)
    {
        Intent intent = new Intent(this.getApplicationContext(), ImageActivity.class);
        intent.putExtra("pos", position);
        startActivity(intent);
    }

so, now u r passing the position of item selected in list.

now, put following code in next activity

    private void addInfo()
    {
        Bundle ext = getIntent().getExtras();
        if(ext != null)
        {
            int pos= ext.getInteger("pos");
                       //  ext.getInt("pos");

            heading.setText(headings[pos]);

            //  hey, frend, you don't have any array for selecting image-name and text
            //  image.setImageResource(images[x]);
            //  text.setText(text[x]);
        }
    }
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
0

use below code

public void onListItemClick(ListView parent, View v, int position, long id)
        {
            Intent intent = new Intent(Primary.this, ImageActivity.class);
            intent.putExtra("selected value", item[position]);
            startActivity(intent);
        }

in ImageActivity class:in oncreate (or you can put item variable as global)

String item = getIntent().getStringExtra("Selected value");
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177