Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义ListActivity中复选框的实现_Android - Fatal编程技术网

Android 自定义ListActivity中复选框的实现

Android 自定义ListActivity中复选框的实现,android,Android,在下面的代码中 这是我的主要活动,在这里我选择各种产品并继续进行 但当我选中多个或一个时,它传递0值; 这是在吐司,我没有得到任何东西,如下图所示 public class MainActivity extends ListActivity { ListView list; Button btn1; String url=""; private ArrayList <Product> allProducts = new ArrayList<Product>(); pr

在下面的代码中 这是我的主要活动,在这里我选择各种产品并继续进行 但当我选中多个或一个时,它传递0值; 这是在吐司,我没有得到任何东西,如下图所示

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();



}
});


}

好,这里声明了checkbox
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
      }
   });
}

getCheckedItemCount()
--这在代码中的什么位置?我添加了计算上述toast代码中选中项的方法,以将结果存储在MainActivity中的固定大小数组中抱歉--我在代码中没有看到这一点。考虑到我不是一个算命的人,我不得不在这里停止回答。这是我的错误,我使用了错误的词,这不在我的代码中,但我在我的工作区使用它在我的工作区代码中进行更新
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;
}



}
@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
      }
   });
}