2015-10-09 46 views
0

我正在开发一个消息应用程序,并已使对话列表显示正常,但我的listview onitemclicklistner遇到问题。我想要检索textviewid = lblID),将其转换为字符串,然后显示对话列表(将该字符串作为id)并将其显示在我的listview中。OnItemClickListener/SimpleDateFormat

  1. 我这样做是否正确?
  2. 已解决simplecursoradapter里面的onitemclicklistener不会让我用“this”作为上下文,应该用什么来代替?
  3. 我想用SimpleDateFormat,我该如何做到这一点之间的光标和适配器?
  4. 解决现在我得到一个错误,没有人没有如何解决这个问题?:

    7月10日至10日: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 OR m_type = 132 OR m_type = 130)))ORDER BY normalized_date DESC)ORDER BY normalized_date递减

这里是我的代码:

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

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

回答

0

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

如果你是在一个片段中,使用getActivity(),或在您的onCreate做到这一点()方法:

Context mContext = getActivity(); 

,并使用mContext为new SimpleCursorAdapter(mContext, .....);

一个活动,您可以指定变量mContext在onCreate是这样的:

Context mContext = this; 

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

+0

真棒谢谢堆:)我明白,“这”是返回OnItemClick,我只是不知道如何检索应用程序上下文:) –