我正在尝试从 content://sms/inbox 调用“正文”和“地址”并将它们添加到 ListView。
“地址”指向 ListView 中的 Item1,“body”指向同一 ListView 中的 SubItem。
为此,我正在实现一个游标以将这些项目添加到 ListView。
当我现在运行代码时,我没有收到任何错误或崩溃,但我的 ListView 中根本没有添加任何内容。我拥有清单中所需的所有权限。
不知道我哪里出错了:(但提前谢谢!
我的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header__root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/burntorange"
android:orientation="horizontal" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:background="@color/burntorange"
android:padding="10dp"
android:text="Messages"
android:textSize="20dp" />
<Button
android:id="@+id/bNewMessage"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="@color/gray"
android:gravity="center"
android:onClick="handleClick"
android:padding="4dp"
android:text="New Thread"
android:textColor="@color/white"
android:textSize="15dp" />
</LinearLayout>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
我的菜单.java:
public class Menu extends ListActivity {
ListView itemList;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle biscuits) {
super.onCreate(biscuits);
setContentView(R.layout.header);
itemList = (ListView) findViewById(android.R.id.list);
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uri, null, null, null, null);
startManagingCursor(c);
String[] number = new String [c.getCount()];
String[] body = new String [c.getCount()];
if(c.moveToFirst()){
for (int i=0; i<c.getCount();i++) {
body[i]=c.getString(c.getColumnIndexOrThrow("body")).toString();
number[i] =c.getString(c.getColumnIndexOrThrow("address")).toString();
c.moveToNext();
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("contactNumber", number[i]);
datum.put("messagePreview", body[i]);
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_1,
new String[] {"contactNumber", "messagePreview"},
new int [] {android.R.id.text1,
android.R.id.text2});
itemList.setAdapter(adapter);
}
}
}
public void handleClick(View v) {
Intent intent = new Intent();
intent.setClass(this, TextActivity.class);
startActivity(intent);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String cheese = (String) l.getItemAtPosition(position);
Toast.makeText(this, cheese + " Selected", Toast.LENGTH_SHORT).show();
try {
Class ourClass = Class.forName("com.example.mytexts." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}