在活动之间传递LinkedList

时间:2015-07-30 14:07:27

标签: java android

我正在尝试了解如何将LinkedList<PassedObject>MainActivity传递到SecondActivity。 所以我在每个活动中创建了一个listview来检查对象是否被传递。如果列表为空,则SecondActivity显示在textview中。我还制作了一个实现Parcelable的PassedObject类。问题是,当我从Intent获取List时,它为null。在下面的代码中,我使用ArrayList更改了LinkedList,因为我认为这是问题所在。我做错了什么?

public class MainActivity extends AppCompatActivity {
private ArrayList<PassedObject> mainList;
private ListView listView;
private CustomArrayAdapter adapter;
Button test;

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

    if(mainList == null){
        mainList = new ArrayList<PassedObject>();
    }

    mainList.add(new PassedObject("Passed"));
    mainList.add(new PassedObject("Passed"));
    mainList.add(new PassedObject("Passed"));
    mainList.add(new PassedObject("Passed"));
    listView = (ListView) findViewById(R.id.listViewMain);
    adapter = new CustomArrayAdapter(getBaseContext(),R.layout.item_list,mainList);
    listView.setAdapter(adapter);

    Button test = (Button) findViewById(R.id.button_id);

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

            Intent intent  =  new Intent(MainActivity.this, SecondActivity.class);
            intent.putParcelableArrayListExtra("list",mainList);
            startActivity(intent);

        }
    });
}
    public   class CustomArrayAdapter extends android.widget.ArrayAdapter<PassedObject>{
        Context _context;
        int layoutRestId;
        ArrayList<PassedObject> data;

        public CustomArrayAdapter(Context context, int resource, ArrayList<PassedObject> objects) {
            super(context, resource, objects);

            _context = context;
            layoutRestId = resource;
            data = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null) {
                LayoutInflater inflater = (LayoutInflater) _context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(layoutRestId, parent, false);
            }

            TextView tv = (TextView) convertView.findViewById(R.id.textViewItem);
            tv.setText(data.get(position).getName());
            return  convertView;
        }
    }
}

    public class SecondActivity extends AppCompatActivity {

    TextView tv ;

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

        tv = (TextView) findViewById(R.id.seocndListText);

        ArrayList<PassedObject> list =(ArrayList<PassedObject>) getIntent().getParcelableExtra("list");

        tv.setText(list.isEmpty()+"");
    }
}
public class PassedObject implements Parcelable{

    private String _name;

    PassedObject(String name){
        _name = name;
    }

    public String getName(){
        return _name;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(_name);
    }

    public PassedObject(Parcel in){

        _name = in.readString();
    }

    public static final Parcelable.Creator<PassedObject> CREATOR = new Parcelable.Creator<PassedObject>() {

        @Override
        public PassedObject createFromParcel(Parcel source) {
            return new PassedObject(source);
        }

        @Override
        public PassedObject[] newArray(int size) {
            return new PassedObject[size];
        }
    };


}

2 个答案:

答案 0 :(得分:1)

使用此

Bundle bdl = getIntent().getExtras();
mArraylist1 = bdl.getParcelableArrayList("your list name");

答案 1 :(得分:1)

更改以下行

ArrayList<PassedObject> list =(ArrayList<PassedObject>) getIntent().getParcelableExtra("list");

ArrayList<PassedObject> list = getIntent().getParcelableArrayListExtra("list");