I am creating a shopping cart app and I am trying to populate a String[] Product which is called items. Each item has an ID a Name and a Price. The issue I am having is that I am trying to make a custom ListView but I can not get it to run on my android device. It keeps crashing every time I use a customer ListView. Maybe I am not doing it right or I need to create a customer adapter as well? I have tried these methods but neither were successful. Here is what I have, I currently am using the simple_list_item_check layout but I just implemented that to see if it would work, in which that does. The android simple layouts do, but my custom won't. Can anyone lead me in the right direction?
CustomerView.activity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CheckedTextView;
public class CustomerView extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_view);
Product[] items = {
new Product(1, "Milk", 21.50),
new Product(2, "Butter", 15.99),
new Product(3, "Yogurt", 14.90),
new Product(4, "Toothpaste", 7.99),
new Product(5, "Ice Cream", 10.00),
};
final ListAdapter theAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_checked, items);
ListView customerlist = (ListView) findViewById(R.id.customerList);
customerlist.setTextFilterEnabled(true);
customerlist.setAdapter(theAdapter);
customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView check = (CheckedTextView)view;
check.setChecked(!check.isChecked());
String itemselected = "You Touched " +
String.valueOf(theAdapter.getItem(position));
Toast.makeText(CustomerView.this, itemselected, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_customer_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Product.java
public class Product {
private int id;
private String name;
private double price;
private boolean selected;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return this.id + " " + this.name + " [$" + this.price + "]";
}
}
row_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="shoppingcart.cop4331.com.shoppingcart.CustomerView">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Cart"
android:gravity="right"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:textSize="20sp"/>
</LinearLayout>
0 comments:
Post a Comment