简体   繁体   English

如何将一项活动中的项目从一项活动发送到另一项

[英]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. 我的应用程序中有三个类,第一类是Appetizer类,第二类是ItemListBaseAdapter类,第三类是Manage类,我想将选中的项目从Appetizers类发送到Manage类。如果您需要了解其他信息,请告诉我。 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 这是我的ItemListBaseAdapter类

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 . 编写实现Parcelable的类。 Pass its instance over the intent. 将其实例传递给该意图。

See this for usage. 以供使用。

You could implement Parcelable interface on your item. 您可以在商品上实现Parcelable接口。

First of all, your RowItem should implement Parcelable interface (ex: http://developer.android.com/reference/android/os/Parcelable.html ) 首先,您的RowItem应该实现Parcelable接口(例如: http : //developer.android.com/reference/android/os/Parcelable.html

Then you should use not List but ArrayList, and put this object in intent: 然后,您应该不使用List而是使用ArrayList,并将此对象放入意图中:

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. 在您要发送到另一个活动的value的MainActivity或活动中,编写以下代码。

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. 它是一个键值对,其中用户名和EMAIL是键,值将是您必须发送给其他活动的字符串。

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要将 RecyclerView 中选中的复选框从一个活动发送到另一个活动 - Need to send the checked checkbox in RecyclerView from one activity to another 显示其他活动中已检查项目的数量 - Display the number of checked items from another activity 如何在Android中将哈希映射数组从一个活动发送到另一个活动 - How to send array of Hashmap from one activity to another activity in android 如何将值从json列表视图中的选定项目发送到另一个活动? - How to send values from a selected item on a json listview to another activity? 如何在Android中将数据从一个活动发送到另一个活动? - how to send data from one activity to another in android? 从一种活动切换到另一种活动后如何发送数据? - How to send data after switching from one activity to another? Android:如何将接口从一个活动发送到另一个活动 - Android: How to send interface from one activity to another 如何将数据从一个活动发送到另一个活动。 (蓝牙) - How to send data from one activity to another. (Bluetooth) 如何在android中将Calendar对象从一个活动发送到另一个活动? - How to send a Calendar object from one activity to another in android? 如何在Android中将一个活动的对象列表发送到另一个活动? - How to send a list of objects from one activity to another in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM