简体   繁体   中英

How to send Items from one activity to another on item checked

I want to send all selected items from my first activity to another activity on item checked. There are three classes in my app First is Appetizer class,second class is ItemListBaseAdapter class and third class is Manage class I want to send the checked item from Appetizers class to Manage class.If you need to know any other information let me know. This is my Appetizer class

public class Appetizers extends Activity{

Button manage;
public static final String[] titles = new String[] { "Aloo Chat",
        "Paneer Tikka", "Spring Roll", "Paneer Chilly", "Veg Roll","Rice balls","Pasta", };
public static final String totalPrice="0";
public static final int[] quantities = new int[]{ 0 };
public static final String[] price={"20","10","30","40","50","60","10"};

public static final Integer[] images = { R.drawable.a39,
        R.drawable.a40, R.drawable.a41, R.drawable.a42,R.drawable.a74,R.drawable.a75,R.drawable.a76 };
public ItemListBaseAdapter adapter;
ListView listView;
public List<RowItem> rowItems;
Button add,sub;
public static Appetizers apti;
public  RowItem item;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    apti=this;
//    LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.button1);
//    layout.addView(new Button(this));
   manage=(Button) findViewById(R.id.manage);
     rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
         item = new RowItem(images[i], titles[i], quantities[0],price[i],totalPrice);
        rowItems.add(item);
        Log.e("item in appetizer", ""+item);
        manage.setOnClickListener(new View.OnClickListener() {

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

                // intt.putParcelable("myclass",rowItems);
                // intt.putStringArrayListExtra("key", rowItems);
                Intent i=new Intent(Appetizers.this,Manage.class);
                startActivity(i);



            }
        });

    }


    listView = (ListView) findViewById(R.id.list);
    ItemListBaseAdapter adapter = new ItemListBaseAdapter(this,
            R.layout.item_details_view, rowItems);
    Log.e("Row items in appetizer",""+rowItems);
    listView.setAdapter(adapter);


}
}

This is my ItemListBaseAdapter class

class ItemListBaseAdapter extends ArrayAdapter<RowItem> {
     public static ItemListBaseAdapter adapter;
    Context context;
    int getPosition;
    int rowItem;
    String check1;

    public List<RowItem> rowItems;

    /*
     * here we must override the constructor for ArrayAdapter the only variable
     * we care about now is ArrayList<RowItem> objects, because it is the list
     * of objects we want to display.
     */

    public ItemListBaseAdapter(Context context, int resourceId,
            List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
        adapter=this;
    }

