Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an contact list from which user select the contacts and send those contacts to other class,when user came back to activity selected contact remain selected,i compare the list from both the activity,and toast them,toast giving me the ans,but then also checkbox is not remained checked,what to do to made them checked always..



package com.example.smscampaign;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.ContactsContract;
import android.telephony.gsm.SmsManager;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
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 android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnItemClickListener {

ArrayList name1 = new ArrayList();
static ArrayList phno1 = new ArrayList();
ArrayList phno0 = new ArrayList();
MyAdapter ma;
Button send;
String[] cellArray = null;
int[] str;
int v = 0;
String contacts;
static int check1;
ListView lv;
int index;
int top;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.get);
TextView txt = (TextView) findViewById(R.id.textView1);
getAllCallLogs(this.getContentResolver());
lv = (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contact_main, menu);

return true;

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

switch (item.getItemId()) {
case R.id.addPage:

break;

}
return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(SmsSend.conct != null)
{
StringBuilder b= SmsSend.conct;
//Toast.makeText(getBaseContext(), b, Toast.LENGTH_LONG).show();

// b3.append("");
contacts = b.toString() ;

cellArray = contacts.split(";"); //matching the phno with selected contacts came from activity a
for(int i=0; i<cellarray.length;i++)>
{
for(int j=0; j< phno1.size();j++){

if(cellArray[i].equals(phno1.get(j)) ){
Toast.makeText(getBaseContext(), cellArray[i] + "hii" +phno1.get(j), Toast.LENGTH_LONG)
.show();
ma.cb.setChecked(ma.mCheckStates.get(j, true));

break;
}
}
}
}
ArrayList mylist = new ArrayList();
StringBuilder checkedcontacts = new StringBuilder();

System.out.println(ma.mCheckStates.size());
for (int i = 0; i < name1.size(); i++)

{
if (ma.mCheckStates.get(i) == true) {

phno0.add(phno1.get(i).toString());
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
ma.cb.setChecked( true);
} else {
System.out.println("..Not Checked......"
+ name1.get(i).toString());
}

}


Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);

setResult(RESULT_OK, returnIntent);

finish();
}

@Override
public void onItemClick(AdapterView // TODO Auto-generated method stub
ma.toggle(arg2);

}

public void getAllCallLogs(ContentResolver cr) {

Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

System.out.println(phoneNumber);

name1.add(name);
phno1.add(phoneNumber);

}


phones.close();


}

class MyAdapter extends BaseAdapter implements
CompoundButton.OnCheckedChangeListener {
public SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;

MyAdapter() {
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

// Save ListView state

@Override
public int getCount() {
// TODO Auto-generated method stub
return name1.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.row, null);
tv = (TextView) vi.findViewById(R.id.textView1);
tv1 = (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText(name1.get(position));
tv1.setText(phno1.get(position));
cb.setTag(position);

cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
Collections.sort(name1);



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);

}
}

}




SmsSend.java



package com.example.smscampaign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class SmsSend extends Activity implements OnClickListener {
BroadcastReceiver smsSentReciver, smsSentDelivery;
EditText ed1, ed2;
static int ResultCode = 12;
static ArrayList sendlist = new ArrayList();
Button b1, b2, b3, b4;
TextView txt;
static StringBuilder conct = new StringBuilder();
String contacts = "";
String delim = ";";
public static String Name;
TextView ed;
int i=0;
DataBaseHandler notasdb = new DataBaseHandler(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.smssend);

ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(this);
b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(this);
b4 = (Button) findViewById(R.id.button4);
b4.setOnClickListener(this);
txt = (TextView) findViewById(R.id.textnum2);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(smsSentReciver);
unregisterReceiver(smsSentDelivery);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

smsSentReciver = new BroadcastReceiver() {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "sms has been sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Fail",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
default:
break;

}
}

};
smsSentDelivery = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sms Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Sms not Delivered",
Toast.LENGTH_SHORT).show();
break;
}
}

};
registerReceiver(smsSentReciver, new IntentFilter("SMS_SENT"));
registerReceiver(smsSentDelivery, new IntentFilter("SMS_DELIVERED"));

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {

case R.id.button3:
Name = ed1.getText().toString();


Intent a = new Intent(SmsSend.this, MainActivity.class);
startActivityForResult(a, ResultCode);
break;
case R.id.button4:
Intent file = new Intent(SmsSend.this, File_Selecter.class);
startActivity(file);
break;

case R.id.button1:

break;

case R.id.button2:

Log.i("SMS", "Sendlist Size: " + sendlist.size());

boolean diditwork1 = true;

try {
String Name = ed1.getText().toString();

SmsManager smsManager = SmsManager.getDefault();

String msg = ed2.getText().toString();


PendingIntent piSend = PendingIntent.getBroadcast(this, 0,
new Intent("SMS_SENT"), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0,
new Intent("SMS_DELIVERED"), 0);

Log.i("SMS", "contacts: " + contacts);

String[] cellArray;
// b3.append("");
contacts = conct.toString() ;
Toast.makeText(getBaseContext(), contacts, Toast.LENGTH_LONG)
.show();
cellArray = contacts.split(";");

for (int a1 = 0; a1 < cellArray.length; a1++) {

// smsManager.sendTextMessage(cellArray[a1].toString(),
// null,
// msg, piSend, piDelivered);
}
DataBaseHandler entry = new DataBaseHandler(SmsSend.this);
entry.open();
entry.entryCreate(Name, msg);

entry.close();
} catch (Exception e) {
diditwork1 = false;
String erroe = e.toString();

Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(erroe);
d.setContentView(tv);
d.show();

} finally {
if (diditwork1) {
Dialog d = new Dialog(this);
d.setTitle("Heck Yeah!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
ed1.setText("");
ed2.setText("");
break;

}

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == ResultCode) {

if (resultCode == RESULT_OK) {
//Intent t = getIntent();
sendlist = data.getStringArrayListExtra("name");
if (sendlist != null) {

for (int i = 0; i < sendlist.size(); i++) {
conct.append(sendlist.get(i).toString());
conct.append(delim);
}

}

}



i = sendlist.size();
txt.setText(Integer.toString(i));
if (resultCode == RESULT_CANCELED) {

}

}
}


}
Posted
Comments
MK-Gii 17-Mar-14 10:24am    
That's a perfect example of an extremely bad question.
Not sure why - you either (sorry for wording) stupid expecting that someone will answer or lazy to ask normal question.

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