0
В следующем коде вот моя основная деятельность Где я выбираю различный продукт и продолжаю дальше , но когда я проверил несколько или один, он передает значение 0; что в тосте я ничего не получаю, как показано ниже на картинкереализация флажка в Custom ListActivity
public class MainActivity extends ListActivity {
ListView list;
Button btn1;
String url="";
private ArrayList <Product> allProducts = new ArrayList<Product>();
private ProductAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
btn1 = (Button) findViewById(R.id.btn);
list = getListView();
url="http://192.168.1.100/test/product.txt?id=";//+d.getInt("id");
try{
ConnectivityManager c =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo n =c.getActiveNetworkInfo();
if (n!= null && n.isConnected()){
Log.d("url*********",url);
new Background().execute(url);
}
}catch(Exception e){}
adapter = new ProductAdapter(this,allProducts);
setListAdapter(adapter);
getListView().setItemsCanFocus(false);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
for(int i=0;i<allProducts.size();i++){
Product product = allProducts.get(i);
if(product.isChecked()){
responseText.append("\n" + product.getProduct_name());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
Вот ProductAdapter
public class ProductAdapter extends ArrayAdapter<Product> {
ArrayList<Product> allProducts;
LayoutInflater vi;
int Resource;
Context context;
public ProductAdapter (Context context ,ArrayList<Product> objects)
{
super(context, R.layout.productrow, objects);
allProducts = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.productrow, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.price);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb1);
int s = allProducts.get(position).getProduct_price();
name.setText(Integer.toString(s));
cb.setText(allProducts.get(position).getProduct_name());
if(allProducts.get(position).isChecked())
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
Мой файл Product.java который моя модель
public class Product {
int product_id;
private boolean checked = false ;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
String product_name;
int product_price;
int product_qunatity;
int hotel_id;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getProduct_price() {
return product_price;
}
public void setProduct_price(int product_price) {
this.product_price = product_price;
}
public int getProduct_qunatity() {
return product_qunatity;
}
public void setProduct_qunatity(int product_qunatity) {
this.product_qunatity = product_qunatity;
}
public int getHotel_id() {
return hotel_id;
}
public void setHotel_id(int hotel_id) {
this.hotel_id = hotel_id;
}
}
но я получаю list.getCheckedItemCount() в 0 !!! почему это так ? – user3651256
'getCheckedItemCount()' - где это в вашем коде? – olyv
Я добавил, чтобы подсчитать отмеченный элемент выше указанного тоста, чтобы сохранить результат в массиве фиксированного размера в mainActivity – user3651256