0

如何在 android 中使用复选框创建自定义日志列表,如未接来电、来电或去电等时间日期和呼叫类型

在此处输入图像描述

4

2 回答 2

1

使用复选框和 2 个图像视图以及您需要的任何内容为该行创建一个视图。根据您的数据在适配器中设置 imageview 的来源

例如:

if (incoming_call)
logImageView.setImageResource(your_resource);
 else {
logImageView.setImageResource(your_other_resource)
}

同样你可以做

于 2013-09-04T09:49:31.143 回答
0

我得到了答案

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);
    }
}

}
于 2013-09-04T11:30:11.937 回答