我遇到的问题是,当选择的项目和我开始滚动它们被取消选择时,我已经看到了一些其他帖子,其中一些人说要放入一个阵列等等但我已经尝试过几个没有似乎工作。
我的代码:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.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) {
view = lInflater.inflate(R.layout.item, parent, false);
}
final Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
final CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
SharedPreferences settings = ctx.getSharedPreferences("data",ctx.MODE_PRIVATE);
boolean Checked = settings.getBoolean(p.name, false);
cbBuy.setChecked(Checked);
cbBuy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cbBuy.isChecked()==true){
SharedPreferences settings = ctx.getSharedPreferences("data",Context.MODE_PRIVATE);
settings.edit().putBoolean(p.name, true).commit();
Toast.makeText(ctx, "You Selected" + p.name, Toast.LENGTH_SHORT).show();
}else{
SharedPreferences settings = ctx.getSharedPreferences("data", Context.MODE_PRIVATE);
settings.edit().putBoolean(p.name, false).commit();
Toast.makeText(ctx, "You Deselected" +p.name, Toast.LENGTH_SHORT).show();
}
}
});{
return view;
}
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> box = new ArrayList<Product>();
for (Product p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
}
};
}
产品类别:
public class Product {
String name;
String price;
int image;
boolean isSelected = false;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected)
{
isSelected = selected;
}
Product(String _describe, String _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
isSelected = _box;
}
}
主要活动
public class MainActivity extends AppCompatActivity {
ArrayList<Product> products = new ArrayList<Product>();
ListAdapter boxAdapter;
private SharedPreferences mPrefs;
private String mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
fillData();
boxAdapter = new ListAdapter(this, products);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
}
void fillData() {
products.add(new Product("Al", "5230%", R.drawable.ic_launcher, false));
products.add(new Product("Al", "5230%", R.drawable.ic_launcher, false));
products.add(new Product("Alf", "5230%", R.drawable.ic_launcher, false));
products.add(new Product("Alfa", "5230%", R.drawable.ic_launcher, false));
products.add(new Product("Alfae", "5120%", R.drawable.ic_launcher, false));
products.add(new Product("Alfsdfsdfakher", "50435%", R.drawable.ic_launcher, false));
products.add(new Product("Alfasdfsdfkher", "5123120%", R.drawable.ic_launcher, false));
products.add(new Product("Alfasdfsdfkher", "501231%", R.drawable.ic_launcher, false));
products.add(new Product("Alfaksdfsdfher", "11250%", R.drawable.ic_launcher, false));
/*for (int i = 1; i <= 20; i++) {
products.add(new Product("Product " + i, i * 100,
R.drawable.ic_launcher, false));
}*/
}
/*public void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
for (Product p : boxAdapter.getBox()) {
if (p.box){
result += "\n" + p.name;
totalAmount+=p.price;
}
}
Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
}*/
}
答案 0 :(得分:1)
U可以管理模型类中的一个变量 喜欢这个
public class Product
{
private boolean isSelected=false;
//Make ur getter and settter
}
适配器类
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
final Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
final CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
SharedPreferences settings = ctx.getSharedPreferences("data",ctx.MODE_PRIVATE);
boolean Checked = settings.getBoolean(p.name, false);
cbBuy.setChecked(Checked);
//use click listener instead of checked change
cbBuy.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(objects.get(position).isSelected)
{
cbBuy.setChecked(false);
objects.get(position).setSelected(false);
//Save into sharepref here
SharedPreferences settings = ctx.getSharedPreferences("data",Context.MODE_PRIVATE);
settings.edit().putBoolean(p.name, false).commit();
Toast.makeText(ctx, "You Selected" + p.name, Toast.LENGTH_SHORT).show();
}
else
{
cbBuy.setChecked(true);
objects.get(position).setSelected(true);
//Save into sharepref here
SharedPreferences settings = ctx.getSharedPreferences("data",Context.MODE_PRIVATE);
settings.edit().putBoolean(p.name, true).commit();
Toast.makeText(ctx, "You Selected" + p.name, Toast.LENGTH_SHORT).show();
}
}
});
//For scrolling issue use this
if(objects.get(position).isSelected)
{
cbBuy.setChecked(true);
}
else
{
cbBuy.setChecked(false);
}
return view;
}
}
希望这会对你有所帮助....如果你有任何问题,你可以问