我正在使用两个活动(ListActivity和DatabaseBaseActivity)并将这两个活动指向单个活动(DetailActivity)。我在两个活动上使用intent和putextra()将数组数据发送到DetailActivity。问题出在这里,我不知道如何区分单个活动中的不同意图。这是我的ListActivity代码
我认为问题主要在于否则在DetailActivity中声明(我得到nullpointerexception)并且If语句正常工作
public class ListActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
final ArrayList<AppName> appNames = new ArrayList<AppName>();
appNames.add(new AppName("app1","description1"));
appNames.add(new AppName("app2","description2"));
appNames.add(new AppName("app3","description3"));
appNames.add(new AppName("app4","description4"));
appNames.add(new AppName("app5","description5"));
appNames.add(new AppName("app6","description6"));
appNames.add(new AppName("app7","description7"));
appNames.add(new AppName("app8","description8"));
appNames.add(new AppName("app9","description9"));
appNames.add(new AppName("app10","description10"));
AppAdapter adapter = new AppAdapter(this , appNames);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String[] appdescription = getResources().getStringArray(R.array.appdescriptionname);
final String appdescipl = appdescription[position];
Intent intent1 = new Intent(getApplicationContext(), DetailActivity.class);
intent1.putExtra("des","mydes");
intent1.putExtra("appdescipl" , appdescipl);
startActivity(intent1);
}
}
);
}
这是我的DatabaseBaseActivity代码
public class DatabaseBaseActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
final ArrayList<AppName> appNames = new ArrayList<AppName>();
appNames.add(new AppName("appdata1","description1"));
appNames.add(new AppName("appdata2","description2"));
appNames.add(new AppName("appdata3","description3"));
appNames.add(new AppName("appdata4","description4"));
appNames.add(new AppName("appdata5","description5"));
appNames.add(new AppName("appdata6","description6"));
appNames.add(new AppName("appdata7","description7"));
appNames.add(new AppName("appdata8","description8"));
appNames.add(new AppName("appdata9","description9"));
appNames.add(new AppName("appdata10","description10"));
AppAdapter adapter = new AppAdapter(this , appNames);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String[] appdatadescription = getResources().getStringArray(R.array.appdatabasedescriptionname);
final String appdatadescipl = appdatadescription[position];
Intent intent2 = new Intent(getApplicationContext(), DetailActivity.class);
intent2.putExtra("data" , "mydata");
intent2.putExtra("appdatadescipl" , appdatadescipl);
startActivity(intent2);
}
}
);
}
}
这是DetailActivity
public class DetailActivity extends Activity{
String description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
TextView descriptionofapp = (TextView)findViewById(R.id.descriptionofapp);
Bundle extras = getIntent().getExtras();
if ( extras.getString("des").equals("mydes") && extras!=null){
description = extras.getString("appdescipl");
descriptionofapp.setText(description);
}
else if (extras.getString("data").equals("mydata") && extras!=null){
description = extras.getString("appdatadescipl");
descriptionofapp.setText(description);
}
}
}
提前致谢
答案 0 :(得分:0)
你不需要区分意图。 getIntent()只返回启动活动的意图。试试这个 -
String description =null;
description = getIntent().getStringExtra("appdescipl");
if(description==null) description = getIntent().getStringExtra("appdatadescipl");
descriptionofapp.setText(description);
答案 1 :(得分:0)
试试这个,
Bundle extras = getIntent().getExtras();
if(extras.containsKey("des")){
description = extras.getString("appdescipl");
descriptionofapp.setText(description);
}
else if (extras.containsKey("data")){
description = extras.getString("appdatadescipl");
descriptionofapp.setText(description);
}
//...... conditions / check with unique key to differentiate from calling activity