Hai i'm trying to develop an app where by i can send sms and email to a particular group of people..
I have a listview showing the contacts which are in my group.Each row is of the form
TextView(Name)TextView(phone) Checkbox(sms)
TextView(email id) Checkbox(mail)
I have used custom adapter to display the contact details to the listview.ihave set the onitemclick listener to find the position of the row..
I have to send sms and email to those contacts for which checkboxes have been set as true.how can i find the state of each of the checkboxes.
Please help me..lotz of thanx in advance..
I hav added below the custom adapetr i hv created..
public class ContactInfoAdapter extends ArrayAdapter{
private ArrayList<Boolean> mChecked_sms,mChecked_email;
Context context;
int layoutResourceId;
ContactInfo data[] = null;
public ContactInfoAdapter(Context context, int layoutResourceId, ContactInfo[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
mChecked_sms = new ArrayList<Boolean>();
mChecked_email = new ArrayList<Boolean>();
for (int i = 0; i < this.getCount(); i++) {
mChecked_sms.add(i, false);
mChecked_email.add(i,false);
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ContactHolder holder;
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactHolder();
holder.txtName = (TextView)row.findViewById(R.id.textViewName);
holder.txtPhone = (TextView) row.findViewById(R.id.textViewPhone);
holder.txtEmail = (TextView) row.findViewById(R.id.textViewEmail);
holder.cb_sms_state = (CheckBox) row.findViewById(R.id.checkBox1);
holder.cb_email_state = (CheckBox) row.findViewById(R.id.checkBox2);
row.setTag(holder);
}
else
{
holder = (ContactHolder)row.getTag();
}
ContactInfo contact = data[position];
holder.txtName.setText(contact.name);
holder.cb_sms_state.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.cb_sms_state.isChecked()) {
mChecked_sms.set(position, true);
Toast.makeText(getContext(), "checked", 2).show();
} else {
mChecked_sms.set(position, false);
}
}
});
holder.cb_sms_state.setChecked(mChecked_sms.get(position));
holder.cb_email_state.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.cb_email_state.isChecked()) {
mChecked_email.set(position, true);
Toast.makeText(getContext(), "checked", 2).show();
} else {
mChecked_email.set(position, false);
}
}
});
holder.cb_email_state.setChecked(mChecked_email.get(position));
holder.txtPhone.setText(contact.number);
holder.txtEmail.setText(contact.email);
return row;
}
static class ContactHolder
{
TextView txtName;
TextView txtPhone;
TextView txtEmail;
CheckBox cb_sms_state;
CheckBox cb_email_state;
}
}
The ContactInfo class is :
public class ContactInfo {
public String name;
public String number;
public String email;
public boolean sms_state;
public boolean email_state;
public ContactInfo(){
super();
}
public ContactInfo(String name,String number,String email,boolean sms_state,boolean email_state) {
super();
this.name = name;
this.number = number;
this.email = email;
this.sms_state = sms_state;
this.email_state = email_state;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setNUmber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setSms_state(Boolean sms_state)
{
this.sms_state = sms_state;
}
public Boolean getSms_state(){
return sms_state;
}
public void setEmail_state(Boolean email_state)
{
this.email_state = email_state;
}
public Boolean getEmail_state(){
return email_state;
}
Inside the getView() method, you have to implement a OnCheckedChangeListener for the CheckBox.
Here is a listener code, say for example:
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
See the doc.
I think you need :
checkbox.isChecked()
Here's a simple sample:
mContactListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
CheckBox chkContact = (CheckBox) view.findViewById(R.id.listrow_contact_chkContact);
if (chkContact.isChecked()) {
...
}
});
I've had this problem with my app Hasta La Vista I've create a with custom listview with checked items and I needed to get checked items this was the solution:
ListView lv = getListView(); //ver listview
if(lv != null){
final SparseBooleanArray checkedItems = lv.getCheckedItemPositions(); //get checked items
if (checkedItems == null) {
return;
}
final int checkedItemsCount = checkedItems.size();
for (int i = 0; i < checkedItemsCount; ++i) {
// This tells us the item position we are looking at
final int position = checkedItems.keyAt(i);
// This tells us the item status at the above position
final boolean isChecked = checkedItems.valueAt(i);
if(isChecked){
//get item from list and do something
lv.getAdapter().getItem(position);
}
}
}
As ListView views are recycled, you can not rely on listening on particular instance, you will have to store "checked" state in your data model. You will also need custom list adapter, where you create and populate individual entries. Following shall be done:
- in overriden getView(), either create new view ( in case no convertView was supplied ) or inflate new one
- populate viewv fields from you data model
- remove old onclick listener, and set new one ( can be anonymous inner class ) modifying your data model
PS: recycling views is important if your list is big
Related
Below is ListView Item Class
public class CategoryItem06 {
private String text;
private boolean checked;
public void setText(String text) {
this.text = text;
}
public String getText() {
return this.text;
}
// public void setCheck(boolean checked) {
this.checked = checked;
}
// public boolean getCheck() {
return this.checked;
}
}
Below is Adapter
public class CategoryAdapter06 extends BaseAdapter {
public ArrayList<CategoryItem06> listViewItemList = new ArrayList<CategoryItem06>() ;
public CategoryAdapter06() {
}
#Override
public int getCount() {
return listViewItemList.size() ;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
final Context context = parent.getContext();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_item06, parent, false);
}
TextView textTextView = (TextView) convertView.findViewById(R.id.textView1) ;
CheckBox checkBox=(CheckBox) convertView.findViewById(R.id.checkBoxMafia);
CategoryItem06 listViewItem = listViewItemList.get(position);
textTextView.setText(listViewItem.getText());
checkBox.setChecked(listViewItem.getCheck());
return convertView;
}
#Override
public long getItemId(int position) {
return position ;
}
#Override
public Object getItem(int position) {
return listViewItemList.get(position) ;
}
public void addItem( String text) {
CategoryItem06 item = new CategoryItem06();
item.setText(text);
listViewItemList.add(item);
}
}
Below is Checkable Relative Layout
public class CategoryCheckableRelativeLayout extends RelativeLayout implements Checkable {
public CategoryCheckableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// mIsChecked = false ;
}
#Override
public boolean isChecked() {
CheckBox cb = (CheckBox) findViewById(R.id.checkBoxMafia);
return cb.isChecked();
// return mIsChecked ;
}
#Override
public void toggle() {
CheckBox cb = (CheckBox) findViewById(R.id.checkBoxMafia);
setChecked(cb.isChecked() ? false : true);
// setChecked(mIsChecked ? false : true) ;
}
#Override
public void setChecked(boolean checked) {
CheckBox cb = (CheckBox) findViewById(R.id.checkBoxMafia);
if (cb.isChecked() != checked) {
cb.setChecked(checked);
}
}
}
Below is Activity that uses ListView
public class CategorySelection06 extends AppCompatActivity {
Singleton s1 = Singleton.getInstance();
ListView listview;
// Creating Adapter
CategoryAdapter06 adapter = new CategoryAdapter06();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_selection06);
listview = (ListView) findViewById(R.id.listview1);
listview.setAdapter(adapter);
// Adding Items
adapter.addItem("Pets");
adapter.addItem("Singers");
adapter.addItem("Game");
adapter.addItem("Nations");
Button button = findViewById(R.id.button6);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i < adapter.listViewItemList.size(); i++) {
if (adapter.listViewItemList.get(i).getCheck()) {
s1.ListViewCategory.add(adapter.listViewItemList.get(i).getText());
}
}
Intent intent = new Intent(getApplicationContext(), RoleSelection07.class);
startActivity(intent);
finish();
}
});
}
}
My ListView's form is like this: TextView ------- Checkbox
I want to make an Activity like this: if user checks checkbox, then the checked row's text is saved in ArrayList in Singleton class.
For example, if a user checked checkbox of "Pets" and "Nations" then these words goes into the ArrayList s1.ListViewCategory, which is in Singleton class.
I've tried for loops and if statements in CategorySelectionActivity like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i < adapter.listViewItemList.size(); i++) {
if (adapter.listViewItemList.get(i).getCheck()) {
s1.ListViewCategory.add(adapter.listViewItemList.get(i).getText());
}
}
Intent intent = new Intent(getApplicationContext(), RoleSelection07.class);
startActivity(intent);
finish();
}
However,getCheck() doesn't work because setCheck() is not in the addItem() in CategoryAdapter class.
I tried to put setCheck() in the addItem() method , but then I have to put another parameter in add(), then I got red lines and errors.
Since I am a novice, I copied these codes from sites, but I don't really get the idea of using CheckableRelativeLayout.
This Layout shows that the checkbox is checked or not, but it doesn't indicate which row is checked.
To sum up, my question is ' how can I get texts from multiple rows that are checked, and know which row is checked ?
I know the question is super long, but I really need to solve this problem...
I will be super grateful if someone answers my question Thank you
Nobody answered so I fixed it by my own.
SparseBooleanArray checkedItems = listview.getCheckedItemPositions();
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
if (checkedItems.get(i)) {
s1.ListViewCategory.add(adapter.listViewItemList.get(i).getText());
}
}
listview.clearChoices();
Intent intent = new Intent(getApplicationContext(), RoleSelection07.class);
startActivity(intent);
finish();
I am working on android example, when i click on checkbox then it gets the textviews value of first item(Position) in listview every time. so but i want to get value of selected (position) checkbox textview value. how to solve it please help .i am a fresher.Thanks in advances.
Some Code In BaseAdapter class
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtTitle = (TextView)convertView.findViewById(R.id.inactivelistview);
// viewItem.txtDescription = (TextView)convertView.findViewById(R.id.adapter_text_description);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtTitle.setText(valueList.get(position).username);
// viewItem.txtDescription.setText(valueList.get(position).cources_description);
return convertView;
}
Some Code in activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_in_active_list);
listCollege = (ListView)findViewById(R.id.listCollege);
proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
checkbox = (CheckBox)findViewById(R.id.checkbox_me);
button =(Button)findViewById(R.id.button1);
new GetHttpResponse(this).execute();
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.checkbox_me:
if (checked) {
username = (TextView)findViewById(R.id.inactivelistview);
Username =username.getText().toString();
System.out.println("print username_=== "+Username);
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("Do you want activate "+Username+"?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), Username+" acivated", Toast.LENGTH_SHORT).show();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
}
});
alertbox.show();
} else
break;
}
I am getting textviews value of first item(Position) in listview every time.
Please help me how to solve.help me update my code
Thank you so much.......
Note that checkbox return array as user can tick multiple elements.
ArrayList<String> selectedStrings = new ArrayList<String>();
The answer describe it in details.
use gettag on onCheckboxClicked method so that you can identify which row number checkbox is click . then you can get the textview of that row use row number
If your CheckBox is in ListView then no need to create ClickListener in Activity. Follow below steps to get selected text from ListView.
First create a model/pojo class, this will help you to store reference of selected CheckBox and also values which is going to show in ListView.
public class MyModel {
private boolean isSelected;
private String name;
public MyModel(boolean isSelected, String name) {
this.isSelected = isSelected;
this.name = name;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
this.isSelected = selected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create a interface which will work as a callback.
public interface ItemSelectListener {
void getSelectedItemText(String text);
}
Refactor your BaseAdapter as now it will accept list of our Model class and also callback listener.
public MyAdapter extends BaseAdapter {
private final List<MyModel> mDataItems;
private final ItemSelectListener mItemListener;
public MyAdapter(List<MyModel> dataItems, ItemSelectListener itemListener)
mDataItems = dataItems;
mItemListener = itemListener;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null) {
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtTitle = (TextView)convertView.findViewById(R.id.inactivelistview);
// Add checkbox in your view item and confirm id of checkbox
viewItem.checkBox = (CheckBox)convertView.findViewById(R.id. checkbox_me);
}
else {
viewItem = (ViewItem) convertView.getTag();
}
final MyModel data = mDataItems.get(position);
viewItem.txtTitle.setText(data.getName());
viewItem.checkBox.setChecked(data.isSelected());
viewItem.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton button, boolean checked)
{
data.setSelected(checked);
if(checked) {
// Make sure you override this in your Activity
mItemListener.getSelectedItemText(data.getName());
}
}
});
return convertView;
}
}
Create datasource for ListView in your Activity.
BaseAdapter adapter = new BaseAdapter(dataItems, itemListener);
i have 2 arrays productname and price. my each row in listview contains productname,price and an image button remove. when i click remove button,the selected productname and price should be removed from my array as well as from my listview. Please guide me doing this,is very important for me. If there is another way of doing this plz let me know.i m student only!
this is my CartActivity
public class CartActivity extends Activity implements View.OnClickListener {
CartAdapter contactAdapter;
ListView listView;
public String productname[]=new String[10];
public String price[]=new String[10];
int i=0,m;
CartActivity(String scanContent){
this.content = scanContent;
}
public CartActivity() {}
String product,Price;
String pri,prod;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
listView = (ListView) findViewById(R.id.cart_list_view);
contactAdapter = new CartAdapter(this,R.layout.activity_cart_list_view);
listView.setAdapter(contactAdapter);
intent = getIntent();
productname = intent.getStringArrayExtra("productname");
price = intent.getStringArrayExtra("price");
for(int j=0;j<price.length;j++) {
product = productname[j];
Price = price[j];
if (Price != null || product != null) {
Cart contacts = new Cart(product, Price);
contactAdapter.add(contacts);
}
}
}
this is my CartAdapter
public class CartAdapter extends ArrayAdapter {
List list = new ArrayList();
int ind,x=0;
public CartAdapter(CartActivity context, int resource) {
super(context, resource);
}
public void add(Cart object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
ContactHolder contactHolder;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.activity_cart_list_view,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_name =(TextView) row.findViewById(R.id.product_name);
contactHolder.tx_price =(TextView) row.findViewById(R.id.price);
contactHolder.cancelButton = (ImageButton) row.findViewById(R.id.cancel_button);
row.setTag(contactHolder);
}
else
{
contactHolder = (ContactHolder)row.getTag();
}
cancelButton.setTag(position);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer index = (Integer) view.getTag();
list.remove(index.intValue());
notifyDataSetChanged();
}
});
Cart contacts = (Cart) this.getItem(position);
contactHolder.tx_name.setText(contacts.getItemname());
contactHolder.tx_price.setText(contacts.getPrice());
return row;
}
static class ContactHolder
{
TextView tx_name,tx_price;
ImageButton cancelButton;
}
}
this is my Cart
public class Cart {
private String itemname,price;
public Cart(String itemname,String price) {
this.setItemname(itemname);
this.setPrice(price);
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public void setPrice(String price) {
this.price = price;
}
public String getItemname() {
return itemname;
}
public String getPrice() {
return price;
}
}
my each row in a listview contains this
Just do this,
in your adapter class,
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
list.remove(position); //position is getView position
// above line will remove from arraylist
notifyDataSetChanged(); //this line reflects change in listview
}
});
To remove from Activity class,
better create an ArrayList first.
ArrayList<Cart> cardList = new ArrayList<Cart>();
add into arrayList,
if (Price != null || product != null) {
Cart contacts = new Cart(product, Price);
cardList.add(contacts);
contactAdapter.add(contacts); // this line should be removed if you work with arrayList
}
now create a method to remove from ActivityCard arrayList,
//call this method from cancelimage click in adapter class
public void removeFromList(int position){
cardList.remove(position); //this will remove from activity arraylist
}
At the end calculate your bill from existing cartList data.
*** You can also create adapter class object with cardList just changing your constructor. If you do this then just call removeFromList on cancelimage click and put,
adapter.notifyDataSetChanged();
after
cardList.remove(position);
it will refresh your listview also.
I am stuck when trying to get the value of selected checkbox listview values in my activity. I don't know how to do it, can any one suggest me a resolution to this issue?. Here is my code :
public class CandidateAdapter extends BaseAdapter implements OnCheckedChangeListener {
Context ctxt;
private LayoutInflater mInflater;
private ArrayList<CandidateModel> candidateList = new ArrayList<CandidateModel>();
private SparseBooleanArray mCheckStates;
public CandidateAdapter(Context context,ArrayList<CandidateModel> candidateList) {
mInflater = LayoutInflater.from(context);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.candidateList = candidateList;
mCheckStates = new SparseBooleanArray(candidateList.size());
}
#Override
public int getCount() {
return candidateList.size();
}
#Override
public CandidateModel getItem(int position) {
return candidateList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if(view == null){
view = mInflater.inflate(R.layout.table_row_candidate,null);
holder = new ViewHolder();
holder.candidateName = (TextView)view.findViewById(R.id.candidatenametxt);
holder.contactNumber = (TextView)view.findViewById(R.id.contactnumbertxt);
holder.email = (TextView)view.findViewById(R.id.emailtxt);
holder.contactCheckBox = (CheckBox)view.findViewById(R.id.contactchk);
view.setTag(holder);
}
else{
holder = (ViewHolder)view.getTag();
}
final CandidateModel cm = candidateList.get(position);
String contactNumber = cm.getContactNumber();
String candidateName = cm.getCandidateName();
String email = cm.getEmail();
if(!candidateName.equals("null") && candidateName!=null)
{
holder.candidateName.setText(cm.getCandidateName());
}
else{
holder.candidateName.setText("confidential");
}
if(!contactNumber.equals("null") && contactNumber!="" && contactNumber!=null && !contactNumber.equals("NA"))
{
holder.contactNumber.setText(cm.getContactNumber());
}
else{
holder.contactNumber.setVisibility(View.GONE);
}
if(!email.equals("null") && email!="" && email!=null && !email.equals("NA"))
{
holder.email.setText(cm.getEmail());
}
else{
holder.email.setVisibility(View.GONE);
}
holder.contactCheckBox.setTag(position);
holder.contactCheckBox.setChecked(mCheckStates.get(position, false));
holder.contactCheckBox.setOnCheckedChangeListener(this);
return view;
}
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) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
private class ViewHolder{
public TextView candidateName;
public TextView contactNumber;
public TextView email;
public CheckBox contactCheckBox;
}
}
///Code in my activity. Here I am actually adding the data to the adapter.
i want the data of selected checkboxes from the list view, how would I achieve it?.
dataarray = datajsonobject.getJSONArray("candidateList");
CandidateAdapter ca = null;
final ArrayList<CandidateModel> candidateList = new ArrayList<CandidateModel>();
for (int i = 0; i < dataarray.length(); i++) {
JSONObject dataobject = new JSONObject();
dataobject = dataarray.getJSONObject(i);
String candidate_name = dataobject.getString("fullName");
String url = dataobject.getString("url");
String email = dataobject.getString("email");
String contactNumber = dataobject.getString("contactNumber");
String candidateId = dataobject.getString("candidateId");
//CandidateModel cm = new CandidateModel(candidate_name,score);
CandidateModel cm = new CandidateModel();
cm.setJobId(job_id);
cm.setCandidateName(candidate_name);
cm.setResumeURL(url);
cm.setContactNumber(contactNumber);
cm.setEmail(email);
cm.setSelected(false);
cm.setCandidateId(candidateId);
candidateList.add(cm);
}
ca = new CandidateAdapter(Candidate.this,candidateList);
clist.setAdapter(ca);
clist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
CandidateModel selItem = candidateList.get(position);//get the selected RowItem from the list
Intent downloadResumeIntent = new Intent(
getApplicationContext(),
DownloadResume.class);
final String url = selItem.getResumeURL();
downloadResumeIntent.putExtra("url",url);
downloadResumeIntent.putExtra("jobId",job_id);
//downloadResumeIntent.putExtra("resumeUniqueID", uniqueResumeId);
startActivity(downloadResumeIntent);
Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();
}
});
Try to add one more boolean field in adapter item modle class CandidateModel :
public class CandidateModel {
private boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
Use isSelected field directly instead SparseBooleanArray or no need to SparseBooleanArray :
holder.contactCheckBox.setChecked(cm.isSelected());
holder.contactCheckBox..setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
cm.setSelected(isChecked);
}
});
Get checked all item :
public ArrayList<CandidateModel> getCheckAllItem(){
ArrayList<CandidateModel> candidateModelArrayList = new ArrayList<CandidateModel>();
for(CandidateModel model :candidateList){
if(model.isSelected()){
candidateModelArrayList.add(model);
}
}
return candidateModelArrayList
}
Check item at position :
public boolean isItemChecked (int index){
return candidateList.get(index).isSelected();
}
maintain arraylist globally in adapter maintain the check box state in the arraylist now you can get the arraylist in activity by calling the adaptet.arraylist;
If candidateList order remains the same all the time, then it is ok to use position as Tag value.
Try putting this method in your adapter and call it from activity.
public ArrayList<CandidateModel> GetCheckedItems(){
ArrayList<CandidateModel> checkedItems = new ArrayList<CandidateModel>();
for(int position = 0; position < candidateList.size(); position++){
if(mCheckStates.get(position))
checkedItems.add(candidateList.get(position));
}
return checkedItems;
}
Maybe i misunderstand your question :). Best regards.
Use the getter methods from class CandidateModel to retrieve all the data you want. eg: selItem.getResumeURL();
why do you need to know the checkbox's state if you know the position in the list that has been clicked? regardless of the answer you can use the already present method adapter.isChecked(position) to see if the checkbox is checked or not.
I have a listview. Each row of a listview contains two texts (name and aaddress), one button and one radio button. I am using CustomAdapter for that. Now I have a condition; if a text1(name) is equal to "Pramod", then I have to delete the radio button from entire rows.
What is happening exactly? The radio button of only that row is deleting.
I have to delete the radio button of all rows. How do I fix that?
Here is my code of customAdapter class:
public class CustomAdapter extends ArrayAdapter<Item> {
private final Context context;
// private boolean userSelected = false;
public static RadioButton mCurrentlyCheckedRB;
private final ArrayList<Item> itemList;
int selected_itemindex=-1;
static String abc="";
public CustomAdapter(Context context, ArrayList<Item> itemList) {
super(context, R.layout.row_item, itemList);
this.context = context;
this.itemList = itemList;
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
final View rowView = inflater.inflate(R.layout.row_item, parent, false);
// 3. Get the two text view from the rowView
Button btn = (Button) rowView.findViewById(R.id.button1);
TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
final TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
final RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);
// 4. Set the text for textView
tv1.setText(itemList.get(position).getName());
tv2.setText(itemList.get(position).getAddress());
// This is the code I am using to delete the radio button from the entire row
if (itemList.get(position).getName().toString().equals("Pramod")) {
radio.setVisibility(View.INVISIBLE);
}
if (itemList.get(position).isSelected()) {
radio.setChecked(true);
rowView.setBackgroundColor(Color.CYAN);
}
else {
rowView.setBackgroundColor(Color.WHITE);
radio.setChecked(false);
}
// The radio button I am using to select a particular row one at a time.
radio.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (itemList.get(position).isSelected()) {
}
else
{
int selected_previousval = -1;
for (int i=0; i<itemList.size(); i++) {
if (itemList.get(i).isSelected()) {
selected_previousval = i;
break;
}
}
String name2=itemList.get(position).getName();
String add2=itemList.get(position).getAddress();
if (selected_previousval==-1) {
itemList.remove(position);
itemList.add(position,new Item(name2, add2, true));
rowView.setBackgroundColor(Color.CYAN);
Toast.makeText(context, "selected a 3 row",2000).show();
radio.setChecked(true);
}
else {
itemList.remove(position);
itemList.add(position, new Item(name2, add2, true));
name2 = itemList.get(selected_previousval).getName();
add2 = itemList.get(selected_previousval).getAddress();
itemList.remove(selected_previousval);
itemList.add(selected_previousval, new Item(name2, add2, false));
MainActivity.lv.setAdapter(new CustomAdapter(context, itemList));
}
}
}
});
return rowView;
}
}
And this is my Arraylist class:
public class Item {
private String Name;
private String Address;
public boolean selected;
public boolean isSelected() {
return this.selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public Item(String Name, String Address,boolean selected) {
super();
this.Name = Name;
this.Address = Address;
this.selected=selected;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
UPDATE
Question: I want to remove the radiobutton from every row of listview if text=pramod.
Answer:
MainActivity class
ArrayList<Item> items;
CustomAdapter adapter;
items = new ArrayList<Item>();
ArrayList<String> temparray;
items.add(new Item("Pramod", "Ballia", false, 0));
items.add(new Item("Pankaj", "Mau", false, 1));
items.add(new Item("Pradeep", "Ranchi", false, 1));
items.add(new Item("Jitendra", "Varansi", false, 1));
items.add(new Item("Amresh", "Sonbhadra", false, 1));
items.add(new Item("Anil", "Sarnath", false, 1));
temparray = new ArrayList<String>();
for(int i=0; i<items.size(); i++)
{
temparray.add(items.get(i).getName());
}
if (temparray.contains("Pramod"))
{
Log.d("temparray", "contains Pramod");
Global.show=0;
}
else
{
Log.d("temparray", "not contain");
Global.show=1;
}
lv = (ListView)dp.findViewById(R.id.listView1);
Spinner sp = (Spinner) dp.findViewById(R.id.spinner1);
adapter = new CustomAdapter(this, items);
lv.setAdapter(adapter);
customAdapter.class
if (Global.show == 0)
{
radio.setVisibility(View.INVISIBLE);
}
else
{
radio.setVisibility(View.VISIBLE);
if (itemList.get(position).isSelected()) {
radio.setChecked(true);
rowView.setBackgroundColor(Color.CYAN);
}
else {
rowView.setBackgroundColor(Color.WHITE);
radio.setChecked(false);
}
}
Global.class
public static class Global {
Public static int show;
}