如何在ListView中将项目排序为Parse.com中的项目顺序(android)

时间:2014-04-25 14:06:52

标签: java android eclipse android-listview parse-platform

我已经完成了ListView并使用了CHOICE_MODE_MULTIPLE和simple_list_item_multiple_choice来允许用户从列表中删除任何项目(通过取消选中该项旁边的复选框)。然后,此操作应仅将选中的项目保留在列表中 我的问题是ListView的出现顺序与它在数据库中存储的顺序不同(也就是创建记录的日期), 因此,当取消选中某个项目时,它不会从数据库中删除。而是删除了一些其他记录,因为ListView中的项目的顺序与数据库中的顺序不同。每次刷新页面时,ListView中的项目顺序都会重新排列。 如何始终以与数据库中相同的顺序将项目显示在ListView中? 我想指出我用过:

queryProducts.orderByAscending( “createdAt”);

但是,由于某种原因,它没有正确排序。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

我认为问题不在于物品的顺序。

要从多选ListView中获取所选项,您需要正确处理SparseBoolean数组。 SparseBoolean数组基本上是一个键值对数组,

所以而不是

  

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

你应该做

  

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

How to get Selected items from Multi Select List View