Onclickitem并在Cursor内部启动新意图

时间:2015-12-10 03:23:09

标签: java android

我是android开发的新手。我创建了一个SMS收件箱应用程序。 我设法得到手机的短信收件箱。现在我想设置一个onclick方法,用新的活动打开一个包含电话号码和消息的特定消息。

以下是我的收件箱活动的代码。我不明白,放置我的onclicklistitem方法的地方。

public class MessageInboxActivity extends ActionBarActivity implements OnItemClickListener {

private static MessageInboxActivity inst;
ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter arrayAdapter;

public static MessageInboxActivity instance() {
    return inst;
}

@Override
public void onStart() {
    super.onStart();
    inst = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_inbox);
    smsListView = (ListView) findViewById(R.id.SMSList);
    arrayAdapter = new ArrayAdapter<String>(this, R.layout.my_adapter_item, R.id.product_name, smsMessagesList);
    smsListView.setAdapter(arrayAdapter);
    smsListView.setOnItemClickListener(this);

    refreshSmsInbox();

}

public void refreshSmsInbox() {
    ContentResolver contentResolver = getContentResolver();
    Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
    int indexBody = smsInboxCursor.getColumnIndex("body");
    int indexAddress = smsInboxCursor.getColumnIndex("address");
    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    arrayAdapter.clear();
    do {
        String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
                "\n" + smsInboxCursor.getString(indexBody) + "\n";
        arrayAdapter.add(str);
    } while (smsInboxCursor.moveToNext());


}

public void updateList(final String smsMessage) {
    arrayAdapter.insert(smsMessage, 0);
    arrayAdapter.notifyDataSetChanged();
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    try {
        String[] smsMessages = smsMessagesList.get(pos).split("\n");
        String address = smsMessages[0];
        String smsMessage = "";
        for (int i = 1; i < smsMessages.length; ++i) {
            smsMessage += smsMessages[i];
        }

        String smsMessageStr = address + "\n";
        smsMessageStr += smsMessage;
        Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

有人可以帮助我使用电话号码和消息开始新的活动。

1 个答案:

答案 0 :(得分:0)

我将此置于评论中,但我无法发表评论,您能澄清一下您的问题吗?您想要链接到哪些活动以及您想要实现什么?看起来你只需要添加到你的onItemClick()

    String smsMessageStr = address + "\n";
    smsMessageStr += smsMessage;
    Intent in = new Intent(getApplicationContext,/*whatever activity you want to open*/);
    in.putStringExtra(/*some static keystring*/,smsMessage);
    startActivity(in);

但如果不了解更多

,很难说肯定