listview没有在广播接收器onReceive中更新

时间:2014-02-20 17:29:32

标签: android listview broadcastreceiver adapter

作为测试,我正在尝试在发送文本时设置图标,但图标不会随状态而变化。我在同一个电话中放了一个日志,日志正在打印,但图标仍然没有更新。

我在onResume注册广播接收器:

smsSentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent arg1) {

        final int passedSentExtra = arg1.getIntExtra("SENT_EXTRA", -1);
        Log.d("Pete Debug", "passedSentExtra = " + String.valueOf(passedSentExtra));

        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d("Pete Debug", "Sent " + smsStatusItems.get(passedSentExtra).getMessage());
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                smsStatusItems.get(passedSentExtra).setImage(R.drawable.fail);
                smsStatusAdapter.notifyDataSetChanged();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                smsStatusItems.get(passedSentExtra).setImage(R.drawable.fail);
                smsStatusAdapter.notifyDataSetChanged();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                smsStatusItems.get(passedSentExtra).setImage(R.drawable.fail);
                smsStatusAdapter.notifyDataSetChanged();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                smsStatusItems.get(passedSentExtra).setImage(R.drawable.fail);
                smsStatusAdapter.notifyDataSetChanged();
                break;
        }
    }
};

smsDeliveredReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent arg1) {

        final int passedDeliveredExtra = arg1.getIntExtra("DELIVERED_EXTRA", -1);
        Log.d("Pete Debug", "passedDeliveredExtra = " + String.valueOf(passedDeliveredExtra));
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.d("Pete Debug", "Delivered " + smsStatusItems.get(passedDeliveredExtra).getMessage());
                smsStatusItems.get(passedDeliveredExtra).setImage(R.drawable.pass);
                smsStatusAdapter.notifyDataSetChanged();
                break;
            case Activity.RESULT_CANCELED:
                smsStatusItems.get(passedDeliveredExtra).setImage(R.drawable.fail);
                smsStatusAdapter.notifyDataSetChanged();
                break;
        }
    }
};

//---when the SMS has been sent---
getActivity().registerReceiver(smsSentReceiver, new IntentFilter(SMS_SENT));

//---when the SMS has been delivered---
getActivity().registerReceiver(smsDeliveredReceiver, new IntentFilter(SMS_DELIVERED));

我有一个for循环,它通过点击按钮发送文本。单击按钮,它会向每个选定的联系人发送文本。将出现一个对话框,显示每个联系人的状态,即图标显示的位置......

protected void sendingSmsDialogBeforeKitKat() {

        final Dialog smsSendingDialog = new Dialog(getActivity());
        smsSendingDialog.setContentView(R.layout.dialog_send_sms_status);
        smsSendingDialog.setTitle("Journey's End - Sending");
        smsSendingDialog.setCancelable(false);

        ListView listView = (ListView) smsSendingDialog.findViewById(R.id.lv_sms_status);

        smsStatusItems = new ArrayList<SmsStatusItems>();
        smsStatusAdapter = new SmsStatusAdapter(getActivity(), smsStatusItems);

        // set the exp list adapter to the exp listview
        listView.setAdapter(smsStatusAdapter);

        for (int i = 0; i < getSelectedNumbers().size(); i++) {
            Log.d("Pete Debug", "value of i = " + String.valueOf(i));

            String number = removeSpecialCharacters(getSelectedNumbers().get(i));
            String smsSending = "Sending to " + number + "... ";
            Log.d("Pete Debug", smsSending);

            statusItems = new SmsStatusItems();
            statusItems.setMessage(smsSending);
            statusItems.setImage(R.drawable.clock);
            smsStatusItems.add(statusItems);
            smsStatusAdapter.notifyDataSetChanged();

            sentIntent = new Intent(SMS_SENT);
            sentIntent.putExtra("SENT_EXTRA", i);

            deliveredIntent = new Intent(SMS_SENT);
            deliveredIntent.putExtra("DELIVERED_EXTRA", i);

            mSmsManager.sendTextMessage(number, null, getFinalMessage(), (PendingIntent.getBroadcast(getActivity(), 0,
                    sentIntent, 0)), (PendingIntent.getBroadcast(getActivity(), 0,
                    deliveredIntent, 0)));
        }
        smsSendingDialog.show();
        Button okButton = (Button) smsSendingDialog.findViewById(R.id.sms_status_ok_button);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().unregisterReceiver(smsSentReceiver);
                getActivity().unregisterReceiver(smsDeliveredReceiver);
                smsSendingDialog.dismiss();
                getActivity().finish();
            }
        });
    }

这是控制该对话框中列表视图的适配器......

public class SmsStatusAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<SmsStatusItems> mData;
    private ViewHolder viewHolder;

    public SmsStatusAdapter(Context context, ArrayList<SmsStatusItems> data) {

        mContext = context;
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public SmsStatusItems getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.sms_status_list_item, null);

            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_sms_status);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_sms_status_icon);

            convertView.setTag(viewHolder);
        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(getItem(position).getMessage());
        viewHolder.imageView.setImageResource(getItem(position).getImage());

        return convertView;
    }

    public class ViewHolder {

        TextView textView;
        ImageView imageView;
    }
}

1 个答案:

答案 0 :(得分:0)

原来问题是我没有收到发送报告,因为它们只支持sprint / verizon。代码有效,图像没有变化,因为我设置了交付报告成功的变化。叹息