如何在android中使用复选框创建自定义日志列表
Posted
技术标签:
【中文标题】如何在android中使用复选框创建自定义日志列表【英文标题】:how to create custom list of logs with check box in android 【发布时间】:2013-09-07 18:16:19 【问题描述】:如何在 android 中使用复选框创建自定义日志列表,其中包含时间日期和未接来电、来电或去电等通话类型
【问题讨论】:
在发布此问题之前您是否进行过谷歌搜索? 是但没有得到适当的答复 【参考方案1】:使用复选框和 2 个图像视图以及您需要的任何内容为该行创建一个视图。根据您的数据在您的适配器中设置 imageview 的来源
例如:
if (incoming_call)
logImageView.setImageResource(your_resource);
else
logImageView.setImageResource(your_other_resource)
你也可以这样做
【讨论】:
【参考方案2】:我得到了答案
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.callblockerapp.R;
public class CallLogs extends Activity implements OnItemClickListener,
OnClickListener
List<String> nameList = new ArrayList<String>();
List<String> numberList = new ArrayList<String>();
List<String> typeList = new ArrayList<String>();
CallLogsAdapter callLogsAdapter;
private Button btnAddCallLogs;
ListView listViewAddFromLogs;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.setContentView(R.layout.add_from_call_logs);
getAllLogContacts(this.getContentResolver());
listViewAddFromLogs = (ListView) findViewById(R.id.listViewAddFromLogs);
callLogsAdapter = new CallLogsAdapter();
listViewAddFromLogs.setAdapter(callLogsAdapter);
listViewAddFromLogs.setOnItemClickListener(this);
listViewAddFromLogs.setItemsCanFocus(false);
listViewAddFromLogs.setTextFilterEnabled(true);
btnAddCallLogs = (Button) findViewById(R.id.btnAddFromCallLogs);
btnAddCallLogs.setOnClickListener(this);
@Override
public void onClick(View v)
StringBuilder checkedcontacts = new StringBuilder();
for (int i = 0; i < nameList.size(); i++)
if (callLogsAdapter.mCheckStates.get(i) == true)
checkedcontacts.append(nameList.get(i).toString());
checkedcontacts.append("\n");
else
Toast.makeText(CallLogs.this, checkedcontacts, Toast.LENGTH_LONG)
.show();
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
// TODO Auto-generated method stub
callLogsAdapter.toggle(arg2);
public void getAllLogContacts(ContentResolver cr)
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(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);
while (managedCursor.moveToNext())
String nameLog = managedCursor.getString(name);
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
// String callDate = managedCursor.getString(date);
// Date callDayTime = new Date(Long.valueOf(callDate));
// 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;
nameList.add(nameLog);
numberList.add(phNumber);
typeList.add(dir);
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
+ dir);
sb.append("\n----------------------------------");
managedCursor.close();
// call.setText(sb);
class CallLogsAdapter extends BaseAdapter implements
CompoundButton.OnCheckedChangeListener
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
private TextView txtName, txtNumber, txtType;
CheckBox cb;
CallLogsAdapter()
mCheckStates = new SparseBooleanArray(nameList.size());
mInflater = (LayoutInflater) CallLogs.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
// TODO Auto-generated method stub
return nameList.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return position;
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return 0;
@Override
public View getView(final int position, View convertView,
ViewGroup parent)
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = mInflater.inflate(R.layout.add_from_call_logs_row, null);
txtName = (TextView) vi.findViewById(R.id.txtCallLogsName);
txtNumber = (TextView) vi.findViewById(R.id.txtCallLogsNumber);
txtType = (TextView) vi.findViewById(R.id.txtCallLogsType);
cb = (CheckBox) vi.findViewById(R.id.chkBoxCallLogs);
txtName.setText("Name :" + nameList.get(position));
txtNumber.setText("Phone No :" + numberList.get(position));
txtType.setText("Type :- " + typeList.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
public boolean isChecked(int position)
return mCheckStates.get(position, false);
public void setChecked(int position, boolean isChecked)
mCheckStates.put(position, isChecked);
public void toggle(int position)
setChecked(position, !isChecked(position));
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
【讨论】:
以上是关于如何在android中使用复选框创建自定义日志列表的主要内容,如果未能解决你的问题,请参考以下文章
Kivymd:如何在不创建自定义类的情况下在列表项中添加复选框?
Android - 通过复选框使用自定义适配器从列表视图中获取项目