开发者

SMS INBOX - LISTVIEW

开发者 https://www.devze.com 2023-02-08 07:01 出处:网络
Hello i am trying to create SMS aplication witch shows me SMS Inbox in ListView.But if i open this aplication there is no action.I can see only black screen with name of aplicatio开发者_如何学运维n :(

Hello i am trying to create SMS aplication witch shows me SMS Inbox in ListView.But if i open this aplication there is no action.I can see only black screen with name of aplicatio开发者_如何学运维n :( .Can somebody check my source code and tell me where is the problem? Thank you.

There is source code:

package sms.five;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Smsfive extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState, ListView messages) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list);

        List<String> msgList = getSMS();

        for(int i = 0; i<msgList.size(); i++) {
            System.out.println(msgList.get(i));
       }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.main, msgList); 
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener()
       {
        @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
         SmsManager m = SmsManager.getDefault();
         String destinationNumber=(String) ((TextView)view).getText();              
         m.sendTextMessage(destinationNumber, null, destinationNumber, null, null);

       }          
         });
       }

    public List<String> getSMS() {      
        List<String> list = new ArrayList<String>();
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor c = null;
        try{
            c = getApplicationContext().getContentResolver().query(uri, null, null ,null,null); 
        }catch(Exception e){
            e.printStackTrace();
        }
        try{
            for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
                final String address = c.getString(c.getColumnIndex("address"));
                final String body = c.getString(c.getColumnIndexOrThrow("body"));
                list.add("Number: " + address + " .Message: " + body);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
     c.close(); 
     return list;
    }
}

XML File(main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>

AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="sms.five"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Smsfive"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>


<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
</manifest> 


I'm assuming your getSMS() is not returning anything. You can check this by adding some debugging code after this line:

List<String> msgList = getSMS();

Something like

Log.d("yourTag","number of items: ".msgList->size());

It seems that it doesn't return anything. You might start by debugging that particular function: are there any exceptions caught? add some logging to see that. Maybe you didn't add the correct line in your manifest

 <uses-permission android:name="android.permission.READ_SMS"/>

Or something else goes wrong in that function. Did you write it yourself, or was it a copy?


remove c.close() in your getSMS method.


the problem is line " ArrayAdapter adapter = new ArrayAdapter(this,R.layout.main, msgList); "

argument R.layout.main is false , it's layout item custom by you Or android built example like android.R.layout.simple_list_item_1

=> change R.layout.main - > android.R.layout.simple_list_item_1 . succsess 100 %

0

精彩评论

暂无评论...
验证码 换一张
取 消