如何使用android中的复选框创建自定义日志列表

时间:2013-09-04 09:44:29

标签: android android-layout android-contacts

如何使用Android中的复选框创建自定义日志列表,如时间日期和呼叫类型,如未接来电,来电或拨出电话

enter image description here

2 个答案:

答案 0 :(得分:1)

使用复选框和2个图像视图为行创建视图,无论您需要什么。根据您的数据

在适配器中设置imageview的来源

例如:

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

同样可以做到

答案 1 :(得分: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);
    }
}

}