如何将通话记录中的号码填充到 onClick 列表中?
How to populate numbers from call log into a onClick list?
我正在构建一个应用程序,我想在其中查看列表中的通话记录并在按下列表中的项目时拨打电话。
最好的选择是什么?
我想尝试制作一个自定义适配器,但我不确定这样做是否正确,或者是否有更简单的方法?
感谢您的帮助!
目前我正在使用文本视图和字符串缓冲区将我的通话记录填充到列表中。
public class LastCallActivity extends Activity {
private List<LastCallModel> wlistCalls;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_call);
if (ContextCompat.checkSelfPermission(LastCallActivity.this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(LastCallActivity.this, Manifest.permission.READ_CALL_LOG)) {
ActivityCompat.requestPermissions(LastCallActivity.this, new String[]{Manifest.permission.READ_CALL_LOG}, 1);
} else {
ActivityCompat.requestPermissions(LastCallActivity.this, new String[]{Manifest.permission.READ_CALL_LOG}, 1);
}
} else {
// do stuff
TextView textView = (TextView) findViewById(R.id.textViewen);
textView.setText(getCallDetails());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(LastCallActivity.this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.textViewen);
textView.setText(getCallDetails());
}
} else {
Toast.makeText(this, "No permission GRANTED!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private String getCallDetails() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details:\n\n");
while (managedCursor.moveToNext()) {
String phName = managedCursor.getString(name);
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
if (phName != null && phNumber != null) {
wlistCalls.add(new LastCallModel(phName, phNumber));
}
Date callDayTime = new Date(Long.valueOf(callDate));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
String dateString = formatter.format(callDayTime);
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nName: " + phName + "\nPhone Number: " + phNumber + " \nCallType: " + dir + "\nCall Date: " + dateString + "\nCall Duration: " + callDuration);
sb.append("\n-------------");
sb.append("\n*************");
sb.append("\n*************");
}
managedCursor.close();
return sb.toString();
}
}
我通过使用带有自定义适配器的 RecyclerView 解决了这个问题!
这是我的自定义适配器。
public class LastCallAdapter extends RecyclerView.Adapter<LastCallAdapter.ViewHolder> {
private LayoutInflater layoutInflater;
private Context mContext;
private ArrayList<String> phoneNumber;
public LastCallAdapter(Context mContext, ArrayList<String> phoneNumber) {
this.mContext = mContext;
this.phoneNumber = phoneNumber;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.from(parent.getContext()).inflate(R.layout.items_contacts, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.number.setText(phoneNumber.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callDial = new Intent(Intent.ACTION_DIAL);
callDial = callDial.setData(Uri.parse("tel:"+phoneNumber.get(position)));
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
mContext.startActivity(callDial);
return;
}
Toast.makeText(mContext, phoneNumber.get(position), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return phoneNumber.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView number;
LinearLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
number = itemView.findViewById(R.id.contact_number);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
而这个方法主要activity用于启动适配器
private void startAdapter(){
RecyclerView recyclerView = findViewById(R.id.recyclerViewen);
LastCallAdapter lastCallAdapter = new LastCallAdapter(this, phoneNumbers);
recyclerView.setAdapter(lastCallAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
我正在构建一个应用程序,我想在其中查看列表中的通话记录并在按下列表中的项目时拨打电话。 最好的选择是什么? 我想尝试制作一个自定义适配器,但我不确定这样做是否正确,或者是否有更简单的方法?
感谢您的帮助!
目前我正在使用文本视图和字符串缓冲区将我的通话记录填充到列表中。
public class LastCallActivity extends Activity {
private List<LastCallModel> wlistCalls;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_call);
if (ContextCompat.checkSelfPermission(LastCallActivity.this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(LastCallActivity.this, Manifest.permission.READ_CALL_LOG)) {
ActivityCompat.requestPermissions(LastCallActivity.this, new String[]{Manifest.permission.READ_CALL_LOG}, 1);
} else {
ActivityCompat.requestPermissions(LastCallActivity.this, new String[]{Manifest.permission.READ_CALL_LOG}, 1);
}
} else {
// do stuff
TextView textView = (TextView) findViewById(R.id.textViewen);
textView.setText(getCallDetails());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(LastCallActivity.this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.textViewen);
textView.setText(getCallDetails());
}
} else {
Toast.makeText(this, "No permission GRANTED!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private String getCallDetails() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details:\n\n");
while (managedCursor.moveToNext()) {
String phName = managedCursor.getString(name);
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
if (phName != null && phNumber != null) {
wlistCalls.add(new LastCallModel(phName, phNumber));
}
Date callDayTime = new Date(Long.valueOf(callDate));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
String dateString = formatter.format(callDayTime);
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nName: " + phName + "\nPhone Number: " + phNumber + " \nCallType: " + dir + "\nCall Date: " + dateString + "\nCall Duration: " + callDuration);
sb.append("\n-------------");
sb.append("\n*************");
sb.append("\n*************");
}
managedCursor.close();
return sb.toString();
}
}
我通过使用带有自定义适配器的 RecyclerView 解决了这个问题!
这是我的自定义适配器。
public class LastCallAdapter extends RecyclerView.Adapter<LastCallAdapter.ViewHolder> {
private LayoutInflater layoutInflater;
private Context mContext;
private ArrayList<String> phoneNumber;
public LastCallAdapter(Context mContext, ArrayList<String> phoneNumber) {
this.mContext = mContext;
this.phoneNumber = phoneNumber;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.from(parent.getContext()).inflate(R.layout.items_contacts, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.number.setText(phoneNumber.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callDial = new Intent(Intent.ACTION_DIAL);
callDial = callDial.setData(Uri.parse("tel:"+phoneNumber.get(position)));
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
mContext.startActivity(callDial);
return;
}
Toast.makeText(mContext, phoneNumber.get(position), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return phoneNumber.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView number;
LinearLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
number = itemView.findViewById(R.id.contact_number);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
而这个方法主要activity用于启动适配器
private void startAdapter(){
RecyclerView recyclerView = findViewById(R.id.recyclerViewen);
LastCallAdapter lastCallAdapter = new LastCallAdapter(this, phoneNumbers);
recyclerView.setAdapter(lastCallAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}