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.
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();
Collections.reverse(sms);
}
public void populateMessageList() {
sms1 = new ArrayList<>();
fetchDatabaseMessage();
lv.setAdapter(new datalist(getApplicationContext(), sms1));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
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);
}
});
}
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());
}
}else {
System.out.println("SMS Not Displayed");
}
}
class datalist extends BaseAdapter {
Context context;
ArrayList<Message> arrayListsms;
public datalist(Context context, ArrayList<Message> arrayListsms) {
this.context = context;
this.arrayListsms = arrayListsms;
}
@Override
public int getCount() {
return sms.size();
}
@Override
public Object getItem(int position) {
return getItem(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = getLayoutInflater();
View row;
row = inflator.inflate(R.layout.row, parent, false);
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);
Calendar mcalendar = Calendar.getInstance();
DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
txt1.setText(sms.get(position).getmBody());
txt2.setText(sms.get(position).getmAddress());
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) {
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);
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 {
}
}
});
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