Reputation: 903
I am new to android programming.I am developing an app in which when user clicks on any ListView item it goes to Google maps app and display pin for that address on the map. But now I want to add two more strings to ListView, phone number and email address. When user clicks on address string it should call google map. When user clicks on phone number it should call to that number, and when user clicks on email id it should open email facility.
Following is my Display Activity:
public class DisplayActivity extends Activity implements OnItemClickListener{
ListView listView;
private String tag_name;
public List<NameAddress> nameAddressList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
Intent intent = getIntent();
if(intent!= null)
{
tag_name = intent.getStringExtra("DashItemName");
setTitle("List of " +tag_name+ " addresses");
}
nameAddressList = null;
try {
XMLDOMParserHandler parser = new XMLDOMParserHandler(tag_name);
nameAddressList = parser.parseXML(getAssets().open("data.xml"));
ArrayAdapter<NameAddress> adapter =
new ArrayAdapter<NameAddress>(this,R.layout.list_item, nameAddressList);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// Build the intent
String address = nameAddressList.get(position).toString();
address = "geo:0,0?q=" + address;
String query = URLEncoder.encode(address, "utf-8");
Uri location = Uri.parse(query);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location;
mapIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
startActivity(mapIntent);
}
else
{
Toast.makeText(this, "Please install google maps app", Toast.LENGTH_LONG).show();
}
}
}
Please suggest me a way to solve this problem.
Upvotes: 1
Views: 789
Reputation: 1811
You need to implement custom adapter for your list view, there you can add click listeners to particular item in each row of list view, and onItemClickListener for complete rows will also work same, just be sure to add android:descendantFocusability="afterDescendants"
in the row item layout of your listview.
To learn how to implement optimized List Adapter you can check this blog, written by me
Upvotes: 0
Reputation: 3402
public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.textView1);
holder.textAddress = (TextView) row.findViewById(R.id.textView2);
holder.textLocation = (TextView) row.findViewById(R.id.textView3);
holder.btnEdit = (Button) row.findViewById(R.id.button1);
holder.btnDelete = (Button) row.findViewById(R.id.button2);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.textName.setText(user.getName());
holder.textAddress.setText(user.getAddress());
holder.textLocation.setText(user.getLocation());
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Edit Button Clicked", "**********");
Toast.makeText(context, "Edit button Clicked",
Toast.LENGTH_LONG).show();
}
});
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Delete Button Clicked", "**********");
Toast.makeText(context, "Delete button Clicked",
Toast.LENGTH_LONG).show();
}
});
return row;
}
static class UserHolder {
TextView textName;
TextView textAddress;
TextView textLocation;
Button btnEdit;
Button btnDelete;
}
}
Upvotes: 2
Reputation: 47807
Code for Send Email
:
Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "[email protected]" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "sub");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Code for Phone Number Calling
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + number));
ctx.startActivity(callIntent);
Used this code for particular Text click event in your ListView Item
. You need make Custom Adapter
for this.
Upvotes: 1