OnItemClickListener / SimpleDateFormat

OnItemClickListener / SimpleDateFormat

我正在开发一个消息传递应用程序,并使对话列表显示正常,但我的 listview onitemclicklistner 遇到了问题。我希望它检索 textview (id=lblID),将其转换为字符串,然后显示对话列表(以该字符串作为 id)并显示它在我的 listview.

  1. 我这样做正确吗?
  2. 已解决 onitemclicklistener 中的 simplecursoradapter 不让我使用 "this" 作为上下文,我应该用什么代替?
  3. 我想使用 SimpleDateFormat,我该如何在游标和适配器之间执行此操作?
  4. 已解决我现在遇到一个错误,有没有人知道如何解决这个问题?:

    10-10 07:45:54.926 24231-24231/? E/AndroidRuntime:致命异常:main 10-10 07:45:54.926 24231-24231/? E/AndroidRuntime:进程:com.example.wq.myapp,PID:24231 10-10 07:45:54.926 24231-24231/? E/AndroidRuntime: android.database.sqlite.SQLiteException: "*" 附近: 语法错误 (代码 1): , 编译时: SELECT * FROM (SELECT DISTINCT date * 1 AS normalized_date, NULL AS * FROM sms WHERE (thread_id = 37 AND (type != 3)) UNION SELECT DISTINCT date * 1000 AS normalized_date, NULL AS * FROM pdu LEFT JOIN pending_msgs ON pdu._id = pending_msgs.msg_id WHERE (thread_id = 37 AND msg_box != 3 AND (msg_box != 3 AND (m_type = 128 或 m_type = 132 或 m_type = 130))) 按 normalized_date desc) 按 normalized_date desc

  5. 排序

这是我的代码:

@Override
public void onClick(View v) {

    if (v == btnSMS) {
        // Create Inbox box URI
        Uri inboxURI = Uri.parse("content://mms-sms/conversations");
        // Get Content Resolver object, which will deal with Content Provider
        ContentResolver cr = getContentResolver();
        // Fetch Inbox SMS Message from Built-in Content Provider
        Cursor a = cr.query(inboxURI, new String[] {"*"}, null, null, "normalized_date desc");
        // Attach Cursor with adapter and display in listView
        adapter1 = new SimpleCursorAdapter(this, R.layout.row, a,
                new String[]{ "body", "date", "address","_id"},
                new int[]{ R.id.lblMsg, R.id.lblDate, R.id.lblNumber, R.id.lblID }, 0);
        lvMsg.setAdapter(adapter1);
        lvMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView TVConvID = (TextView)findViewById(R.id.lblID);
                String ConvID = TVConvID.getText().toString();
                Uri ConvURI = Uri.parse("content://mms-sms/conversations/"+ConvID);
                Cursor b = getContentResolver().query(ConvURI, new String[]{"*"}, null, null, "normalized_date desc");
                adapter2 = new SimpleCursorAdapter(getApplicationContext(), R.layout.convrow, b,
                        new String[]{ "body", "date", "address" },
                        new int[]{ R.id.msglblMsg, R.id.msglblDate, R.id.msglblNumber }, 0);
                lvMsg.setAdapter(adapter2);
            }
        });
    }

任何帮助或额外知识将不胜感激。 :)

对于 2: SimpleCursorAdapter 需要 'Context' 作为第一个参数。如果您在 OnItemClick 方法中调用 'this',您的上下文就是您的 OnItemClick。

如果您在片段中,请使用 getActivity(),或在您的 onCreate() 方法中执行此操作:

Context mContext = getActivity();

并将 mContext 用作 new SimpleCursorAdapter(mContext, .....);

在activity中,你可以像这样在onCreate中分配变量mContext:

Context mContext = this;

还有其他方法,例如 getApplicationContext() 您可以尝试。