SmsAdapter.java
package com.s4starb4boy.smstopdf.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.s4starb4boy.smstopdf.R;
import com.s4starb4boy.smstopdf.model.SMS;
import java.util.List;
public class SmsAdapter extends RecyclerView.Adapter<SmsAdapter.SmsViewHolder> {
private static final String TAG = SmsAdapter.class.getSimpleName() ;
private List<SMS> smsList;
private LayoutInflater mInflater;
public SmsAdapter(Context context, List<SMS> smsList) {
this.smsList = smsList;
this.mInflater = LayoutInflater.from(context);
}
@Override
public SmsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i(TAG, "onCreateViewHolder");
View view = mInflater.inflate(R.layout.sms_list, parent, false);
SmsViewHolder holder = new SmsViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(SmsViewHolder holder, int position) {
Log.i(TAG, "onBindViewHolder" + position);
SMS currentObj = smsList.get(position);
holder.setData(currentObj, position);
}
@Override
public int getItemCount() {
return smsList.size();
}
class SmsViewHolder extends RecyclerView.ViewHolder{
TextView tvPhoneNo;
TextView tvsmsBody;
int position;
SMS current;
public SmsViewHolder(View itemView) {
super(itemView);
tvPhoneNo = (TextView) itemView.findViewById(R.id.tvPhoneNo);
tvsmsBody = (TextView) itemView.findViewById(R.id.tvMessageBody);
}
public void setData(SMS current, int position) {
this.tvPhoneNo.setText(current.getPhoneNo());
this.tvsmsBody.setText(current.getSmsBody());
this.current =current;
this.position = position;
}
}
}
SMS.java
public class SMS extends AppCompatActivity{
private String phoneNo;
private String smsBody;
public SMS(String phoneNo, String smsBody) {
this.phoneNo = phoneNo;
this.smsBody = smsBody;
}
public SMS() {
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getSmsBody() {
return smsBody;
}
public void setSmsBody(String smsBody) {
this.smsBody = smsBody;
}
public static ArrayList<String> getSMS(){
ArrayList<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
while (cur != null && cur.moveToNext()) {
String address = cur.getString(cur.getColumnIndex("address"));
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
sms.add("Number: " + address + " .Message: " + body);
}
if (cur != null) {
cur.close();
}
return sms;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int READ_SMS_PERMISSIONS_REQUEST = 1;
private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpToolbar();
setUpRecyclerView();
}
private void setUpRecyclerView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
SmsAdapter adapter = new SmsAdapter(this, SMS.getSMS());
recyclerView.setAdapter(adapter);
LinearLayoutManager mLinearLayoutManagerVertical = new LinearLayoutManager(this);
mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLinearLayoutManagerVertical);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
private void setUpToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Navigation Drawer Demo");
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String msg = "";
switch (item.getItemId()) {
case R.id.settings:
msg = "Settings";
break;
case R.id.edit:
msg = "Edit";
break;
case R.id.search:
msg = "Search";
break;
case R.id.exit:
msg = "Exit";
break;
case R.id.delete:
msg = "Delete";
break;
}
Toast.makeText(MainActivity.this, msg + " Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
Above are all code I want to fetch sms and display them in card view using recycler view. I am repeating here that I am absolutely beginner so please keep this point in your mind while giving me solution I may unable to get your professional hint rather ask me in detail plz.
Try to pass current context
object to the SMS
class
SmsAdapter adapter = new SmsAdapter(this, SMS.getSMS(this.getApplicationContext()));
and change getSMS
to:
public static ArrayList<String> getSMS(Context context){
.....
Cursor cur = context.getContentResolver().query(uriSMSURI, null, null, null, null);
.....
Unfortunately I don't have any Android IDE to test this but it should work.