0

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

}
4

2 回答 2

0

如果您想使用自己创建的类 MyContact 而不是 Contacts,

public class MyContact{
    String Phone;
    String Name;
    //...

    public MyContact (String Phone, String Name) //or more {
        this.Phone = Phone;
        this.Name = Name;
        //...
    }
}

String myText; //the short message

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyContact selected = allContacts.get(position); //allContacts[position];
        Intent intentSms = new Intent(Intent.ACTION_VIEW);
        intentSms.setData(Uri.fromParts("sms", selected.Phone, null));
        intentSms.putExtra("sms_body", myText);
        startActivity(intentSms);
    }
于 2013-08-03T15:57:20.930 回答
0

好吧,您的问题并不清楚,并且很费时间阅读所有代码。我从您的问题中了解到的是,您正在 listView 中显示数字列表,并且当用户点击 listView 项目时,您想向该号码发送消息。所以试试下面的代码。

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

String phoneNumber = (String) l.getItemAtPosition(position);

// send message to the number stored in phoneNumber
}

使用您的代码,您可以这样做:

  listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String phoneNumber = (String) listView.getItemAtPosition(position);

        // Don't store the item returned by this method in Contact object.
    }
}

);
于 2013-08-03T15:55:50.370 回答