本文介绍了自定义ListActivity使用复选框得到一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的主要活动我有自定义ListActivity使用复选框
Here is my main ActivityI have custom ListActivity with checkbox
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(getApplicationContext(),R.layout.productrow,allProducts);
setListAdapter(adapter);
getListView().setItemsCanFocus(false);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = getListView().getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
ArrayList<Integer> selectedItemsPrice = new ArrayList<Integer>();
ArrayList<Integer> selectedItemsId = new ArrayList<Integer>();
Log.d("**************",Integer.toString(checked.size()));
if(checked!=null){
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i))
{
selectedItems.add(adapter.getItem(position).getProduct_name());
selectedItemsPrice.add(adapter.getItem(position).getProduct_price());
selectedItemsId.add(adapter.getItem(position).getProduct_id());
}
}}
int k = selectedItems.size();
int[] outputStrArrId = new int[k];
int[] outputStrArrPrice = new int[k];
String[] outputStrArrItem = new String[k];
for (int i = 0; i < k; i++) {
outputStrArrItem[i] = selectedItems.get(i);
outputStrArrId[i] = selectedItemsId.get(i);
outputStrArrPrice[i] = selectedItemsPrice.get(i);
}
Intent intent = new Intent(getApplicationContext(), AddOrder.class);
Bundle b = new Bundle();
b.putStringArray("st3", outputStrArrItem);
b.putIntArray("st1",outputStrArrId );
b.putIntArray("st2",outputStrArrPrice );
intent.putExtras(b);
startActivity(intent);
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Product p = allProducts.get(position);
if (p.isChecked()) {
p.setChecked(false);
l.setItemChecked(position, false);
} else {
p.setChecked(true);
l.setItemChecked(position, true);
}
}
和我的适配器
public class ProductAdapter extends ArrayAdapter<Product> {
private ArrayList<Boolean> itemChecked = null;
ArrayList<Product> allProducts;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public ProductAdapter (Context context , int resource ,ArrayList<Product> objects)
{
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
allProducts = objects;
}
@Override
public View getView(final int position ,View convertView,ViewGroup parent)
{
View v = convertView;
if(v==null)
{
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.name = (TextView) v.findViewById(R.id.tv1);
holder.price = (TextView) v.findViewById(R.id.price);
holder.checkbox = (CheckBox) v.findViewById(R.id.cb1);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
holder.name.setText(allProducts.get(position).getProduct_name());
int s = allProducts.get(position).getProduct_price();
holder.price.setText(Integer.toString(s));
holder.checkbox.setChecked(allProducts.get(position).isChecked());
return v;
}
static class ViewHolder {
public TextView name;
public TextView price;
public CheckBox checkbox ;
}
}
这是我的POJO类产品
here is my pojo class for product
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;
}
}
当我检查多个菜,点击进入(给checked.size具有0值)要去次活动也没有给出任何结果
When I check multiple dishes and click on proceed (gives checked.size has 0 value),on going second activity it not give any result
推荐答案
试图通过这个code我使用它可以帮助你布局
Try By this Code I have Use It may help YouLayout
<ListView
android:id="@+id/listdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ListView>
code的.class
Code .class
private ListView listdata ;
String[] values = new String[] { "Android", "iPhone"};
listdata=(ListView)findViewById(R.id.listdata);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_multiple_choice, list);
listdata.setAdapter(adapter);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
这篇关于自定义ListActivity使用复选框得到一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!