简体   繁体   中英

Android - Making an activity display different things depending on what is clicked

I have a listview that is populated by an arraylist of classes.

public void populateThingsList(){
        listExample.add(new Example("Example name", 1, "Example detail 1", "Example detail 2", R.drawable.1_icon, "Example detail 3"));
        listExample.add(new Example("Example name1", 2, "Example detail 1", "Example detail 2", R.drawable.2_icon, "Example detail 3"));
        listExample.add(new Example("Example name2", 3, "Example detail 1", "Example detail 2", R.drawable.3_icon, "Example detail 3"));
        listExample.add(new Example("Example name3", 4, "Example detail 1", "Example detail 2", R.drawable.4_icon, "Example detail 3"));
        listExample.add(new Example("Example name4", 5, "Example detail 1", "Example detail 2", R.drawable.5_icon, "Example detail 3"));
        listExample.add(new Example("Example name5", 6, "Example detail 1", "Example detail 2", R.drawable.6_icon, "Example detail 3"));
        listExample.add(new Example("Example name6", 7, "Example detail 1", "Example detail 2", R.drawable.7_icon, "Example detail 3"));
        listExample.add(new Example("Example name7", 8, "Example detail 1", "Example detail 2", R.drawable.8_icon, "Example detail 3"));
        listExample.add(new Example("Example name8", 9, "Example detail 1", "Example detail 2", R.drawable.9_icon, "Example detail 3"));
        listExample.add(new Example("Example name9", 10, "Example detail 1", "Example detail 2", R.drawable.10_icon, "Example detail 3"));
}

This is the code for the clicklistener.

ListView list = (ListView) findViewById(R.id.exampleListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     Example clickedexample = listExample.get(position);
     startActivity(new Intent("com.example.app.EXAMPLEACTIVITY"));
  }
}

Is there any way I can get the information from clickedExample and set the textViews and imageView in ExampleActivity depending on which one was clicked?

You can use an Intent and extras. Call putExtra with any primitive type or String on the Intent before calling startActivity and in ExampleActivity use getIntent and getExtra to retrieve the data you added.

When calling Activity , use put methods. When retrieving, use getExtras() .

    ListView list = (ListView) findViewById(R.id.exampleListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Example clickedexample = listExample.get(position);
        String text = clickedexample.getDetails1(); // Assuming you have this in your Example code
        Integer drawableInt = clickedexample.getIcon(); // Same assumption
        Intent intent = new Intent("com.example.app.EXAMPLEACTIVITY");
        intent.putString("text",text);
        intent.putInt("icon",drawableInt);
        startActivity(intent);
     }
   }

There's a few ways you could do this. One is to make your Example object implement Parcelable and then set that in an intent. Your second option is to create an intent and just set the resource and each string individually like so:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Example clickedexample = listExample.get(position);
    Intent intent = new Intent("com.example.app.EXAMPLEACTIVITY");
    intent.putExtra("detail_1", clickedexample.getDetail1());
    intent.putExtra("detail_2", clickedexample.getDetail2());
    intent.putExtra("detail_3", clickedexample.getDetail3());
    intent.putExtra("detail_icon", clickedexample.getIcon());
    startActivity(intent);
}

then in your activity...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String e1 = getIntent().getStringExtra("detail_1");
    String e2 = getIntent().getStringExtra("detail_2");
    String e3 = getIntent().getStringExtra("detail_3");
    int icon = getIntent().getIntExtra("detail_icon", 0);
    Example e = new Example(e1, e2, icon, e3);


}

Use putExtra and then getExtra :

Intent intent = new Intent("com.example.app.EXAMPLEACTIVITY"); 
intent.putExtra("Example_Name", clickedexample.name);
intent.putExtra("Example_detail", clickedexample.detail);
startActivity(intent);

In your EXAMPLEACTIVITY :

void onCreate(Bundle savedInstanceState) {
     Intent intent = getIntent();
     String name   = intent.getStringExtra("Example_Name");
     String detail = intent.getStringExtra("Example_detail");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM