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;
}
});
}
}
以上是我想要获取短信的所有代码,并使用recycleler视图在卡片视图中显示它们。我在这里重申,我绝对是初学者,所以请在解决问题的同时保持这一点,我可能无法得到你的专业提示,而是详细询问我。
答案 0 :(得分:0)
getContentResolver&gt;&gt;&gt;非静态成员无法引用
这意味着,您尝试从静态上下文访问非静态成员,以防getContentResolver()
是非静态方法,您尝试从静态方法调用,即getSMS ()
你可以这样做:
从static
删除getSMS
或将static
添加到getContentResolver
创建此类的实例以调用getContentResolver
答案 1 :(得分:0)
尝试将当前context
对象传递给SMS
类
SmsAdapter adapter = new SmsAdapter(this, SMS.getSMS(this.getApplicationContext()));
并将getSMS
更改为:
public static ArrayList<String> getSMS(Context context){
.....
Cursor cur = context.getContentResolver().query(uriSMSURI, null, null, null, null);
.....
不幸的是我没有任何Android IDE来测试它,但它应该可以工作。