Search code examples
javaandroidsimpledateformatsimplecursoradapter

OnItemClickListener / SimpleDateFormat


I am working on a messaging app, and have made the conversation list show up fine, but am having trouble with my listview onitemclicklistner. I would like it to retrieve a textview (id=lblID), convert it to a string, then show the conversations list (with that string as the id) and display it in my listview.

  1. Am I doing this correctly?
  2. Solved the simplecursoradapter inside the onitemclicklistener wont let me use "this" as the context, what should I use instead?
  3. I would like to use SimpleDateFormat, how would I do this between the cursor and adapter?
  4. Solved I am now getting an error, does anyone no how to fix this?:

    10-10 07:45:54.926 24231-24231/? E/AndroidRuntime: FATAL EXCEPTION: main 10-10 07:45:54.926 24231-24231/? E/AndroidRuntime: Process: com.example.wq.myapp, PID: 24231 10-10 07:45:54.926 24231-24231/? E/AndroidRuntime: android.database.sqlite.SQLiteException: near "*": syntax error (code 1): , while compiling: 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 desc

Here is my code:

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

Any help or extra knowledge would be greatly appreciated. :)


Solution

  • For 2: SimpleCursorAdapter wants 'Context' as first Parameter. If you call 'this' in your OnItemClick method, your context is your OnItemClick.

    If you are in a fragment, use getActivity(), or do this in your onCreate() method:

    Context mContext = getActivity();
    

    and use mContext as new SimpleCursorAdapter(mContext, .....);

    In an activity, you can assign the variable mContext in onCreate like this:

    Context mContext = this;
    

    There are other methods like getApplicationContext() which you can try.