如何将数组列表的所有数据发送到另一个活动?

时间:2016-04-25 10:49:42

标签: android listview android-bundle android-parser

Daily Screen :-

public class DailyScreen extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ImageView add, edit;
    MyCustomAdapter adapter1;
    Button ok;
    Button next2;

    final Context context = this;
    ListView listView;
    private ArrayAdapter<String> adapter;
    List<Milk> items=new ArrayList();

    public List<Milk> getItems() {
        return items;
    }

    public void setItems(List<Milk> items) {
        this.items = items;
    }

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


        add = (ImageView) findViewById(R.id.add);
        edit = (ImageView) findViewById(R.id.edit);
        listView = (ListView) findViewById(R.id.list);
        next2 = (Button) findViewById(R.id.next2);

        Milk milkDefault=new Milk("Toned Milk",0);
        items.add(milkDefault);
        //this.setArrayList((ArrayList<String>) Arrays.asList(items));
        adapter1 = new MyCustomAdapter(items, this);

        listView.setAdapter(adapter1);
        listView.setOnItemClickListener(this);
        registerForContextMenu(listView);// to set context menu in list view
    }
    @Override
    protected void onResume() {
        super.onResume();
        next2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DailyScreen.this, SupplierActivity.class);
                intent.putExtra("LIST_ITEMS",List,items);//

                startActivity(intent);
            }
        });


        add.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(DailyScreen.this);
                dialog.setTitle("Enter new Milk");
                dialog.setContentView(R.layout.dialog_box);
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();

                final EditText editText = (EditText)  dialog.findViewById(R.id.pro);
                String data = editText.getText().toString();
                //button initialization
                Button ok = (Button) dialog.findViewById(R.id.ok);

                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String data = editText.getText().toString();
                        if (!data.isEmpty()) {
                            items.add(new Milk(data,0));
                            adapter1.notifyDataSetChanged();
                            Toast.makeText(getApplicationContext(), "product name is :" + data, Toast.LENGTH_LONG).show();
                            dialog.cancel();
                        }
                        else{
                            Toast.makeText(DailyScreen.this, "Please enter the data", Toast.LENGTH_SHORT).show();
                        }

                    }
                });

            }

        });
    }

MyCustomAdapter类

public class MyCustomAdapter extends BaseAdapter implements ListAdapter {

    private List<Milk> list = new ArrayList<Milk>();
    private Context context;

    public MyCustomAdapter(List<Milk> list, Context context) {
        this.list = list;
        this.context = context;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom_layout, null);
        }
        TextView name = (TextView) view.findViewById(R.id.text);
        name.setText(list.get(position).getNsme());
        final TextView milkCount = (TextView) view.findViewById(R.id.milkcount);
        milkCount.setText(""+list.get(position).getAmount());
        ImageView increment = (ImageView) view.findViewById(R.id.add);
        ImageView decrement = (ImageView) view.findViewById(R.id.sub_item);
        increment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = Integer.parseInt(milkCount.getText().toString().trim());
                a = a + 1;
                milkCount.setText("" + a);
                list.get(position).setAmount(a);
            }
        });
        decrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = Integer.parseInt(milkCount.getText().toString().trim());
                if (a == 0) {
                    a = 0;
                } else {
                    a = a - 1;
                }
                milkCount.setText("" + a);
                list.get(position).setAmount(a);
            }
        });
        return view;
    }

牛奶班

public class Milk {

    private String nsme ;
    private int amount ;

    public Milk(String nsme, int amount) {
        this.nsme = nsme;
        this.amount = amount;
    }

    public String getNsme() {
        return nsme;
    }

    public void setNsme(String nsme) {
        this.nsme = nsme;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
  

我已经尝试了所有的事情将列表视图的所有数据发送到另一个活动请建议我怎么办?

4 个答案:

答案 0 :(得分:0)

试试这个。可能会有所帮助。

通过演员将arraylist发送到活动。

intent.putStringArrayListExtra("ARRAY_LIST", arrayList);

在活动中检索arraylist。

arrayList= i.getStringArrayListExtra("ARRAY_LIST");

如果你必须发送除字符串以外的arraylist。

intent.putParcelableArrayListExtra("ARRAY_LIST", (ArrayList<Milk>) milkArrayList);

牛奶类必须是可包裹的

答案 1 :(得分:0)

将列表设为静态,然后您可以在任何活动中访问它。确保你初始化列表

答案 2 :(得分:0)

milk class extends serializable firstly
then u do
make object of milk class in mainactivity class
Milk milk=new Milk(this);
Intent i=new Intent(getApplicationContext(),Update.class);
            i.putExtra("emnumber",(Serializable)milk);
            startActivity(i);
then u can pick the value in update class
if(getIntent().getSerializableExtra("emnumber")!=null) {
            con = (ArrayList<Contact>) getIntent().getSerializableExtra("emnumber");
            email_mobile = contactList.get(0).nsme;
            pass__word = contactList.get(0).amount;

答案 3 :(得分:0)

你可以实现:

    public class Milk implements Parcelable{

...... }

将arraylist发送为:

    intent.putExtra("files", "your arraylist");

获得arraylist作为:

     Intent inIntent = getIntent();
    list=inIntent.getParcelableArrayListExtra("files");