短信收件箱 - ListView

时间:2011-02-05 08:43:17

标签: android

您好我正在尝试创建应用程序女巫在ListView中向我显示短信但我不知道我如何通过数据填充listview。我试过这样做但它不起作用。可以检查我的源代码并给我一些解决方案?感谢

源代码:

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.widget.ArrayAdapter;
import android.widget.ListView;


public class Mynewtwo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list);
        List<String> msgList = getSMS();


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.main, msgList); 
        list.setAdapter(adapter);
    }

    public List<String> getSMS(){
         List<String> sms = new ArrayList<String>();
            Uri uriSMSURI = Uri.parse("content://sms/inbox");
            Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

            while (cur.moveToNext()) {
                   String address = cur.getString(cur.getColumnIndex("address"));
                   String body = cur.getString(cur.getColumnIndexOrThrow("body"));
                  sms.add("Number: " + address + " .Message: " + body);  

              }
            return sms;

        }

}

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>

1 个答案:

答案 0 :(得分:1)

它永远不会向ListView填充任何数据,因为您所犯的错误就在这里:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.main, msgList);

在此,您要告知msgList对象的项目将显示在R.layout.main文件中,并且您的文件不包含任何TextView或其他布局对象。

所以请这样使用:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1,msgList);