0

1.How do i setup a onItemClickListener so that if i go in my SMS inbox and click a contact it will open a new activity showing a conversation view between me and that contact?

2.And my second question is where is my code should i put the onItemClickListener?

package techyapp.greg.spanglish;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Inbox extends Activity implements OnItemClickListener {

    ListView lViewSMS;
    String thread_id, body;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox);

        ListView list = (ListView) findViewById(R.id.lvlistViewSMS);
        List<String> msgList = getSMS();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, msgList);
        list.setAdapter(adapter);

    }

    public List<String> getSMS() {
        List<String> sms = new ArrayList<String>();
        Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,
                "date");

        while (cur.moveToNext()) {
            String address = cur.getString(cur.getColumnIndex("address"));
            String body = cur.getString(cur.getColumnIndexOrThrow("body"));
            String person = cur.getString(cur.getColumnIndexOrThrow("person"));
            String thread_id = cur.getString(cur
                    .getColumnIndexOrThrow("thread_id"));
            sms.add(address + " " + person + "\n" + body);

        }
        return sms;

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent(getApplicationContext(),
                Conversation.class);
        startActivity(myIntent);
    }
}    

01-21 15:33:33.674: E/AndroidRuntime(4749): FATAL EXCEPTION: main

01-21 15:33:33.674: E/AndroidRuntime(4749): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{techyapp.greg.spanglish/techyapp.greg.spanglish.Conversation}: java.lang.NullPointerException

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.ActivityThread.access$600(ActivityThread.java:153)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.os.Handler.dispatchMessage(Handler.java:99)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.os.Looper.loop(Looper.java:137)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.ActivityThread.main(ActivityThread.java:5289)

01-21 15:33:33.674: E/AndroidRuntime(4749): at java.lang.reflect.Method.invokeNative(Native Method)

01-21 15:33:33.674: E/AndroidRuntime(4749): at java.lang.reflect.Method.invoke(Method.java:525)

01-21 15:33:33.674: E/AndroidRuntime(4749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)

01-21 15:33:33.674: E/AndroidRuntime(4749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)

01-21 15:33:33.674: E/AndroidRuntime(4749): at dalvik.system.NativeStart.main(Native Method)

01-21 15:33:33.674: E/AndroidRuntime(4749): Caused by: java.lang.NullPointerException

01-21 15:33:33.674: E/AndroidRuntime(4749): at techyapp.greg.spanglish.Conversation.(Conversation.java:23)

01-21 15:33:33.674: E/AndroidRuntime(4749): at java.lang.Class.newInstanceImpl(Native Method)

01-21 15:33:33.674: E/AndroidRuntime(4749): at java.lang.Class.newInstance(Class.java:1130)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)

01-21 15:33:33.674: E/AndroidRuntime(4749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)

01-21 15:33:33.674: E/AndroidRuntime(4749): ... 11 more

Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61

1 Answers1

0

Looks like you forgot to add the item click listener to the listview.

list.setOnItemClickListener(this);

You will also need to pass the relevent piece of data to the new activity.

You can do something like

    Intent myIntent = new Intent(getApplicationContext(),
            Conversation.class);
    myIntent.putExtra("msg", myMsgData);
    startActivity(myIntent);

Keep in mind myMsgData has to be one of a few supported formats to pass between activities. Check out this post How do I pass data between Activities in Android application?

Community
  • 1
  • 1
sgarman
  • 6,152
  • 5
  • 40
  • 44
  • I have looked at the post and tried a few of the solution your solution was a great help and i believed i inputted my data correctly. The error that i am running into is the thee receive class. When i click on my contact it crashes. – user3200079 Jan 18 '14 at 07:50
  • If there is a crash you need to post your logcat output of the crash. – sgarman Jan 20 '14 at 21:04