query sms text body and show in listview - android

I want to filter the inbox view of my app depending on the string entered in search view.
So if user enters "How" the filter should get all the texts containing "How" in it
this is the code I have written :
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
AutoCompleteTextView textView = (AutoCompleteTextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
final Uri inboxURI = Uri.parse("content://sms/");
final String[] reqCols = new String[] {"_id","address","body" };
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
final SimpleCursorAdapter sadapter = new SimpleCursorAdapter(this, R.layout.list_row, c, new String[] {"address", "body" }, new int[] {R.id.phone_number,R.id.text_msg});
textView.setAdapter(sadapter);
sadapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return getContentResolver().query(inboxURI, reqCols, "body" + " LIKE '" + constraint + "%'", null, null);
}
});
but here if I type "How" the filter returns only texts which start with "How". I want all the texts which contain this string at any position in it.
Need help on how to create a query to get the result.

your query like constraint% ... change it like to like %constraint% (note the additional %)

Related

Displaying all SMS from one specific sender in my android app

My app is currently displaying all the SMS in the inbox with this code:
public void onClick(View v) {
if (v == btnInbox) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);
}
}
and I want to display only messages from one specific sender, what will i change to eliminate messages from the other sender?
apply selection arguments to filter the contents based on address field.
Actual code becomes:
String[] selectionArgs = { "12672631" }; //add the phone number here
Cursor c = cr.query(inboxURI, reqCols, "address=?", selectionArgs, null);

AutoCompleteTextView with SimpleCursorAdapter for Contacts not returning cursor actual value

I have a AutoCompleteTextView that uses a SimpleCursorAdapter to filter emails for an input field. I have it working, although there are some deprecated commands that I am not sure how to rework.
The only problem I am having is that when I select a value from the list provided, I am not getting the email address selected, but something like the following:
android.content.ContentResolver$CursorWrapperInner#13a08d9c
Here is the code I have:
final AutoCompleteTextView edt_Contact = (AutoCompleteTextView)findViewById(idTo);
ContentResolver cr = getContentResolver();
String[] projection={ContactsContract.CommonDataKinds.Email._ID,ContactsContract.CommonDataKinds.Email.ADDRESS};
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, null, null, null);
startManagingCursor(cursor);
String[] from = new String[] { ContactsContract.CommonDataKinds.Email.ADDRESS};
int[] to = new int[] { android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] {},
ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE '%" + constraint + "%'",
null, null);
}
});
edt_Contact.setAdapter(adapter);
Any suggestions on how to get the actual selected value to populate the AutoCompleteTextView when selected?
Also, as mentioned, the deprecated items are startManagingCursor and SimpleCursorAdapter.
Found the solution and I am posting it here for anyone else with a similar situation
I needed to add the following
adapter.setStringConversion(1);
I added it right before the last line in the example above. This changed the result from the
android.content.ContentResolver$CursorWrapperInner#13a08d9c
to the selected email address.
Just a heads-up too for anyone using the MultiAutoCompleteTextView to do the same thing, this code works for that as well... just change add Multi before the AutoCompleteTextView and add the .setTokenizer of your choice.

About Android ContentResolver Query Group By

I test sql and it works fine in Sqlite Spy:
select ifnull(_name, _number) as identifer, count(_id) as amount from call group by identifer
And I wanna use it in ContentConsolver but it can't work with "group by":
String[] projections = new String[] { "ifnull(name, number) as identifer", "count(_id) as amount" };
String group = "identifer";
//String selection = ") GROUP BY (" + group;
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, projections, null, null, null /*!group*/ );
What should I do?
#njzk2 did the trick by using HashSet: Group By in ContentResolver in Ice Cream Sandwich, But it didn't work with count() if you want sum.
I think the best solution is to make a copy of CallLog Database, then you can use rawQuery() or whatever you want (maybe it is a waste of performance).
private void refreshDbCache()
{
CallDbCache dbCache = new CallDbCache(this);
dbCache.clear(CallDbCache.TB_CALL);
String[] columns = new String[] { CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME };
Cursor cursor = getContentResolver().query(URI_CALL, columns, null, null, null);
while (cursor != null && cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
dbCache.insert(CallDbCache.TB_CALL, new LogData(id, number, name, "", "", "", ""));
}
cursor.close();
dbCache.close();
}

How do I change contact numbers to contact names?

Is there a way to display the contact names instead of the contact numbers?
This method returns the phone numbers within an inbox, but I need the names instead:
UPDATE: code changed from the original.
NOTE: I'm trying to change the contact numbers to display their corresponding contact names in the INBOX. Im not trying to get a list of all available contacts names.
public ArrayList<String> fetchInboxNumbers() {
ArrayList<String> sms = new ArrayList<String>();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null,
null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
String address = cursor.getString(1); // Displays phone number
sms.add(address);
}
return sms;
} // END FETCHINBOX
Is there a way to display the contact names instead of the contact
numbers?
Following Snippet will help you.
String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
}
nameCur.close();
I will like to add to #Vipul Shah answer.
wouldnt be better to add a projection with only the columns that you need to retreive?
new String [] { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }
Regards

How to read more than one column in database by cursor and fill into a list?

I have a table called tbl_homework. There are 3 columns called "_id", "hw" and "hwdate".
Now I like to read out "hw" AND "hwdate". So I like it on the same line in this listview.
I have to say that it is working without crash or error. It just don't show the date...
Following picture will tell you what I mean:
Here are the code snippets to tell you how I tried this:
private Button.OnClickListener add_hw = new Button.OnClickListener(){
public void onClick(View arg0) {
mDbHelper.open_database_rw();
String txt_insert_hw = insert_hw.getText().toString();
if(txt_insert_hw.equals("")){
doMessage("Keine Eingabe!");
}else{
String date_picker_message= date_pick.getDayOfMonth() + "/" + (date_pick.getMonth()+1) + "/" + date_pick.getYear();
final String INSERT_HW = "INSERT INTO tbl_homework ('hw', 'hwdate') VALUES ('"+txt_insert_hw+"', '"+date_picker_message+"')";
db.execSQL(INSERT_HW);
insert_hw.setText("");
fillData();
}
}
};
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { dbHelper.KEY_TITLE, dbHelper.KEY_DATE};
int[] to = new int[] {R.id.txt_notes_row};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
And in mDbHelper:
public Cursor fetchAllNotes() {
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE}, null, null, null, null, null);
}
Soulution of this problem: (Received from Jury)
String[] from = new String[] { dbHelper.KEY_TITLE, dbHelper.KEY_DATE};
int[] to = new int[] {R.id.txt_notes_row, R.id.txt_notes_row2};
I had to add a new TextView. dbHelper.KEY_TITLE AND dbHelper.KEY_DATE coudn't be handle by one TextView. So I had to add a new TextView to handle the second part of my String "from".
I think the problem is that you try to match two columns (from) to only one container (to) here:
String[] from = new String[] { dbHelper.KEY_TITLE, dbHelper.KEY_DATE};
int[] to = new int[] {R.id.txt_notes_row};
You should specify two fields in the list row where you store your values. I.e. your R.id.txt_notes_row should contain two widgets (for instance, R.id.txtview1 and R.id.txtview2) where you can store your values from db.

Categories

Resources