簡體   English   中英

OnItemClickListener / SimpleDateFormat

[英]OnItemClickListener / SimpleDateFormat

我正在開發一個消息傳遞應用程序,並且使對話列表顯示得很好,但是我的listview onitemclicklistner遇到了麻煩。 我希望它檢索textviewid = lblID ),將其轉換為字符串,然后顯示對話列表(以該字符串作為id),並將其顯示在我的listview

  1. 我這樣做正確嗎?
  2. 解決simplecursoradapter內的onitemclicklistener不會讓我使用“ this”作為上下文,應該怎么用?
  3. 我想使用SimpleDateFormat ,如何在光標和適配器之間執行此操作?
  4. 解決了我現在遇到錯誤,沒有人沒有辦法解決此問題嗎?:

    10-10 07:45:54.926 24231-24231 /? E / Android運行時:致命異常:主要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日期* 1 AS normalized_date,NULL AS * FROM sms WHERE(thread_id = 37 AND(類型!= 3))UNION SELECT DISTINCT日期* 1000 AS歸一化日期,NULL AS *來自pdu左連接Joining_msgs在pdu._id =待處理的msgs.msg_id位置(thread_id = 37 AND msg_box!= 3 AND(msg_box!= 3 AND (m_type = 128 OR m_type = 132 OR m_type = 130)))ORDER BY normalized_date desc)ORDER BY normalized_date desc

這是我的代碼:

@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希望將“上下文”作為第一個參數。 如果在OnItemClick方法中調用“ this”,則上下文為OnItemClick。

如果您在一個片段中,請使用getActivity() ,或在您的onCreate()方法中執行以下操作:

Context mContext = getActivity();

並使用mContext作為new SimpleCursorAdapter(mContext, .....);

在一個活動中,您可以像這樣在onCreate中分配變量mContext:

Context mContext = this;

您還可以嘗試其他方法,例如getApplicationContext()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM