ListView onItemClick by position
-
05-03-2021 - |
Pregunta
I'm trying to make it so when you click row 2 or 4 it runs a different method than the rest of the list items. The rest go to a website, but I want row 4 to open email, and row 2 to open the navigator. BUT WHEN I CLICK ON ROW 4, IT OPENS THE BROWSER (i.e. passes the Intent) THEN OPENS THE EMAIL AFTER I BACK OUT. why? --also, navigator force closes but i think its cause the emulator doesn't have navigator installed :/ ANY SUGGESTIONS? THANKS!
getListView().setOnItemClickListener(new OnItemClickListener() {final String[] links =getResources().getStringArray(com.BandCustomListView.R.array.ItemLinks); @Override public void onItemClick(AdapterView parent, View view, int position, long id){
String positions = links[position];
if (position == 4){
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"Mtarpey@worcester.edu"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
;
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
if (position == 2){
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=127+Southbridge+Street+Auburn+Massachusetts"));
startActivity(i);
}
if (position != (2 | 4)) {
Intent SendToWebPageView = new Intent(getApplicationContext(), WebPageView.class);
SendToWebPageView.setData(Uri.parse(positions));
startActivity(SendToWebPageView);
}
}
});
Solución
I think you may want to change this LOC
if (position != (2 | 4))
To be
if(position != 2 && position != 4)
Since 4 != 2 it will pass that check and kick off the webpage view after you do the check to perform the email intent.