我对Android开发很新(虽然不是一般的开发)。我正在尝试创建一个应用程序:
当用户点击按钮并在列表视图(已完成)中显示时,打开SMS收件箱
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[] { "_id", "address", "body" };
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[] { "body", "address" }, new int[] {
R.id.lblMsg, R.id.lblNumber });
lvMsg.setAdapter(adapter);
当用户从列表中点击一条消息时,我想获取txt消息的正文(未能做到)
我在activity_main中使用此代码,但是当我点击列表中的某个项目时,它会导致我的应用关闭
lvMsg.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
logcat的
16.828 32383-32383/com.example.taymour.moneywatch D/AndroidRuntime: Shutting down VM
02-17 20:42:16.828 32383-32383 / com.example.taymour.moneywatch W / dalvikvm:threadid = 1:线程退出,未捕获异常(group = 0x409bf1f8) 02-17 20:42:16.828 32383-32383 / com.example.taymour.moneywatch E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ClassCastException:android.content.ContentResolver $ CursorWrapperInner无法强制转换为java.lang.String at com.example.taymour.moneywatch.MainActivity $ 1.onItemClick(MainActivity.java:39) 在android.widget.AdapterView.performItemClick(AdapterView.java:292) 在android.widget.AbsListView.performItemClick(AbsListView.java:1058) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2514) 在android.widget.AbsListView $ 1.run(AbsListView.java:3168) 在android.os.Handler.handleCallback(Handler.java:605) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) 02-17 20:42:17.473 32383-32389 / com.example.taymour.moneywatch I / dalvikvm:threadid = 3:对信号3作出反应 02-17 20:42:17.684 32383-32389 / com.example.taymour.moneywatch I / dalvikvm:将堆栈跟踪写入&#39; /data/anr/traces.txt' 02-17 20:42:21.158 32383-32383 /? I /处理:发送信号。 PID:32383 SIG:9
答案 0 :(得分:1)
String item = ((TextView)view).getText().toString();
到
TextView textView = (TextView) view.findViewById(R.id.lblMsg);
String item= textview.getText().toString();
oops:你有CursorAdapter。在onItemClick。
c.moveToPosition(position);
String body = c.getString(c.getColumnIndexOrThrow("body"));
或
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));