content:// sms / inboxから「body」と「address」を呼び出して、ListViewに追加しようとしています。
「address」は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>
My Menu.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();
}
}
}