如何使用复选框将选定的消息发送到特定的否?

时间:2014-01-29 09:53:43

标签: android listview sms custom-adapter

首先我将所有消息提取到我的应用程序以存储在列表视图中然后我正在使用复选框从列表视图中选择消息并希望将这些选定的短信发送到单个数字是预定义的,并希望将选定的短信作为邮件正文发送到单个号码,但 问题 是发送的 消息包含完整的列表视图消息,而不是选定的消息。 所以请某人 纠正我代码错误 因为我希望仅发送选定的消息而不是完整的列表视图项目(消息)


public class MainActivity extends Activity implements OnItemClickListener, OnClickListener{

Button send;
ListView listViewSMS;
Cursor cursor;
SMSListAdapter smsListAdapter;
Context context;
SharedPreferences prefs=null;
ArrayAdapter<SMSListModel> adapter;
List<SMSListModel> list = new ArrayList<SMSListModel>();
TextView textViewSMSSender, textViewSMSBody;
int i;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    listViewSMS=(ListView)findViewById(R.id.lvSMS);

    send = (Button)findViewById(R.id.btnproperty);
    send.setOnClickListener(this);

    textViewSMSSender=(TextView)findViewById(R.id.tvSMSSend);
    textViewSMSBody=(TextView)findViewById(R.id.tvSMSBody);

    cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

    smsListAdapter = new SMSListAdapter(this,getModel());
    listViewSMS.setAdapter(smsListAdapter);
    listViewSMS.setOnItemClickListener(this);


}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
    TextView label = (TextView) v.getTag(R.id.tvSMSSend);
    CheckBox checkbox = (CheckBox) v.getTag(R.id.cbSelect);
    Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();      
}

private String isCheckedOrNot(CheckBox checkbox) {
    if(checkbox.isChecked())
        return "is checked";
    else
        return "is not checked";
}

private List<SMSListModel> getModel() {

    if(cursor.getCount()>0){
        for(i=0;i<cursor.getCount();i++){
            if(cursor.moveToPosition(i)){
                list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))));
            }
        }
    }


    return list;
}
@Override
public void onClick(View v) {



    if( v == send){
        mDialog();

    }



public void mDialog(){



     // Show The Dialog with Selected SMS 
     AlertDialog dialog = new AlertDialog.Builder(context).create();
     dialog.setTitle("Message App");
     dialog.setIcon(android.R.drawable.ic_dialog_info);
     dialog.setMessage("Count : ");
     dialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok",
             new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) 
         {
             String phoneNo = "111";
             if(list.size()>0){
                 for(i=0;i<list.size();i++){
                     if(list.get(i).isSelected()){

                         try{
                             SmsManager smsManager = SmsManager.getDefault();
                             StringBuilder builder = new StringBuilder();
                             for(SMSListModel p: list){
                                 builder.append(p.toString());
                                 builder.append('\n');
                             }
                            String sms = builder.toString();
                             smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                          Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();

                         }

                         catch (Exception e){
                             Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
                             e.printStackTrace();

                         }
                         dialog.dismiss();

                 }
              }
            }
         }
     });

     dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
            new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "SMS not Sent",Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                    }
                });
     dialog.show();

}

public class SMSListAdapter extends ArrayAdapter<SMSListModel> {

private final List<SMSListModel> list;
private final Activity mContext;
boolean checkAll_flag = false;
boolean checkItem_flag = false;

public SMSListAdapter(Activity context,List<SMSListModel> list) 
{
    super(context, R.layout.listview_each_item, list);
    mContext = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView textAddress;
    protected TextView textBody;
    protected CheckBox checkbox;
}

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

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = mContext.getLayoutInflater();
        convertView = inflator.inflate(R.layout.listview_each_item, null);
        viewHolder = new ViewHolder();
        viewHolder.textAddress = (TextView) convertView.findViewById(R.id.tvSMSSend);
        viewHolder.textBody = (TextView) convertView.findViewById(R.id.tvSMSBody);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.cbSelect);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                    }
                });
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.tvSMSSend, viewHolder.textAddress);
        convertView.setTag(R.id.tvSMSBody, viewHolder.textBody);
        convertView.setTag(R.id.cbSelect, viewHolder.checkbox);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.checkbox.setTag(position); // This line is important.

    viewHolder.textAddress.setText(list.get(position).getAddress());
    viewHolder.textBody.setText(list.get(position).getBody());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());    

    return convertView;
}

public class SMSListModel {

private String address;
String body;
private boolean selected;

public SMSListModel(String address, String body) {
    this.address = address;
    this.body = body;
}

public String getAddress() {
    return address;
}

public String getBody() {
    return body;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public String toString(){

    return body;
}}

2 个答案:

答案 0 :(得分:0)

我可以知道您为什么要选择多行。我是指通过选择多行来实现您想要执行的确切操作?

以下是为您更新的代码。如果您没有收到任何密码,请告知我们:

    ---------------
    package com.example.multiselectlist;

