我对android很陌生,对java了解不多,但由于学校项目需要。当在列表视图上单击特定名称或号码时,我需要提取电话号码并发送短信。我被困住了。提前致谢。现在,每当我单击列表中的任何项目时,我都只能向此号码“123456”发送短信。这是我所有的代码。
联系人
package com.Elson.ProjectVersion;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
public class Contacts implements Comparable<Contacts> {
private long id;
private String name;
private int Phone;
private int Email;
private Date date;
private double runningAverage;
public Contacts(String name, int Phone, Date date) {
this.name = name;
this.Phone = Phone;
this.date = null;
}
public Contacts(long id, String name,int Phone) {
this.id=id;
this.Phone=Phone;
this.name= (name);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getPhone() {
return Phone;
}
public void setPhone(int Phone) {
this.Phone = Phone;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public Date getDate() {
return null;
}
public long getDateEpoch(){
return date.getTime()/1000;
}
public void setDateEpoch(long seconds){
date= new Date (seconds*1000);
}
public void setDate(Date date) {
this.date = date;
}
public void setRunningAverage(double runningAverage) {
this.runningAverage = runningAverage;
}
public boolean equals(Object that){
Contacts bs = (Contacts) that;
return this.date.equals(bs.date);
}
@Override
public String toString() {
String result;
if(date != null) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
result = df.format(date) + "" + name + "" + Phone ;
}
else {
result = name + "" + Phone ;
}
return result;
}
@Override
public int compareTo(Contacts another) {
// TODO Auto-generated method stub
return 0;
}
}
发送活动
package com.Elson.ProjectVersion;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class SendActivity extends ListActivity {
Bundle savedInstanceState;
private ArrayList<Contacts> allContacts;
private Button AddContactsButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
setContentView(R.layout.send_layout);
// get data from the App
ContactsActivityApplication app = (ContactsActivityApplication) getApplication();
allContacts = app.getAllContacts();
// View --- Adapter ------ Data
setListAdapter(new ArrayAdapter<Contacts>(this, R.layout.history_row, R.id.textStart, allContacts));
final ListView listView = this.getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Contacts selectedContax = (Contacts) listView.getSelectedItem();
//sendSMS(String.valueOf("123456"), "Hi");
}
}
);
}
private void sendSMS(String phoneNumber, String message) {
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SendActivity.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
public void AddContactstoListClickHandler (View v){
Intent intent = new Intent(this, EnterContactsActivity.class);
startActivity(intent);
}
}