Arraylist <class>传递给另一个活动,用于在该活动中添加对象,返回显示它们的第一个活动

时间:2017-07-26 06:58:17

标签: java android arraylist

我的第一个活动,我已经初始化了ArrayList,并且必须将其传递给自定义数组适配器,以便将对象添加到该数组列表中。这是我的第一个活动。

public class OrderSummary extends AppCompatActivity {

    ArrayList<Order> orderItems= new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lview);

        OrderAdapter orderArrayAdapter = new OrderAdapter(this,orderItems);
        ListView listView = (ListView) findViewById(R.id.satya);
        listView.setAdapter(orderArrayAdapter);

    }

    public ArrayList<Order> getorderItem() {
        return this.orderItems;
    }
}

我将对象添加到ArrayList<custom class>的第二个活动自定义适配器。这些元素会在button点击时添加。

    final Button buttonName = (Button) 

    listItemView.findViewById(R.id.listitems_addcart);
        if ( currentPosition != null ) {
            buttonName.setText(currentPosition.getButtonnName());
            buttonName.setOnClickListener(new View.OnClickListener() {
              private int index;
              Context context = getContext();

              @Override
              public void onClick(View v) {
                  if ( currentPosition.getCount() == 0 ) {

                     Order o = new Order(currentPosition.getmPrice(), currentPosition.getmItemName(), currentPosition.updateCount());
                      obj.getorderItem().add(o);
                      index = obj.getorderItem().indexOf(o);
                      Toast.makeText(context, "The" + currentPosition.getmItemName() + currentPosition.getCount() + "  no has been added", Toast.LENGTH_SHORT).show();
                  } else {
                     Toast.makeText(context, " " + currentPosition.getmItemName() + obj.getorderItem().get(index).updateQuantity() + "  no has been added", Toast.LENGTH_SHORT).show();

                  }
              }
          }
       );
       ...
    }

我使用getter方法将ArrayList传递给自定义适配器。 我的输出必须是当我打开第一个活动时,它必须显示使用按钮单击添加的数据我正确初始化了所有意图。 我的完整自定义适配器代码用于在listView中显示内容。

import java.util.ArrayList;

import static satya_mydream.srkrcanteen.R.layout.word_items;

//custom word adapter used to display and hanndle the row_iem

class WordAdapter extends ArrayAdapter<Word> {
    private  OrderSummary obj = new OrderSummary();

    WordAdapter(Activity context, ArrayList<Word> items) {

        super(context, 0, items);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if ( listItemView == null ) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    word_items, parent, false);
        }

        // Get the {@link AndroidFlavor} object located at this position in the list
        final Word currentPosition = getItem(position);

        // Find the TextView in the list_item.xml layout with the ID version_name

        // Find the TextView in the list_item.xml layout with the ID version_number
        final TextView itemTextView = (TextView) listItemView.findViewById(R.id.listitems_item);
        // Get the version number from the current AndroidFlavor object and
        // set this text on the number TextView


        if ( currentPosition != null ) {
            itemTextView.setText(currentPosition.getmItemName());
        }


        TextView dollarTextView = (TextView) listItemView.findViewById(R.id.listitems_dollor);
        // Get the version number from the current AndroidFlavor object and
        // set this text on the number TextView

        if ( currentPosition != null ) {
            dollarTextView.setText(currentPosition.getdollar());
        }


        final Button buttonName = (Button) listItemView.findViewById(R.id.listitems_addcart);
        if ( currentPosition != null ) {
            buttonName.setText(currentPosition.getButtonnName());
            buttonName.setOnClickListener(new View.OnClickListener() {
                                             private int index;


               `enter code here`                               Context context = getContext();

                                              @Override
                                              public void onClick(View v) {
                                                  if ( currentPosition.getCount() == 0 ) {

                                                     Order o = new Order(currentPosition.getmPrice(), currentPosition.getmItemName(), currentPosition.updateCount());
                                                      obj.getorderItem().add(o);
                                                      index = obj.getorderItem().indexOf(o);
                                                      Toast.makeText(context, "The" + currentPosition.getmItemName() + currentPosition.getCount() + "  no has been added", Toast.LENGTH_SHORT).show();
                                                  } else {
                                                     Toast.makeText(context, " " + currentPosition.getmItemName() + obj.getorderItem().get(index).updateQuantity() + "  no has been added", Toast.LENGTH_SHORT).show();

                                                  }
                                              }

                                          }

            );
        }
        TextView priceTextView = (TextView) listItemView.findViewById(R.id.list_items_price);




            if ( currentPosition != null )
                priceTextView.setText(currentPosition.getmPrice()+"");




        /*Find the ImageView in the list_item.xml layout with the ID list_item_icon
        ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
         Get the image resource ID from the current AndroidFlavor object and
         set the image to iconView
        iconView.setImageResource(currentAndroidFlavor.getImageResourceId());

         Return the whole list item layout (containing 2 TextViews and an ImageView)
         so that it can be shown in the ListView */
        return listItemView;
    }



}

0 个答案:

没有答案