    import java.util.ArrayList;
    import java.util.List;

    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnItemClickListener, OnClickListener{

        Button btnDelete;
        ListView listViewSMS;
        Cursor cursor;
        SMSListAdapter smsListAdapter;
        Context context;

        ArrayAdapter<SMSListModel> adapter;
        List<SMSListModel> list = new ArrayList<SMSListModel>();


        int count = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context=this;
            listViewSMS=(ListView)findViewById(R.id.lvSMS);
            btnDelete = (Button)findViewById(R.id.buttonDelete);

            cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

            smsListAdapter = new SMSListAdapter(this,getModel());
            listViewSMS.setAdapter(smsListAdapter);
            listViewSMS.setOnItemClickListener(this);

            btnDelete.setOnClickListener(this);
        }
        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
            TextView label = (TextView) v.getTag(R.id.tvSMSSend);
            CheckBox checkbox = (CheckBox) v.getTag(R.id.cbSelect);
            Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();      
        }

        private String isCheckedOrNot(CheckBox checkbox) {
            if(checkbox.isChecked())
                return "is checked";
            else
                return "is not checked";
        }

        private List<SMSListModel> getModel() {

            if(cursor.getCount()>0){
                for(int i=0;i<cursor.getCount();i++){
                    if(cursor.moveToPosition(i)){
                        list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))));
                    }
                }
            }


            return list;
        }
        @Override
        public void onClick(View v) {
            int id = v.getId();

            switch(id){
                case R.id.buttonDelete:
                    if(list.size()>0){
                        for(int i=0;i<list.size();i++){
                            if(list.get(i).isSelected()){
                                list.remove(i);
                            }
                        }

                        smsListAdapter = new SMSListAdapter(this,list);
                        smsListAdapter.notifyDataSetChanged();
                        listViewSMS.setAdapter(smsListAdapter);
                    }
                    break;
                    case R.id.buttonSend:
 String contactNum = "+123456789";
            String messageList = "";
            if(list.size()>0){
                for(int i=0;i<list.size();i++){
                    if(list.get(i).isSelected()){
                        if(messageList.equals(""))
                            messageList = list.get(i).getBody();
                        else
                            messageList = messageList+";"+list.get(i).getBody();
                    }
                }
                Log.v("messageList",""+messageList);
                Uri sendSmsTo = Uri.parse("smsto:" + contactNum);
                Log.d("sendSmsTo",""+sendSmsTo);
                Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, sendSmsTo);
                intent.putExtra("sms_body", messageList);
                startActivityForResult(intent, 100);    
            }

            }

        }
    }

SMSListModel上课:

    package com.example.multiselectlist;

    public class SMSListModel {

        private String address;
        String body;
        private boolean selected;

        public SMSListModel(String address, String body) {
            this.address = address;
            this.body = body;
        }

        public String getAddress() {
            return address;
        }

        public String getBody() {
            return body;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    }

SMSListAdapter课程:         package com.example.multiselectlist;

    import java.util.List;

    import android.app.Activity;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.TextView;

    public class SMSListAdapter extends ArrayAdapter<SMSListModel> {

        private final List<SMSListModel> list;
        private final Activity mContext;
        boolean checkAll_flag = false;
        boolean checkItem_flag = false;

        public SMSListAdapter(Activity context,List<SMSListModel> list) 
        {
            super(context, R.layout.listview_each_item, list);
            mContext = context;
            this.list = list;
        }

        static class ViewHolder {
            protected TextView textAddress;
            protected TextView textBody;
            protected CheckBox checkbox;
        }

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

            ViewHolder viewHolder = null;
            if (convertView == null) {
                LayoutInflater inflator = mContext.getLayoutInflater();
                convertView = inflator.inflate(R.layout.listview_each_item, null);
                viewHolder = new ViewHolder();
                viewHolder.textAddress = (TextView) convertView.findViewById(R.id.tvSMSSend);
                viewHolder.textBody = (TextView) convertView.findViewById(R.id.tvSMSBody);
                viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.cbSelect);
                viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                                list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                            }
                        });
                convertView.setTag(viewHolder);
                convertView.setTag(R.id.tvSMSSend, viewHolder.textAddress);
                convertView.setTag(R.id.tvSMSBody, viewHolder.textBody);
                convertView.setTag(R.id.cbSelect, viewHolder.checkbox);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.checkbox.setTag(position); // This line is important.

            viewHolder.textAddress.setText(list.get(position).getAddress());
            viewHolder.textBody.setText(list.get(position).getBody());
            viewHolder.checkbox.setChecked(list.get(position).isSelected());    

            return convertView;
        }

    }

xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

         <Button
            android:id="@+id/buttonDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Delete" />

        <ListView
            android:id="@+id/lvSMS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/buttonDelete" >
        </ListView>

    </RelativeLayout>

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cbSelect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" />

         <TextView
            android:id="@+id/tvSMSSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/cbSelect"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/cbSelect"
            android:text="9998698700" />

        <TextView
            android:id="@+id/tvSMSBody"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/tvSMSSend"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/tvSMSSend"
            android:text="body" />

    </RelativeLayout>

答案 1 :(得分:0)

您可以随时使用:

yourListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

要选择多个项目,请记住其界面取决于您创建它。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
    android:state_pressed="true"
    android:drawable="@color/white" />
<item
    android:state_selected="true"
    android:drawable="@drawable/list_item_bg_selected" />
<item 
    android:drawable="@color/list_bg" />
</selector>