2011-02-05 66 views
1

你好,我想創建應用女巫顯示我在ListView短信,但我不知道我怎麼可以填充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> 
+0

你爲什麼問同樣的問題3倍? http://stackoverflow.com/questions/4905736/sms-inbox-textview和http://stackoverflow.com/questions/4897004/sms-inbox-listview – 2011-02-05 08:51:18

+0

這是另一個源代碼,他們在說我,我必須做出新的話題.. – Adam 2011-02-05 08:57:14

回答

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);