Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Arraylist<;类别>;传递到另一个活动以在该活动中添加对象,并返回第一个显示对象的活动_Java_Android_Arraylist - Fatal编程技术网

Java Arraylist<;类别>;传递到另一个活动以在该活动中添加对象,并返回第一个显示对象的活动

Java Arraylist<;类别>;传递到另一个活动以在该活动中添加对象,并返回第一个显示对象的活动,java,android,arraylist,Java,Android,Arraylist,在我的第一个活动中,我初始化了ArrayList,并且必须将其传递给自定义数组适配器,以便将对象添加到该数组列表中。这是我的第一个活动 public class OrderSummary extends AppCompatActivity { ArrayList<Order> orderItems= new ArrayList<>(); @Override protected void onCreate(Bundle savedInstance

在我的第一个活动中,我初始化了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;
    }
}
我使用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;
    }



}
import java.util.ArrayList;
导入静态satya_mydream.srkrnafflement.R.layout.word_项;
//自定义字适配器,用于显示和隐藏行
类WordAdapter扩展了ArrayAdapter{
private OrderSummary obj=新OrderSummary();
WordAdapter(活动上下文、ArrayList项){
超级(上下文,0,项);
}
@非空
@凌驾
公共视图getView(int位置,视图转换视图,@NonNull视图组父级){
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
word_项目,父项,false);
}
//获取位于列表中此位置的{@link AndroidFlavor}对象
最终单词currentPosition=getItem(位置);
//在list_item.xml布局中查找ID版本名称为的TextView
//在list_item.xml布局中查找ID版本号为的TextView
final TextView itemTextView=(TextView)listItemView.findViewById(R.id.listitems\u item);
//从当前AndroidFlavor对象获取版本号,然后
//在数字文本视图上设置此文本
如果(当前位置!=null){
itemTextView.setText(currentPosition.getmItemName());
}
TextView dollarTextView=(TextView)listItemView.findViewById(R.id.listitems\u doller);
//从当前AndroidFlavor对象获取版本号,然后
//在数字文本视图上设置此文本
如果(当前位置!=null){
dollarTextView.setText(currentPosition.getdollar());
}
最终按钮buttonName=(按钮)listItemView.findViewById(R.id.listitems\u addcart);
如果(当前位置!=null){
setText(currentPosition.getButtonnName());
buttonName.setOnClickListener(新视图.OnClickListener(){
私有整数索引;
`在此处输入代码`Context=getContext();
@凌驾
公共void onClick(视图v){
如果(currentPosition.getCount()==0){
订单o=新订单(currentPosition.getmPrice(),currentPosition.getmItemName(),currentPosition.updateCount());
obj.getorderItem().add(o);
index=obj.getorderItem().indexOf(o);
Toast.makeText(上下文“+currentPosition.getmItemName()+currentPosition.getCount()+”未添加“,”Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(上下文,“+currentPosition.getmItemName()+obj.getorderItem().get(索引).updateQuantity()+“未添加任何内容”,Toast.LENGTH_SHORT).show();
}
}
}
);
}
TextView价格TextView=(TextView)listItemView.findViewById(R.id.list\u items\u price);
如果(当前位置!=null)
priceTextView.setText(currentPosition.getmPrice()+);
/*在list_item.xml布局中使用ID list_item_图标查找ImageView
ImageView图标视图=(ImageView)listItemView.findViewById(R.id.list\u item\u图标);
从当前AndroidFlavor对象获取图像资源ID,然后
将图像设置为iconView
iconView.setImageResource(currentAndroidFlavor.getImageResourceId());
返回整个列表项布局(包含两个TextView和一个ImageView)
以便可以在ListView中显示它*/
返回listItemView;
}
}

您的问题是?不清楚您在问什么我在arraylist中添加的对象没有显示(我使用listview+customadapter来显示)arraylist项没有显示它是否显示了Toast.makeText(上下文,“+currentPosition.getmItemName()+currentPosition.getCount()+”没有添加“,Toast.LENGTH_SHORT).show()
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;
    }



}