checkbox select all delete issue - android

i have checkboxes in custom list view and i have two seperate buttons one to select all and another to delete but i have big issue in it if i select one checkbox and select delete it works.but if i select all and delete it delete few of the data in lists ,for example i have 50 sms in list view once i select all and delete it delete few sms and again i press delete it delete another few like this..suggest some solutions
sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <sms.size() ; i++) {
if(sms.get(i).getChecked() == false ) {
sms.get(i).setChecked(true);
}else if(sms.get(i).getChecked()==true) {
sms.get(i).setChecked(false);
}
}
((datalist)mlistView.getAdapter()).notifyDataSetChanged();
}
});
del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0; i<sms.size(); i++){
if(sms.get(i).getChecked() == true){
sms.remove(i);
}
}
((datalist)mlistView.getAdapter()).notifyDataSetChanged();
}

I guess the problem arises when you remove the item from the ArrayList in the for loop there. It's size gets reduced as a result and this can induce undesirable side effects.
Try iterating your sms ArrayList in reverse order like this:
for (int i = sms.size()-1; i >= 0; i--) {
if(sms.get(i).getChecked() == true){
sms.remove(i);
}
}
This way reduction in the size of sms ArrayList won't effect the loop.

I think problem is upper for loop. In else if statement you are unchecking the checkboxes. try below code instead,
sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <sms.size() ; i++) {
if(sms.get(i).getChecked() == false ) {
sms.get(i).setChecked(true);
}else if(sms.get(i).getChecked()==true) {
sms.get(i).setChecked(true);
}
}
((datalist)mlistView.getAdapter()).notifyDataSetChanged();
}
});

Related

Selected Item has to be added to cart in android

Already I have some static data in expanded list view and from that list if any customer click on that multiple items they has to be selected and it has to be added into cart.So for that please give me some suggestions and if u have any implemented code please post here.
Thanks in advance.
Assume there is two button for add and remove item on cart screen so both have click event on adapter class below is just sample example
holder.imgAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCartDetail mCartDetail;
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
int finalMmaxBuy = 0;
if (!mCartDetail.categoryProductDetail.max_buy_qty.equalsIgnoreCase(" ")) {
finalMmaxBuy = Integer.parseInt(mCartDetail.categoryProductDetail.max_buy_qty);
}
if (mCartDetail.addQuantity < finalMmaxBuy) {
mCartDetail.addQuantity++;
}
} else {
mCartDetail = new mCartDetail();
mCartDetail.categoryProductDetail = mcategoryProductDetail.productdetails.get(0);
mCartDetail.addQuantity = 1;
Utility.mCartList.put(mcategoryProductDetail.productdetails.get(0).psid, mCartDetail);
}
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
}
});
holder.imgRemoveItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
mCartDetail.addQuantity--;
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
if (mCartDetail.addQuantity == 0) {
Utility.mCartList.remove(mCartDetail.categoryProductDetail.psid);
notifyDataSetChanged();
}
}
});
and below is my model class and hashmap for data storing and send to server
public static HashMap<String, CartDetail> mCartList;
public CartDetail mCartDetail;
Hope this concept will help you to implement in your scenario

Is there a way to find the endpoint of a string array list?

Ok I have an sqlite database that is collecting data from the user- this is information is then sent to a string array and displayed on screen with the following code-
Heres the array and the int var
List<String> array = new ArrayList<String>();
int currentPos = 0;
Heres the next button code
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentPos ++;
if (currentPos > 30) currentPos = 0;
textView.setText(array.get(currentPos ));
}
});
and the previous button code
prevBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentPos --;
if (currentPos < 0) currentPos = 30;
textView.setText(array.get(currentPos ));
}
});
}
Here's how this code works the two button allow the user to go back and forth in the array by changing the int using an increment while changing the current outputted text- this means you can go from 0 to 30 without a crash or vice versa.
this works fine when you have a set number amount of data in the array but it obviously crashes in this case when its impossible to hard code the amount of data that the user is imputing to sqlite and then the array.
The question is-is there a way around this i.e some code you could replace 30 with that would point to the end of the array?
arraylist.size() returns the size of the array or in your case array.size() will return the size of the array.
Yes, you are almost there:
your array has a method wich returns it own size:
List<String> array = new ArrayList<String>();
int currentPos = 0;
...
currentPos = array.size();
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentPos ++;
if (currentPos > 30) currentPos = 0;
textView.setText(array.get(currentPos ))
}
});

