Android 带有复选框的自定义ListActivity获取错误
这是我的主要活动 我有带有复选框的自定义ListActivityAndroid 带有复选框的自定义ListActivity获取错误,android,Android,这是我的主要活动 我有带有复选框的自定义ListActivity public class MainActivity extends ListActivity { ListView list; Button btn1; String url=""; private ArrayList <Product> allProducts = new ArrayList<Product>(); private ProductAdapter adapter; @Override
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);
}
}
当我选中多个盘子并单击继续时(给出checked.size的值为0),
正在进行的第二个活动它不会给出任何结果 布局
代码,类
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;
}
}
私有ListView listdata;
字符串[]值=新字符串[]{“Android”、“iPhone”};
listdata=(ListView)findViewById(R.id.listdata);
最终ArrayList=新ArrayList();
对于(int i=0;i
使用列表的长度,而不是SparseBooleanArray
,而不是选中的。size()
for(int i=0;i
请看这里:
这里呢
与其他观察结果一起,您的ViewHolder中充满了其他视图,例如文本视图和复选框。您正在访问TextView并希望在其中找到pojo,它只有文本。
适配器加载带有文本的视图——getProgramName()返回文本。
holder.name.setText(allProducts.get(position.getProduct_name())
要访问文本视图中的文本,请使用以下命令:selectedItems.add(adapter.getItem(position.getText().toString()) 告诉我我的代码有什么问题,请帮助或任何建议总是好的。他们不担心这个问题……这个代码可能会帮助你……你不需要这些代码。这是大代码,如果你不需要更多的代码,那么你为什么要写更多的代码……在我的代码中,我以价格展示了产品,如果您在单击侦听器时遇到问题,则应修改此行final StablerRayAdapter=new StablerRayAdapter(此,android.R.layout.simple\u列表\u项\u多选,列表);但我想用price显示产品,我从服务器获取数据并存储到poduct对象中,该对象包含price、name和id
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;
}
}
<ListView
android:id="@+id/listdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ListView>
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;
}
}
for (int i = 0; i < list.getCount(); i++)