如何将项目的位置存储在列表视图中,并选中捆绑包中的复选框?

时间:2018-10-18 04:59:00

标签: android listview bundle

我有一个列表视图,每一行都有一个复选框。我有一个按钮来显示单击列表视图时选中的项目。我的问题是,当方向更改时,所有选择都将消失。我知道我需要将项目的位置存储在listview中,并已将复选框选中到捆绑包中,但是我不知道该怎么做。有人可以帮我吗?

这是我的主要活动。

public class MainActivity extends AppCompatActivity {
ListView mainActivity;
// creating arraylist of MyItem type to set to adapter
ArrayList<MyItem> myItems = new ArrayList<>();
Button button;
MyCustomAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainActivity = (ListView) findViewById(R.id.mainactivitylistview);
    myItems.add(new MyItem("Greece", "Athens",R.drawable.amsterdam_icon,false));
    myItems.add(new MyItem("Emirate of Dubai","Dubai",R.drawable.dubai_icon,false));
    myItems.add(new MyItem("Netherlands","Amsterdam",R.drawable.amsterdam_icon,false));
    myItems.add(new MyItem("England","London",R.drawable.london_icon,false));
    myItems.add(new MyItem("Egypt","Giza",R.drawable.egypt_icon,false));
    myItems.add(new MyItem("France","Paris",R.drawable.paris_icon,false));
    myItems.add(new MyItem("Russia","Moscow",R.drawable.moscow_icon,false));
    myItems.add(new MyItem("Italy","Florence",R.drawable.florence_icon,false));
    myItems.add(new MyItem("Switzerland","Zurich",R.drawable.zurich_icon,false));
    myItems.add(new MyItem("Japan","Tokyo",R.drawable.tokyo_icon,false));
    //Creating Adapter object for setting to list
    adapter = new MyCustomAdapter(myItems, MainActivity.this);
    mainActivity.setAdapter(adapter);

    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Your Wishlist");
            builder.setMessage(adapter.getWishlist().toString().replace("[","").replace("]","").replace(",",""));
            builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });
}}

这是我的适配器。

public class MyCustomAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<MyItem> myList= new ArrayList<>();
private ArrayList<String> wishList = new ArrayList<>();
private ArrayList<Integer> positionList = new ArrayList<>();

public MyCustomAdapter(ArrayList<MyItem> itemArray,Context mContext) {
    super();
    this.mContext = mContext;
    myList=itemArray;
}
@Override
public int getCount() {
    return myList.size();
}
@Override
public String getItem(int position) {
    return myList.get(position).toString();
}

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

public ArrayList<String> getWishlist() {
    return wishList;
}

public ArrayList<Integer> getPositionList() {
    return positionList;
}

public class ViewHolder {
    public TextView name_country;
    public TextView name_city;
    public CheckBox checkBox;
    public ImageView imageView;
}

@Override
public View getView(final int position, View convertView,
                    final ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder view = null;
    LayoutInflater inflator = ((Activity) mContext).getLayoutInflater();
    if (view == null) {
        view = new ViewHolder();
        convertView = inflator.inflate(  R.layout.myadapter, null);
        view.name_city = (TextView) convertView.findViewById(R.id.name_city);
        view.name_country=(TextView) convertView.findViewById(R.id.name_country);
        view.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
        view.imageView = (ImageView) convertView.findViewById(R.id.imageView);
        view.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag(); // Here
                // we get  the position that we have set for the checkbox using setTag.
                myList.get(getPosition).setChecked(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                if (isChecked) {
                    wishList.add(myList.get(position).getName_city() + "\n");
                    positionList.add(position);

                }
                else
                {
                    wishList.remove(myList.get(position).getName_city() + "\n");
                    positionList.remove(position);
                }
            }
        });
        convertView.setTag(view);
    } else {
        view = (ViewHolder) convertView.getTag();
    }
    view.imageView.setImageResource(myList.get(position).getImgId());
    view.checkBox.setTag(position);
    view.name_country.setText(myList.get(position).getName_country());
    view.name_city.setText(myList.get(position).getName_city());
    view.checkBox.setChecked(myList.get(position).isChecked());
    return convertView;
}}

这里是MyItem.java。

public class MyItem {
private String name_country;
private String name_city;
private int ImgId;
private boolean checked;

public MyItem(String name_country, String name_city, int imgId, boolean checked) {
    this.name_country = name_country;
    this.name_city = name_city;
    ImgId = imgId;
    this.checked = checked;
}

public String getName_country() {
    return name_country;
}

public void setName_country(String name_country) {
    this.name_country = name_country;
}

public String getName_city() {
    return name_city;
}

public void setName_city(String name_city) {
    this.name_city = name_city;
}

public int getImgId() {
    return ImgId;
}

public void setImgId(int imgId) {
    ImgId = imgId;
}

public boolean isChecked() {
    return checked;
}

public void setChecked(boolean checked) {
    this.checked = checked;
}}

0 个答案:

没有答案