deleting multiple items from listview

I am trying to delete several items from my listview but it just deletes one element(even though i select more than one). The listview implements android.R.layout.simple_list_item_multiple_choice which has delListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE). I have a button where in the onClick method i would first like to check if there are no items selected if so prompt the user with some message. Else if there are some items selected delete them. This is what i have so far:
public void onClick(View v) {
// TODO Auto-generated method stub
selectItems();
Intent intent = new Intent(); ...
}
selectItems function:
private boolean selectItem()
{
String message = "Please select an item!";
for (int i = 0; i < array.size(); i++)
{
if(array.valueAt(i))
{
String item = delListView.getAdapter().getItem(array.keyAt(i)).toString();
Log.i("my_app", item + " selected");
deleteAdapter.remove(item);
deleteAdapter.notifyDataSetChanged();
return false; //item found
}
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
return true; //no item found
}
Thank you for your help!
I have managed to solve my problem. My solution was:
private void deleteItems() {
// TODO Auto-generated method stub
for (int i = (array.size()) - 1; i >= 0; i--)
{
if(array.valueAt(i))
{
String selected = deleteAdapter.getItem(array.keyAt(i));
Log.i("my_app", selected + " will be selected");
deleteAdapter.remove(selectat);
}
}
Intent intent = new Intent();
intent.putStringArrayListExtra(BUNDLE_DELETE, deleteList);
setResult(REQUEST_CODE_DELETE_STRING, intent);
getInstance().finish();
}
private boolean selectItem()
{
String message = "Please select an item!";
for (int i = 0; i < array.size(); i++)
{
if(array.valueAt(i))
{
String item = delListView.getAdapter().getItem(array.keyAt(i)).toString();
Log.i("my_app", item + " selected");
deleteItems();
return false;
}
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
return true;
}
I made a separate method for selecting the items and deleting them. Then i simply called the delete method in the selectItem(). In the onClick method i called the selectItem() method.
Thank you all for your help! :)
Notifydata set changed after all your items have been removed from the data map.
1) In onClick() call ListView.getCheckedItemPositions() to get the positions of the selected items in the list or ListView. getCheckedItemIds() to get the Id's of the selected items.
2) Then delete them.
3) Then call notifyDataSetChanged() on the Adapter to cause the list to be refreshed.
try something like this:
see an example :(Portuguese Link) http://nglauber.blogspot.com.br/2013/07/listview-com-selecao-multipla-actionbar.html
private boolean selectItem() {
String item = delListView.getAdapter().getItem(array.keyAt(i)).toString();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = checked.size()-1; i >= 0; i--) {
if (checked.valueAt(i)) {
String item = delListView.getAdapter().getItem(checked.valueAt(i)).toString();
deleteAdapter.remove(item);
}
}
deleteAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
return true; //no item found
}
Edited:
I first want to verify(when
pressing a button) if there are no items selected.
mButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick( final View v ) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
if ( checked.size() == 0 ) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
} );

Way to get the no of Imageview that is clicked

I have a lot of imageview . Lets say I have a 50 ImageViews . These will be in one screen . Horizontal and vertical scrolling will be available .
for(int j = 0; j < numCol; j++)
{
imageView[i*5+j] = new ImageView(this);
imageView[i*5+j].setImageResource(R.drawable.icon);
imageView[i*5+j].setMaxHeight(80);
imageView[i*5+j].setMinimumHeight(80);
imageView[i*5+j].setMaxWidth(90);
imageView[i*5+j].setMinimumWidth(90);
tblRow.addView(imageView[i*5+j],j);
imageView[i*5+j].setOnClickListener(this);
}
Suppose an user will click on imageview[25] . Is there any direct way to catch that imageview[25] is clicked ? Or I will have to iterate over all imageview .
imageView[i*5+j].setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
someMethod((ImageView) v);
}
});
private someMethod(ImageView currentView) {
}
You can use
imageView[i*5+j].setTag(new Integer(j));
and then in the OnClockListener
public void onClick(View v) {
Integer i = (Integer)v.getTag();
}

Android button 2 clicks

How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}

Categories

Resources