0

I have done a ListView and used CHOICE_MODE_MULTIPLE and simple_list_item_multiple_choice to allow the user to delete any item from the list (by unchecking the check box next to the item). This action should then only keep the checked items in the list.
My problem is that ListView does not appear in the same order as it is stored in the database (which is by the date the record is created), Therefore, when an item is unchecked, it does not get deleted from the database. Instead, some other record is deleted because because the items in the ListView do not appear in the same order as in the database. And the order of the items in the ListView get rearranged everytime the page is refreshed. How can I always have the items appear in the ListView in the same order as in the database? I want to point out that I used:

queryProducts.orderByAscending("createdAt");

But, for some reason, it does not order correctly.

This is my code:

package com.androidbegin.parselogintutorial;

import java.util.ArrayList;
import java.util.List;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class OrderListActivity extends Activity{
    @Override
    public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called");
    Intent setIntent = new                        Intent(getApplicationContext(),TabsActivity.class);
    startActivity(setIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.action_contactus:
        String [] to = new String[]{"Shatha-sa@hotmail.com", "",};
        String [] subject = new String[]{"Test Email - Smart Shopping System",};
        Intent emailActivity = new Intent(Intent.ACTION_SEND);
        //set up the recipient address
        emailActivity.putExtra(Intent.EXTRA_EMAIL, to );
        //set up the email subject
        emailActivity.putExtra(Intent.EXTRA_SUBJECT, subject);
        //you can specify cc addresses as well
        emailActivity.putExtra(Intent.EXTRA_CC, new String[]       {"nabuhossain@stu.kau.edu.sa"});
        emailActivity.setType("message/rfc822");
        startActivity(Intent.createChooser(emailActivity, "Select your Email Provider :"));
        return true;

    case R.id.action_logout:
        ParseUser.logOut();
        Intent intent = new Intent(this, FirstScreenActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        return true;


    default:
        return super.onOptionsItemSelected(item);
    }
}

ListView OrderItem;
Button button_confirmOrder;
private ProgressBar pb =null;
ParseObject u = new ParseObject("OrderItem");
int counter = 0;
int i;
int m = 0;


public void onStart() {
    super.onStart();
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orderslist);

    OrderItem = (ListView) findViewById(R.id.listV_displayorders);
    button_confirmOrder = (Button) findViewById(R.id.button_confirmorder);

    pb = (ProgressBar) findViewById(R.id.progressBar1);
    pb.setVisibility(View.VISIBLE);


    ParseQuery<ParseObject> queryId = new ParseQuery<ParseObject>("orders");
    queryId.whereEqualTo("Customer", ParseUser.getCurrentUser());       
    queryId.findInBackground(new FindCallback<ParseObject>() {
        public void done(final List<ParseObject> ParseObject,final ParseException e) {
            for (final ParseObject parseObject : ParseObject) {    
                final ParseQuery<ParseObject> queryProducts = new        ParseQuery("OrderItem");
                queryProducts.whereEqualTo("OrderID", parseObject.getObjectId());
                queryProducts.orderByAscending("createdAt");
                queryProducts.findInBackground(new FindCallback<ParseObject>() {
                    public void done(final List<ParseObject> ParseObject1, final ParseException e) {
                        if(ParseObject1.size() != 0){   
                            if (e == null) {
                                final ArrayAdapter<String>        listAdapter = new ArrayAdapter<String>(OrderListActivity.this,
                                        android.R.layout.simple_list_item_multiple_choice);

                                for (i = 0; i < ParseObject1.size(); i++) {
                                    u = ParseObject1.get(i);
                                    final int Quantity = u.getInt("Quantity");

                                    ParseQuery<ParseObject> query = new       ParseQuery<ParseObject>("products");
                                    query.whereEqualTo("productID", u.get("Barcode"));
                                    query.findInBackground(new   FindCallback<ParseObject>() {
                                        public void done(List<ParseObject> ParseObject, ParseException e) {
                                            if (e == null) {
                                                for (final ParseObject parseObject : ParseObject) {
                                                    final String ProductName = parseObject.get("productName").toString();
                                                    final int Price = parseObject.getInt("price");
                                                    int TotalPrice = Quantity * Price;
                                                    listAdapter.add(ProductName + "  " + TotalPrice + " $ " + "Q. " + Quantity);

                                                    counter++;
                                                    OrderItem.setAdapter(listAdapter);
                                                    OrderItem.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

                                                    for (m = 0; m <= ParseObject1.size(); m++) {
                                                        OrderItem.setItemChecked(m , true);
                                                    }
                                                    pb.setVisibility(View.INVISIBLE);

                                                }}}});


                                    button_confirmOrder.setOnClickListener(new OnClickListener() {  
                                        //SparseBooleanArray checkedPositions = OrderItem.getCheckedItemPositions();
                                        public void onClick(View v) {                                     

                                            int count=0;
                                            for(int j=0; j < ParseObject1.size() ; j++){
                                                if (OrderItem.isItemChecked(j) == false)
                                                    count++;
                                            }
                                            if(count == ParseObject1.size()){
                                                Toast.makeText(getApplicationContext(),"Please select a product",Toast.LENGTH_LONG).show();
                                                return;}
                                            else{
                                                SparseBooleanArray checkedPositions = OrderItem.getCheckedItemPositions();
                                                for(int j=0; j < counter ; j++) {
                                                    if(checkedPositions.get(j) == false) {
                                                        // UNCHECKED
                                                        final int n = j;
                                                        ParseQuery<ParseObject> queryId = new ParseQuery<ParseObject>("orders");
                                                        queryId.whereEqualTo("Customer", ParseUser.getCurrentUser());
                                                        queryId.findInBackground(new FindCallback<ParseObject>() {
                                                            public void done( List<ParseObject> ParseObject,  ParseException e) {
                                                                final ParseQuery<ParseObject> queryProducts = new ParseQuery("OrderItem");

                                                                queryProducts.whereEqualTo("OrderID", parseObject.getObjectId());
                                                                queryProducts.findInBackground(new FindCallback<ParseObject>() {
                                                                    public void done( List<ParseObject> ParseObject,  ParseException e) {
                                                                        int r = n;
                                                                        ParseObject.get(n).deleteInBackground();
                                                                        listAdapter.notifyDataSetChanged();
                                                                    }});
                                                            }});
                                                    }}
                                                Intent i = new Intent(getApplicationContext(),locationsActivity.class);
                                                startActivity(i);
                                                return;
                                            }}});
                                }}   
                        }else{  
                            final ArrayAdapter<String> listAdapter1 = new ArrayAdapter<String>(OrderListActivity.this,
                                    android.R.layout.simple_list_item_1);
                            listAdapter1.add("There are no results.");
                            OrderItem.setAdapter(listAdapter1);
                            pb.setVisibility(View.INVISIBLE);
                        }}});
            }}});
}}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66

1 Answers1

0

I think the problem does not lie in order of items.

To get selected items from multi-select ListView, you need to handle SparseBoolean array correctly. SparseBoolean array is basically an array of key-value pair,

so instead of

if(checkedPositions.get(j) == false) {

you should do

if(checkedPositions.get(checkedPositions.keyAt(j)) == false) {

How to get Selected items from Multi Select List View

Community
  • 1
  • 1
Kazuki
  • 1,462
  • 14
  • 34
  • I did that, but also it doesn't work :( , Items in the list are saved as a string from multi columns of parse.com, so I can't compare items of the list with objects in database unless I compared with places. – user3314733 Apr 26 '14 at 12:51