在Custom ListActivity中实现Checkbox

时间:2015-02-03 13:22:19

标签: android

在以下代码中 这是我的主要活动我选择各种产品并继续进行 但当我检查多个或一个时,它传递0值; 在Toast中我没有得到如下图像

中的任何内容
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;
}



}

Img Of screen Shot

1 个答案:

答案 0 :(得分:0)

好的,有复选框CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb1);的声明,但听众在哪里?

我假设你需要添加监听器:

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
   //your code goes here
   cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.i(TAG, "Entered onCheckedChanged()");
      //other stuff for listener
      }
   });
}