1.如何设置onItemClickListener,以便如果我进入我的短信收件箱并点击联系人,它将打开一个新活动,显示我和该联系人之间的对话视图?
2.我的第二个问题是我的代码应该放在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):致命异常:主
01-21 15:33:33.674:E / AndroidRuntime(4749):java.lang.RuntimeException:无法实例化活动ComponentInfo {techyapp.greg.spanglish / techyapp.greg.spanglish.Conversation}:java.lang。的NullPointerException
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.app.ActivityThread.access $ 600(ActivityThread.java:153)
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1269)
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.os.Handler.dispatchMessage(Handler.java:99)
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.os.Looper.loop(Looper.java:137)
01-21 15:33:33.674:E / AndroidRuntime(4749):在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):引起: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):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-21 15:33:33.674:E / AndroidRuntime(4749):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
01-21 15:33:33.674:E / AndroidRuntime(4749):... 11更多
答案 0 :(得分:0)
您似乎忘了将项目点击监听器添加到列表视图中。
list.setOnItemClickListener(this);
您还需要将相关数据传递给新活动。
您可以执行类似
的操作 Intent myIntent = new Intent(getApplicationContext(),
Conversation.class);
myIntent.putExtra("msg", myMsgData);
startActivity(myIntent);
请记住,myMsgData必须是在活动之间传递的一些支持格式之一。看看这篇文章How do I pass data between Activities in Android application?