2017-05-28 112 views
0

我遇到問題,當選定的項目和我開始滾動他們正在被取消選擇,我見過一些其他一些帖子說這些放在數組等等,但我曾嘗試有幾個似乎沒有工作。當滾動時取消選擇列表視圖項目

我的代碼:

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", "5%", 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(); 
}*/ 
} 
+1

你應該使用一個ListView持有人模式。類似的東西: https://stackoverflow.com/questions/28730417/making-a-listview-holder-for-mainactivity – RonTLV

回答

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; 

     } 

    } 

希望這將幫助ü....如果u有任何疑問u能問

+0

在我的產品類中,我已經有一個名爲box的布爾值,我已經添加了上面的代碼來展示如何它在產品類中。 – 77rshah

+0

還setselected即將到來因爲無法解決方法setSelected – 77rshah

+0

任何幫助,請這? – 77rshah

相關問題