我想从列表视图中获取文本视图的所有值。我怎么做。我有一个列表视图,并在列表视图中放置一个数组列表。我的列表视图是一个自定义列表视图。我想从价格文本视图中获取所有数据,然后我想显示价格的总和。我做了一些代码,但给出了一些错误 这是我的代码。
public class SelectedProductFromShopingCartShow extends AppCompatActivity {
ArrayList<ShowProducts> arrayList = new ArrayList<>();
String condition = "SelectedItemsFromShoppingCart";
CustomAdapter customAdapter;
ListView listView;
TextView tvTotal;
int total;
private static final String TAG = "SelectedProductFromShop";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selected_product_from_shoping_cart_show);
listView = (ListView) findViewById(R.id.listViewSelectedItemsOfShopingCart);
tvTotal = (TextView) findViewById(R.id.tvTotal);
arrayList = (ArrayList<ShowProducts>) getIntent().getSerializableExtra("selectedList");
customAdapter = new CustomAdapter(SelectedProductFromShopingCartShow.this, R.layout.show_selected_item_of_shopingcart, condition, arrayList);
listView.setAdapter(customAdapter);
total = getTotal(listView);
tvTotal.setText(String.valueOf(total).toString());
}
public int getTotal(ListView listView) {
int sum = 0;
for (int i = 0; i < listView.getCount(); i++) {
Log.d(TAG, "getTotal: "+listView.getCount());
Log.d(TAG, "getTotal: "+arrayList.size());
View view = listView.getChildAt(i);
TextView tvprice = (TextView) view.findViewById(R.id.tvSetPriceOfSeletedItem);
TextView tvquantity = (TextView) view.findViewById(R.id.tvQunatitySetOfSelectedItem);
int price = Integer.parseInt(tvprice.getText().toString());
int quantity = Integer.parseInt(tvquantity.getText().toString());
sum = sum + price * quantity;
}
return sum;
}
}
这是我的错误。我不知道怎么做。请帮帮我。
07-26 18:03:49.404 21155-21155/com.sizdom.sizdomstockmanager E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sizdom.sizdomstockmanager/com.sizdom.sizdomstockmanager.SelectedProductFromShopingCartShow}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5069)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.sizdom.sizdomstockmanager.SelectedProductFromShopingCartShow.getTotal(SelectedProductFromShopingCartShow.java:88)
at com.sizdom.sizdomstockmanager.SelectedProductFromShopingCartShow.onCreate(SelectedProductFromShopingCartShow.java:38)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5069)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我查看了多个网站的解决方案。但我找不到任何解决方案
自定义适配器
public class CustomAdapter extends ArrayAdapter {
private List<Integer> list;
private List<ShowProducts> listShowProducts;
private Context context;
private int resource;
private String condition;
String uri;
private static final String TAG = "CustomAdapter";
int i = 0;
public CustomAdapter(@NonNull Context context, @LayoutRes int resource, List<ShowProducts> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.listShowProducts = objects;
}
@Override
public int getCount() {
return super.getCount();
}
@Nullable
@Override
public Object getItem(int position) {
return super.getItem(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(resource, parent, false);
final ShowProducts showProducts = listShowProducts.get(position);
ImageView imageView = (ImageView) view.findViewById(R.id.imageViewOfSelecteditem);
ImageView plus = (ImageView) view.findViewById(R.id.imageviewPlus);
ImageView minus = (ImageView) view.findViewById(R.id.imageviewminus);
TextView tvSetNameOfSeletedItem = (TextView) view.findViewById(R.id.tvSetNameOfSeletedItem);
TextView tvSetSizeOfSeletedItem = (TextView) view.findViewById(R.id.tvSetSizeOfSeletedItem);
TextView tvSetPriceOfSeletedItem = (TextView) view.findViewById(R.id.tvSetPriceOfSeletedItem);
final TextView tvQunatitySetOfSelectedItem = (TextView) view.findViewById(R.id.tvQunatitySetOfSelectedItem);
tvSetNameOfSeletedItem.setText(showProducts.getProduct_name().toString());
tvSetSizeOfSeletedItem.setText(showProducts.getSize_name());
tvSetPriceOfSeletedItem.setText(String.valueOf(showProducts.getSize_price()).toString());
uri = showProducts.getProduct_photo().toString();
Picasso.with(context).load(uri).into(imageView);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a=Integer.parseInt(tvQunatitySetOfSelectedItem.getText().toString());
a++;
if (a<=showProducts.getSize_quantity())
tvQunatitySetOfSelectedItem.setText(String.valueOf(a).toString());
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a=Integer.parseInt(tvQunatitySetOfSelectedItem.getText().toString());
a--;
if (a>0)
tvQunatitySetOfSelectedItem.setText(String.valueOf(a).toString());
}
});
}
return view;
}
}
这是我的列表视图,它显示了我获得价值但看起来很恐怖的价值
我调试了应用
public int getTotal(ListView listView) {
int sum = 0;
for (int i = 0; i < listView.getCount(); i++) { // i=0;
Log.d(TAG, "getTotal: "+listView.getCount());
Log.d(TAG, "getTotal: "+arrayList.size()); // arraylist=4;
View view = listView.getChildAt(i); //view=null;
TextView tvprice = (TextView) view.findViewById(R.id.tvSetPriceOfSeletedItem);
Log.d(TAG, "getTotal: "+tvprice.getText());
TextView tvquantity = (TextView) view.findViewById(R.id.tvQunatitySetOfSelectedItem);
答案 0 :(得分:1)
您没有在tvquantity textView中设置tvprice和数量的任何价格。因此为空值。
答案 1 :(得分:1)
ListView
加载ArrayList<ShowProducts> arrayList
如果您的观看次数有所更新,请在arrayList
内更新您的模型。
然后,您希望获得所有产品的总价值,只需通过arrayList
计算即可完成。请勿触摸View
首先,请确保您的模型包含2个字段:quantity
和price
。
其次,在Adapter
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(resource, parent, false);
ShowProducts showProducts = listShowProducts.get(position);
// Handle your views...
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quantity = Integer.valueOf(tvQunatitySetOfSelectedItem.getText().toString());
quantity++;
if (quantity <= showProducts.getSize_quantity()) {
tvQunatitySetOfSelectedItem.setText(String.valueOf(quantity));
showProducts.setQuantity(quantity); // If your model doesn't have this field, create it
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quantity = Integer.valueOf(tvQunatitySetOfSelectedItem.getText().toString());
quantity--;
if (quantity > 0) {
tvQunatitySetOfSelectedItem.setText(String.valueOf(quantity));
showProducts.setQuantity(quantity); // If your model doesn't have this field, create it
}
}
});
return view;
}
public float getTotal() { // You can use int, long or double ... It's up to you
float result = 0;
for (ShowProducts showProducts : listShowProducts) {
result += showProducts.getPrice() * showProducts.getQuantity();
}
return result;
}
然后,如果你想计算总价 - &gt;只需致电yourAdapter.getTotal()
答案 2 :(得分:1)
因为你没有在你的arraylist中添加任何值,这就是你获得nullpointer异常的原因 首先调用下面的函数来添加arraylist中的元素。然后你就能得到总价。
private void addProducts() {
ShowProducts products = new ShowProducts();
products.price = 100;
products.quantity = 10;
arrayList.add(products);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// your code
addProducts();
//yourcode
}
您可以使用以下函数获取总和
private int total() {
int sum = 0;
for(ShowProducts product: arraylist) {
sum += product.price;
}