Search code examples
androidlistviewandroid-edittextinbox

Retrieve Messages from listview and print to edittext


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.


Solution

  • 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