从列表视图检索消息并打印到编辑文本

发布于 2024-12-26 04:29:12 字数 1102 浏览 3 评论 0原文

我怎样才能做到这一点。我尝试了很多方法。 但我不能。 我所做的唯一工作就是用消息填充列表。但当我触摸它时,它没有任何作用。我怎样才能使列表可点击并获取消息?

这是我的代码和列表。它会读取收件箱,

    ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);


    if(fetchInbox()!=null)
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fetchInbox());
        lViewSMS.setAdapter(adapter);
     } 
}

public ArrayList<String> fetchInbox() {
    ArrayList<String> sms = new ArrayList<String>();

    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null); 

    cursor.moveToFirst();
    while  (cursor.moveToNext()) 
    {
           String address = cursor.getString(1);
           String body = cursor.getString(3);

           System.out.println("======> Mobile number => "+address);
           System.out.println("=====> SMS Text => "+body);

           sms.add(address+"\n"+body);

    }

    return sms;


}

请帮助我在我的代码中添加什么内容。

how can i do that. i try many ways.
but i cant .
the only i did and work is to fill the list with messages. but when i touch that it doesnt do anything. how can i make the list clickable and get the messages ?

here is my code with the list . it reads the inbox

    ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);


    if(fetchInbox()!=null)
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fetchInbox());
        lViewSMS.setAdapter(adapter);
     } 
}

public ArrayList<String> fetchInbox() {
    ArrayList<String> sms = new ArrayList<String>();

    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null); 

    cursor.moveToFirst();
    while  (cursor.moveToNext()) 
    {
           String address = cursor.getString(1);
           String body = cursor.getString(3);

           System.out.println("======> Mobile number => "+address);
           System.out.println("=====> SMS Text => "+body);

           sms.add(address+"\n"+body);

    }

    return sms;


}

please help of what to add on my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2025-01-02 04:29:12

您需要为lViewSMS编写OnItemClicklistener。

lViewSMS .setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });

这是android链接 ListView

You need to write OnItemClicklistener for lViewSMS.

lViewSMS .setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });

Here is android link ListView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文