Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created custom ListView with CheckBox next to title. I want to get checked  CheckBox from ListView

I created following function for get message from ListView and send it to another ListView with Intent but I cant get the particular CheckBox position to move.


Java
public class SpamActivity extends AppCompatActivity {

List<Message> sms;
ArrayList<Message> sms1;
ArrayList<String> al;
ListView lv;
CheckBox checkbox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.messagebox);

    lv= (ListView) findViewById(R.id.lvMsg);
    checkbox = (CheckBox) findViewById(R.id.checkBox);
     sms = new ArrayList<>();
     al=new ArrayList<>(0);
    populateMessageList();

  /*  SpamActivity spamactivity = new SpamActivity();
    al = spamactivity.al;
    //InboxFragment inboxfragment = new InboxFragment();
    //al = inboxfragment.list;

    for (int i=0;i<sms.size();i++){
        System.out.println("AddressSPS" + sms.get(i));
    }                */
    Collections.reverse(sms);

}

public void populateMessageList() {
     sms1 = new ArrayList<>();
    fetchDatabaseMessage();

    lv.setAdapter(new datalist(getApplicationContext(), sms1));

        // to handle click event on listView item
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                // when user clicks on ListView Item , onItemClick is called
                // with position and View of the item which is clicked
                // we can use the position parameter to get index of clicked item
                TextView textViewSMSSender = (TextView) v.findViewById(R.id.lblNumber);
                TextView textViewSMSBody = (TextView) v.findViewById(R.id.lblMsg);
                TextView textViewSMSDate = (TextView) v.findViewById(R.id.smsdate);
                String smsSender = textViewSMSSender.getText().toString();
                String smsBody = textViewSMSBody.getText().toString();
                String smsDate = textViewSMSDate.getText().toString();

                Intent intentspam = new Intent(getApplicationContext(), DisplaySpamsms.class);
                intentspam.putExtra("number",smsSender);
                intentspam.putExtra("msg",smsBody);
                startActivity(intentspam);

            }
        });
}

// This method featch the message stored in database
public  void fetchDatabaseMessage(){
    DB_Message dbmessage = new DB_Message(this);
    sms = dbmessage.ViewMessageData();
    String addr = sms.get(0).getmAddress();
    if (addr.equals(sms.get(0).getmAddress())) {
        for (int i = 0; i < sms.size(); i++) {
            al.add(sms.get(i).getmAddress());
            //al.add(sms.get(i).getmBody());
        }
    }else {
        System.out.println("SMS Not Displayed");

    }
}


class datalist extends BaseAdapter {

    Context context;
    ArrayList<Message> arrayListsms;

    public datalist(Context context, ArrayList<Message> arrayListsms) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.arrayListsms = arrayListsms;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return sms.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return getItem(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
        LayoutInflater inflator = getLayoutInflater();
        View row;
        row = inflator.inflate(R.layout.row, parent, false);
        //  ImageView img1 = (ImageView) row.findViewById(R.id.imageView1);
        final CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
        final TextView txt1 = (TextView) row.findViewById(R.id.lblMsg);
        final TextView txt2 = (TextView) row.findViewById(R.id.lblNumber);
        final TextView txt3 = (TextView) row.findViewById(R.id.smsdate);
        //Long timestamp = Long.parseLong(sms.get(position).getmDate());
        Calendar mcalendar = Calendar.getInstance();
        //mcalendar.setTimeInMillis(timestamp);
        DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");


        txt1.setText(sms.get(position).getmBody());
        txt2.setText(sms.get(position).getmAddress());
        //+"\n"+mformatter.format(mcalendar.getTime())
        txt3.setText(mformatter.format(mcalendar.getTime()));

        final String msgBody = txt1.getText().toString();
        final String msgAddr = txt2.getText().toString();
        final String msgDate = txt3.getText().toString();

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Message msg = new Message();
                if (isChecked) {
                    //  selectedStrings.add(tv.getText().toString());

                    Toast.makeText(context, "" + msgBody, Toast.LENGTH_LONG).show();

                    System.out.println("Body" + msgBody);
                    System.out.println("Address" + msgAddr);
                    System.out.println("Date" + msgDate);

                      al.add(msgBody + msgAddr + msgDate);
                    //                        al.add(msgAddr);
                   //                        al.add(msgDate);

                    sms.remove(position);
                    ContentValues values = new ContentValues();
                    values.put("address", sms.get(position).getmAddress());
                    values.put("body", sms.get(position).getmBody());
                    String date = msg.getmDate();
                    SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss");
                    getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

                    notifyDataSetChanged();
                } else {
                    //   selectedStrings.remove(tv.getText().toString());
                }

            }
        });


        return row;
    }
}

}


When I used above function for sending message but error is that when I click on any CheckBox to send that message then automatically Different Message is sent to another activity. so, I want to get particular message position to sent it.


What I have tried:

I tried getTag() and setTag() methods of check box for getting and setting position, but this not work
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900