    /* private view holder class */
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle,txtPrice,ttprice;
        TextView txtDesc;
        Button add, sub,manage;
        CheckBox check;

    }

    /*
     * we are overriding the getView method here - this is what defines how each
     * list item will look.
     */
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            convertView = mInflater.inflate(R.layout.item_details_view, null);
        }

        /*
         * Recall that the variable position is sent in as an argument to this
         * method. The variable simply refers to the position of the current
         * object in the list. (The ArrayAdapter iterates through the list we
         * sent it)
         * 
         * Therefore, rowItem refers to the current RowItem object.
         */
        RowItem rowItem = getItem(position);
        if (rowItem != null) {
            holder = new ViewHolder();
            // This is how you obtain a reference to the TextViews
            // Images,checkBox,Buttons.
            // These TextViews are created in the XML files we defined.

            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
            holder.ttprice=(TextView) convertView.findViewById(R.id.tPrice);

            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            holder.check = (CheckBox) convertView.findViewById(R.id.checkBox1);
            holder.add = (Button) convertView.findViewById(R.id.button1);
            holder.sub = (Button) convertView.findViewById(R.id.button2);
        //  holder.manage = (Button) convertView.findViewById(R.id.manage);






//          holder.check.setOnClickListener( new View.OnClickListener() {  
//               public void onClick(View v) {  
//                CheckBox cb = (CheckBox) v ;  
//                RowItem rowItem = (RowItem) cb.getTag();  
//                Toast.makeText(context,"Clicked on Checkbox: " + cb.getText() +" is " + cb.isChecked(), Toast.LENGTH_LONG).show();
//                rowItem.setSelected(cb.isChecked());
//               }  
//              });  

            /*
             * holder.check.setOnClickListener( new View.OnClickListener() {
             * public void onClick(View v) { CheckBox cb = (CheckBox) v ;
             * RowItem rowItem = (RowItem) cb.getTag(); Toast.makeText(context,
             * "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(),
             * Toast.LENGTH_LONG).show(); rowItem.setSelected(cb.isChecked()); }
             * });
             */
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }
            /*holder = (ViewHolder) convertView.getTag();

         *rowItem = rowItems.get(position); holder.txtTitle.setText(" (" +
         *rowItem.getTitle() + ")"); holder.check.setText(rowItem.getName());
         *holder.check.setChecked(rowItem.isSelected());
         * holder.check.setTag(rowItem);
         */

        holder.txtDesc.setText(rowItem.getDesc());
        holder.txtTitle.setText(rowItem.getTitle());
        holder.txtPrice.setText(rowItem.getPrice());
        Log.e("title",""+rowItem.getTitle());
        Log.d("description",""+rowItem.getDesc());
        Log.e("Price",""+rowItem.getPrice());


        holder.imageView.setImageResource(rowItem.getImageId());
        holder.add.setTag(position);
        holder.sub.setTag(position);
        //holder.manage.setTag(position);
        holder.check.setTag(position);

        holder.add.setEnabled(false);
        holder.sub.setEnabled(false);

        final RowItem finalRowItem = rowItem;
        final ViewHolder viewHolderFinal = holder;

        holder.add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                int quantity = finalRowItem.getQuantity();


                if (quantity >= 0) {
                    finalRowItem.setQuantity(quantity + 1); // update it by adding 1
                    quantity=finalRowItem.getQuantity();
                    if (quantity>=1) {
                        String p=finalRowItem.getPrice();
                        int x=Integer.parseInt(p);
                        int y=quantity*x;
                        String f=Integer.toString(y);
                        finalRowItem.setTotalPrice(f);
                        viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());

                    }


                    viewHolderFinal.add.setEnabled(true);
                    viewHolderFinal.sub.setEnabled(true);

                } else {
                    viewHolderFinal.add.setEnabled(false);
                    viewHolderFinal.sub.setEnabled(false);
                }
                // get the quantity for this row item

                viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());

                viewHolderFinal.add.setEnabled(true);
                viewHolderFinal.sub.setEnabled(true);
                String check =finalRowItem.getDesc()+finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getTotalPrice();
                  Toast.makeText(context,"Clicked on Checkbox check1: " +finalRowItem.getDesc()+finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getTotalPrice(), Toast.LENGTH_LONG).show();
                   Intent intt=new Intent(context,Manage.class);
                   intt.putExtra("allItem", check);
                  Log.d("final description in add", finalRowItem.getDesc());
                // set the new description (that uses the updated qunatity)
            }
        });

        holder.sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int quantity = finalRowItem.getQuantity();
                if (quantity == 1) {
                    finalRowItem.setQuantity(quantity - 1);
                    Toast.makeText(context, "Item is unchecked", Toast.LENGTH_LONG).show();
                    viewHolderFinal.check.setChecked(false);
                    viewHolderFinal.add.setEnabled(false);
                    viewHolderFinal.sub.setEnabled(false);

                } else {

                    viewHolderFinal.add.setEnabled(true);
                    viewHolderFinal.sub.setEnabled(true);
                }

                // get the quantity for this row item
                finalRowItem.setQuantity(quantity - 1);

                quantity=finalRowItem.getQuantity();

                    String p=finalRowItem.getPrice();
                    int x=Integer.parseInt(p);
                    int y=quantity*x;
                    String f=Integer.toString(y);
                    finalRowItem.setTotalPrice(f);
                    viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
                    //Toast.makeText(context, "898798",0).show();


                // update it by subtracting 1
                viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
                Log.d("final description in sub", finalRowItem.getDesc());
                // set the new description (that uses the updated qunatity)

            }
        });
        // the view must be returned to our activity
        //Toast.makeText(context,"Quantity selected"+finalRowItem.getTotalPrice() ,Toast.LENGTH_SHORT).show();
        holder.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CheckBox cb = (CheckBox) v;

                if (cb.isChecked()) {
                    Log.d("checkbox checked", "checkbox checked");
                    // RowItem finalRowItem = (RowItem) cb.getTag();  
                      Toast.makeText(context,"Clicked on Checkbox: " +finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getDesc()+" is " + cb.isChecked(), Toast.LENGTH_LONG).show();



                   // finalRowItem.setSelected(cb.isChecked());
                    viewHolderFinal.add.setEnabled(true);
                    viewHolderFinal.sub.setEnabled(true);


                    finalRowItem.quantity =finalRowItem.getQuantity(); 

                    finalRowItem.quantity = 1;
                    viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
                    if (finalRowItem.quantity>=1) {
                        Log.e("checkbox checked-2", "checkbox checked-2");
                        String p=finalRowItem.getPrice();
                        int x=Integer.parseInt(p);
                        int y=finalRowItem.quantity*x;
                        String f=Integer.toString(y);
                        finalRowItem.setTotalPrice(f);
                        viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
                        //Toast.makeText(context, "898798",0).show();
                          Toast.makeText(context,"Clicked on Checkbox check1: " +finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getTotalPrice()+" is " + cb.isChecked(), Toast.LENGTH_LONG).show();

                    }
                } else {

                    if (finalRowItem.quantity>=1) {
                        Log.d("checkbox checked-3", "checkbox checked-3");
                        String p=finalRowItem.getPrice();
                        int x=Integer.parseInt(p);
                        int y=finalRowItem.quantity*x;
                        y=0;
                        String f=Integer.toString(y);
                        finalRowItem.setTotalPrice(f);
                        viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
                        //Toast.makeText(context, "898798",0).show();
                    }
                    finalRowItem.quantity = finalRowItem.getQuantity();
                    if (finalRowItem.quantity > 0) {
                        finalRowItem.quantity = 0;
                    }
                    viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
                      Toast.makeText(context,"Clicked on Checkbox check2: " +finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getDesc()+finalRowItem.getQuantity()+" is " + cb.isChecked(), Toast.LENGTH_LONG).show();
                    viewHolderFinal.add.setEnabled(false);
                    viewHolderFinal.sub.setEnabled(false);

                }

            }
        });





        return convertView;
    }



}

There are two easier ways by which you can send the item from one class to another:

1. Save the items in SharedPrefernces and access them in another class.

2. Send them as part of intent using putExtra() api.

Write your class which implements Parcelable . Pass its instance over the intent.

See this for usage.

You could implement Parcelable interface on your item.

First of all, your RowItem should implement Parcelable interface (ex: http://developer.android.com/reference/android/os/Parcelable.html )

Then you should use not List but ArrayList, and put this object in intent:

Intent i = ...;
i.putParcelableArrayListExtra(rowItems);

In your MainActivity or the activity where you the values , which you want to send to another activity, write below code.

SharedPreferences app_preferences = PreferenceManager
                        .getDefaultSharedPreferences(MainActivity.this);
                SharedPreferences.Editor editor = app_preferences.edit();

                editor.putString("Username", YOUR STRING);
                editor.putString("EMAIL", YOUR STRING);
                editor.commit();

its a key value pair, where username and EMAIL are key and value will be your string which you have to send to other activity.

Then in other activity where you want to retrieve the data add below code.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String text = preferences.getString("Username", "null");
        String Email1=preferences.getString("EMAIL", "null");
        String Email=Email1.toString();

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM