将所选项从ListView传递到另一个Activity

时间:2017-03-14 09:46:18

标签: android-layout listview

MainActivity是我的类,用户从列表视图中选择项目。我使用setOnItemClickListener来获取所选项目,从这一点我不知道如何将它传递给下一个活动,我是否需要使用parcellable接口?还是有另一种方式只是通过意图。

公共类MainActivity扩展AppCompatActivity实现了Parcelable {

ArrayList<String> shoppingList;
ArrayAdapter adapter;
ListView lv;

选择了ArrayList;

protected MainActivity(Parcel in) {

}

public static final Creator<MainActivity> CREATOR = new Creator<MainActivity>() {
    @Override
    public MainActivity createFromParcel(Parcel in) {
        return new MainActivity(in);
    }

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

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

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

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

    // Example of a call to a native method

    shoppingList = new ArrayList<>();
    Collections.addAll(shoppingList, "Eggs", "Yogurt", "Milk", "Bananas", "Apples", "Tide with bleach", "Cascade","candy","Dental Floss","Toothpaste");
    shoppingList.addAll(Arrays.asList("ice cream","hand lotion"));


    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);

    lv = (ListView) findViewById(R.id.listview);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int Position, long id) {
            Toast.makeText(MainActivity.this, " You Selected  "+((TextView) view).getText() , Toast.LENGTH_SHORT).show();
            selected = new ArrayList();
            Collections.addAll(selected,((TextView) view).getText());

             System.out.println(((TextView) view).getText());
        }
    });

}

public void checkout(View view){
    Intent intent = new Intent(this,SelectedItemsControl.class);

    startActivity(intent);
}

1 个答案:

答案 0 :(得分:0)

  public void checkout(View view){
    Intent intent = new Intent(this,SelectedItemsControl.class);
    intent.putExtra("selected_list", selectedList)
    startActivity(intent);
   }

在接收活动中,您需要执行以下操作:

Intent i = getIntent();  
ArrayList<String> selectedList = i.getStringArrayListExtra("selected_list");

在列表中添加项目就像休息一样

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    shoppingList = new ArrayList<>();
    Collections.addAll(shoppingList, "Eggs", "Yogurt", "Milk", "Bananas", "Apples", "Tide with bleach", "Cascade","candy","Dental Floss","Toothpaste");
    shoppingList.addAll(Arrays.asList("ice cream","hand lotion"));
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);

    lv = (ListView) findViewById(R.id.listview);
    lv.setAdapter(adapter);
    selectedList = new ArrayList();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int Position, long id) {
           selectedList.add(adapter.getItem(position));]
        }
    });

}