ListView未显示,非常感谢任何帮助!我能够单独使用ListView显示产品,但我无法弄清楚如何在我的相对布局下面显示它。
Cart.java:
public class Cart extends AppCompatActivity {
private String user;
static CartChangeListener cartChangeListener;
ProductsAdapter adaptCart;
ArrayList<Product> cartItems = new ArrayList<>();
Menu menu;
float total = (float)0.00;
float shipCost = 15;
float tax = (float)0.07;
String URI;
TextView subTotal;
TextView shipping;
TextView taxes;
TextView totals;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
View header = getLayoutInflater().inflate(R.layout.activity_cart, null);
//ListView.addHeaderView(headerView);
user = MainActivity.currentAccount.getUsername();
if(user == null || user.equals("Guest")) {
shipCost = (float)0.00;
tax = (float)0.00;
}
if(user != null && !(user.equals("Guest"))) {
new getCartIcon().execute();
//View header = getLayoutInflater().inflate(R.layout.activity_cart, null);
//View footer = getLayoutInflater().inflate(R.layout.activity_search, null);
ListView listView = (ListView) findViewById(R.id.list_cart_view);
new returnCartItems().execute();
//if (!(cartItems.isEmpty())) {
adaptCart = new ProductsAdapter(Cart.this, 0, cartItems);
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
try {
ProductsAdapter.ViewHolder holder = new ProductsAdapter.ViewHolder();
holder.product_name = (TextView) v.findViewById(R.id.product_name);
holder.product_dept = (TextView) v.findViewById(R.id.product_dept);
holder.product_desc = (TextView) v.findViewById(R.id.product_desc);
holder.product_price = (TextView) v.findViewById(R.id.product_price);
holder.product_qty = (TextView) v.findViewById(R.id.product_qty);
holder.product_img = (ImageView) findViewById(R.id.icon);
URI = "http://www.michaelscray.com/Softwear/graphics/";
String dept = "Dept: ";
String money = "$";
String qty = "Qty: ";
URI += cartItems.get(position).getProduct_img();
Uri uris = Uri.parse(URI + cartItems.get(position).getProduct_img());
URI uri = java.net.URI.create(URI);
holder.product_name.setText(cartItems.get(position).getProduct_name());
holder.product_desc.setText(cartItems.get(position).getProduct_desc());
holder.product_dept.setText(dept + cartItems.get(position).getProduct_dept());
holder.product_price.setText(money + String.valueOf(cartItems.get(position).getPrice()));
holder.product_qty.setText(qty + String.valueOf(cartItems.get(position).getProduct_qty()));
Picasso.with(getApplicationContext()).load(URI).error(R.mipmap.ic_launcher).into(holder.product_img);
} catch (Exception e) {
e.printStackTrace();
}
}
});
listView.setAdapter(adaptCart);
//}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
this.menu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
return true;
}
public class getCartIcon extends AsyncTask<Void, Void, Void> {
String tempUser = user;
int cartNum = 0;
float tempTotal;
@Override
protected void onPreExecute() {
subTotal = (TextView) findViewById(R.id.textView_subTotal);
shipping = (TextView) findViewById(R.id.textView_shipping);
totals = (TextView) findViewById(R.id.textView_total);
taxes = (TextView) findViewById(R.id.textView_taxes);
}
@Override
protected Void doInBackground(Void... arg0) {
if(tempUser != null) {
try {
Connection conn = ConnectDB.getConnection();
String queryString = "SELECT * FROM Orders WHERE `User_Name` = '" + tempUser + "'";
PreparedStatement st = conn.prepareStatement(queryString);
//st.setString(1, tempUser);
final ResultSet result = st.executeQuery(queryString);
while (result.next()) {
try {
tempTotal += result.getFloat("Price");
//skus.add(result.getInt("SKU"));
} catch(SQLException e) {
e.printStackTrace();
}
cartNum++;
//setTotal(result.getFloat(String.valueOf("Price")));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
getCartItems(cartNum);
subTotal.setText(String.valueOf(tempTotal));
shipping.setText(String.valueOf(shipCost));
tax = tempTotal * tax;
taxes.setText(String.valueOf(tax));
totals.setText(String.valueOf(tempTotal + shipCost + tax));
//setTotal(tempTotal);
//super.onPostExecute(result);
}
}
public class returnCartItems extends AsyncTask<Void, Void, Void> {
String tempUser = user;
Product product = null;
List<Integer> skus = new ArrayList<>();
//String descr = "DETAILS: \n\n";
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... arg0) {
if(tempUser != null) {
try {
Connection conn = ConnectDB.getConnection();
String queryString = "SELECT * FROM Orders WHERE `User_Name` = '" + tempUser + "'";
PreparedStatement st = conn.prepareStatement(queryString);
//st.setString(1, tempUser);
final ResultSet result = st.executeQuery(queryString);
while (result.next()) {
try {
skus.add(result.getInt("SKU"));
} catch(SQLException e) {
e.printStackTrace();
}
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
Connection conn = ConnectDB.getConnection();
String queryString = "SELECT * FROM Inventory";
PreparedStatement st = conn.prepareStatement(queryString);
//st.setString(1, tempUser);
final ResultSet result = st.executeQuery(queryString);
while (result.next()) {
for(int i=0; i < skus.size(); i++) {
if(skus.get(i) == result.getInt("SKU")) {
product = new Product();
product.setProduct_name(result.getString("Name"));
product.setProduct_dept(result.getString("Department"));
product.setProduct_desc(result.getString("Description"));
product.setPrice(result.getFloat("Price"));
product.setProduct_qty(result.getInt("Quantity"));
product.setProduct_img(result.getString("Image"));
cartItems.add(product);
/*
descr += "Product: " + result.getString("Name") + "\n" +
"Department: " + result.getString("Department") + "\n" +
"Price: $" + result.getFloat("Price") + "\n\n";
*/
}
}
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
public void setTotal(float total) {
this.total += total;
}
public float getTotal() {
return total;
}
public void getCartItems(int cart) {
MenuItem cartMenuItem = (MenuItem) menu.findItem(R.id.action_cart);
if (cart == 0) {
cartMenuItem.setIcon(R.drawable.cart0);
}
if (cart == 1) {
cartMenuItem.setIcon(R.drawable.cart1);
}
if (cart == 2) {
cartMenuItem.setIcon(R.drawable.cart2);
}
if (cart == 3) {
cartMenuItem.setIcon(R.drawable.cart3);
}
if (cart == 4) {
cartMenuItem.setIcon(R.drawable.cart4);
}
if (cart == 5) {
cartMenuItem.setIcon(R.drawable.cart5);
}
if (cart > 5) {
cartMenuItem.setIcon(R.drawable.cart5plus);
}
if (cart > 10) {
cartMenuItem.setIcon(R.drawable.cart10plus);
}
}
}
ProductsAdapter.java:
public class ProductsAdapter extends ArrayAdapter<Product> {
private Activity activity;
private ArrayList<Product> products;
private static LayoutInflater inflater = null;
String money = "$";
String dept = "Dept: ";
String qty ="Qty: ";
static String URI;
public ProductsAdapter(Activity activity, int textViewResourceId, ArrayList<Product> product) {
super(activity, textViewResourceId, product);
try {
this.activity = activity;
this.products = product;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//imageLoader = new ImageLoader(activity.getApplicationActivity());
} catch (Exception e) {
return;
}
}
@Override
public int getCount() {
return products.size();
}
public Product getItem(Product position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView product_name;
public TextView product_desc;
public TextView product_dept;
public TextView product_price;
public TextView product_qty;
public ImageView product_img;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
try {
if(convertView == null) {
vi = inflater.inflate(R.layout.activity_search, parent, false);
holder = new ViewHolder();
holder.product_name = (TextView) vi.findViewById(R.id.product_name);
holder.product_dept = (TextView) vi.findViewById(R.id.product_dept);
holder.product_desc = (TextView) vi.findViewById(R.id.product_desc);
holder.product_price = (TextView) vi.findViewById(R.id.product_price);
holder.product_qty = (TextView) vi.findViewById(R.id.product_qty);
holder.product_img = (ImageView) vi.findViewById(R.id.icon);
vi.setTag(holder);
}
else {
holder = (ViewHolder) vi.getTag();
}
URI = "http://www.michaelscray.com/Softwear/graphics/";
URI += products.get(position).getProduct_img();
Uri uri = Uri.parse(URI + products.get(position).getProduct_img());
holder.product_name.setText(products.get(position).getProduct_name());
holder.product_desc.setText(products.get(position).getProduct_desc());
holder.product_dept.setText(dept + products.get(position).getProduct_dept());
holder.product_price.setText(money + String.valueOf(products.get(position).getPrice()));
holder.product_qty.setText(qty + String.valueOf(products.get(position).getProduct_qty()));
Picasso.with(getContext()).load(URI).error(R.mipmap.ic_launcher).into(holder.product_img);
}
catch(Exception e) {
}
return vi;
}
}
并且,activity_cart.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llSliderCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#5e80ab"
android:orientation="vertical" >
<TextView
android:id="@+id/summary"
android:layout_width="254dp"
android:layout_height="55dp"
android:gravity="center"
android:text="CART SUMMARY"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/checkout_btn" />
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Checkout"
android:id="@+id/checkout_btn"
android:background="@drawable/shape"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="true"
android:paddingLeft="1dp"
android:paddingTop="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:textStyle="bold"
android:textColor="#ffffff" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/cart_rows"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="Subtotal"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/vertical_divider_welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingLeft="20dp"
android:text="@string/currency"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
<TextView
android:id="@+id/textView_subTotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="0.00"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/vertical_divider_welcome" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/cart_rows"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="Shipping"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/vertical_divider_welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingLeft="20dp"
android:text="@string/currency"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
<TextView
android:id="@+id/textView_shipping"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="0.00"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/vertical_divider_welcome" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/cart_rows"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="Tax"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/vertical_divider_welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingLeft="20dp"
android:text="@string/currency"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="0"
android:textColor="#a4a4a4"
android:textSize="@dimen/text_normal"
android:id="@+id/textView_taxes" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/vertical_divider_welcome" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/cart_rows"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp"
android:text="Total"
android:textStyle="bold"
android:textColor="@color/blue"
android:textSize="@dimen/text_title" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/vertical_divider_welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingLeft="20dp"
android:text="@string/currency"
android:textStyle="bold"
android:textColor="@color/blue"
android:textSize="@dimen/text_title" />
<TextView
android:id="@+id/textView_total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:textStyle="bold"
android:paddingRight="20dp"
android:text="0.00"
android:textColor="@color/blue"
android:textSize="@dimen/text_title" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/vertical_divider_welcome" />
<!--
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Description"
android:id="@+id/textView" />
-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cart contents:"
android:id="@+id/contents"
android:background="@color/vertical_divider_welcome"
android:textStyle="bold|italic"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/list_cart_view">
</ListView>
</LinearLayout>
再次感谢!
答案 0 :(得分:0)
当您的RelativeLayout
(ListView
之前的那个)高度为match_parent
时,它会占用窗口的剩余空间...所以没有你的位置要显示ListView
。
答案 1 :(得分:0)
试试这个..
将id提供给上面的相对布局并使用下面的相同内容。
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_below="@id/"
android:id="@+id/list_cart_view">
</ListView>