Android - 根据所点击的内容,使活动显示不同的内容

时间:2014-06-06 21:14:14

标签: java android android-activity

我有一个列表视图,由一个类的arraylist填充。

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"));
}

这是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"));
  }
}

有什么方法可以从clickedExample获取信息并在ExampleActivity中设置textViews和imageView,具体取决于点击了哪一个?

4 个答案:

答案 0 :(得分:2)

您可以使用Intent和extras。 在调用startActivity之前,在Intent上调用带有任何原始类型或String的putExtra,在ExampleActivity中使用getIntent和getExtra来检索您添加的数据。

答案 1 :(得分:1)

调用Activity时,请使用put方法。检索时,请使用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);
     }
   }

答案 2 :(得分:1)

有几种方法可以做到这一点。一种是使您的Example对象实现Parcelable,然后在intent中设置它。你的第二个选择是创建一个intent,只需像下面那样单独设置资源和每个字符串:

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);
}

然后在你的活动......

@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);


}

答案 3 :(得分:1)

使用putExtra然后getExtra

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

EXAMPLEACTIVITY

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