0

I am very new to Android development (though not to development in general). I am trying to create an app which:

Opens SMS inbox when a user taps a button and display them in listview (done)

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

When the user taps one message from the list, I want to get the body of the txt message (failed to do)

I am using this code in activity_main but it causes my app to close once I tap an item from the list

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: thread exiting with uncaught exception (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 cannot be cast to java.lang.String at com.example.taymour.moneywatch.MainActivity$1.onItemClick(MainActivity.java:39) at android.widget.AdapterView.performItemClick(AdapterView.java:292) at android.widget.AbsListView.performItemClick(AbsListView.java:1058) at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) at android.widget.AbsListView$1.run(AbsListView.java:3168) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at 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: reacting to signal 3 02-17 20:42:17.684 32383-32389/com.example.taymour.moneywatch I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt' 02-17 20:42:21.158 32383-32383/? I/Process: Sending signal. PID: 32383 SIG: 9

Bondo2
  • 1
  • 1

1 Answers1

1
        String item = ((TextView)view).getText().toString();

to

    TextView textView = (TextView) view.findViewById(R.id.lblMsg);

String item= textview.getText().toString();

oops: you have CursorAdapter. inside onItemClick.

  c.moveToPosition(position);

         String body = c.getString(c.getColumnIndexOrThrow("body"));

or

Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));

or try this How to get the item id in an onItemClick handler

Community
  • 1
  • 1
Sree Reddy Menon
  • 1,301
  • 13
  • 23
  • still crashes `code` lvMsg = (ListView) findViewById(R.id.lvMsg); lvMsg.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { TextView textView = (TextView) view.findViewById(R.id.lblMsg); String item= textView.getText().toString(); Toast.makeText(getBaseContext(),item , Toast.LENGTH_LONG).show(); } }); – Bondo2 Feb 17 '16 at 17:54