I have created an android application. in that application when I click on add, item added to the array list and this array list I want to show in Cart Activity. how to do that.
here is my activity1
public void addShirt(View view) {
MainActivity.cartItems.add(getString(R.string.shirt));
}
public void addPant(View view) {
MainActivity.cartItems.add(getString(R.string.pant));
}
public void view(View view) {
Intent i =new Intent(OnlyIron.this,CartActivity.class);
startActivity(i);
}
and cart activity is
for(int i=0; i<MainActivity.cartItems.size();i++) {
Toast.makeText(this, "item : " + MainActivity.cartItems.get(i), Toast.LENGTH_SHORT).show();
}
it showing toast but I enter code here want this show in listview
First lets Create model class and store data in it.
public class Cart {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now to add items in that list i would call it this way :
private ArrayList<Cart > cart_array;
cart_array= new ArrayList<>();
Cart cart1 = new Cart ();
cart.setId("1");
cart.setName("first product");
Cart cart2 = new Cart ();
cart.setId("2");
cart.setName("first product");
Cart cart3 = new Cart ();
cart.setId("3");
cart.setName("first product");
//and than add your model into array
cart_array.add(cart1);
cart_array.add(cart2);
cart_array.add(cart3);
//and finaly set your adapter
Cart_Adapter adapter = new Cart_Adapter(cart_array, getActivity());
Recycler.setAdapter(adapter );
Please take a list view in CartActivity & create an Custom Adapter as according to your need(UI). Pass the MainActivity.cartItems this list to Adapter. It will start to show in your CartActivity.
You can see below example:
public class CustomAdapter extends BaseAdapter{
Activity mContext;
public ArrayList<String> mCartList = new ArrayList<String>();
private LayoutInflater mInflater=null;
public CustomAdapter(Activity activty, ArrayList<String> list)
{
this.mContext = activty;
mInflater = activty.getLayoutInflater();
this.mCartList=list;
}
#Override
public int getCount() {
if (mCartList != null){
return mCartList.size();
} else{
return 0;
}
}
#Override
public String getItem(int arg0) {
return mCartList.get(arg0);
}
#Override
public long getItemId(int index) {
return index;
}
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
final ViewHolder holder;
if (convertView == null ) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_layout, null);
holder.mItemNameTV= (TextView) convertView.findViewById(R.id.itemtv);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mNameTV.setText(mCartList.get(position));
return convertView;
}
private static class ViewHolder {
TextView mNameTV;
}
}
// Item Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/forty_dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/fieldTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/five_dp"
android:layout_weight="0.4"
android:padding="#dimen/ten_dp"
android:text="Custom Field"
android:textColor="#color/dark_gray_color"
android:textSize="#dimen/normal_font_size"
android:visibility="visible" />
</LinearLayout>
// Let Suppose your CartActivity is following:
ListView mListView = (ListView)findViewById(R.id.listview);
CustomAdapter adapter = new CustomAdapter(this, MainActivity.cartItems);
mListView.setAdapter(adapter);
Related
I have a list of posts with the autor name.
I want to know which author name had been clicked.
The layout :
<TextView
android:id="#+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:onClick="userClick"
tools:text="User" />
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:layout_alignLeft="#+id/user"
android:layout_below="#+id/user"
android:onClick="postClick"
tools:text="Content" />
When an author name is clicked this function is called :
public void userClick(View v) {
//Is it possible to know which author name had been clicked from here?
}
What is the correct method to identify on which author name the user had clicked.
EDIT
I've tryied the getId() method
public void userClick(View v) {
Log.d("userClick called :",String.valueOf(v.getId()));
}
It always return the same id even if I click on different users.
If you are using listview, you can use the following:
String author ;
yourListView.setOnItemClickListener (new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Textview user = (TextView) view.findViewById(R.id.user);
author=user.getText().toString();
}
});
Update :
Did you try that ?
public void userClick(View view){
TextView textview = (TextView) view;
author=textview.getText().toString();
}
I assume that this is the RecyclerView.
And you can use
RecyclerViewAdapter.class
private View.OnClickListener globalClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
int clickedPosition = (int) v.getTag();
// do something with position
}
}
RecyclerViewAdapter.onBindViewHolder
holder.itemView.setTag(position);
holder.itemView.setOnClickListener(globalClickListener);
Also if you want to get view id,
private View.OnClickListener globalClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(R.id.title == v.getId()){
// Clicked child view (R.id.title which is implemented in XML)
}
I would solve this problem by implementing an AdapterView.OnItemClickListener and adding it to my ListView. The following solution works because both MyAdapter and MyListener have access to the Activity's posts field.
public class MainActivity extends AppCompatActivity {
private List<Post> posts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
posts = new ArrayList<>();
posts.add(new Post("author1", "title1"));
posts.add(new Post("author1", "title2"));
posts.add(new Post("author2", "title3"));
posts.add(new Post("author3", "title4"));
posts.add(new Post("author2", "title5"));
posts.add(new Post("author1", "title6"));
posts.add(new Post("author3", "title7"));
posts.add(new Post("author3", "title8"));
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new MyAdapter());
list.setOnItemClickListener(new MyListener());
}
private class MyAdapter extends BaseAdapter {
#Override
public int getCount() {
return (posts != null) ? posts.size() : 0;
}
#Override
public Post getItem(int position) {
return posts.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post, parent, false);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
if (holder == null) {
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
Post item = getItem(position);
holder.user.setText(item.user);
holder.title.setText(item.title);
return convertView;
}
}
private class MyListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Post item = posts.get(position);
Toast.makeText(MainActivity.this, item.user, Toast.LENGTH_SHORT).show();
}
}
private static class ViewHolder {
private final TextView user;
private final TextView title;
public ViewHolder(View v) {
this.user = (TextView) v.findViewById(R.id.user);
this.title = (TextView) v.findViewById(R.id.title);
}
}
private static class Post {
private final String user;
private final String title;
public Post(String user, String title) {
this.user = user;
this.title = title;
}
}
}
In the java file, after calling the onClickListener, try to access the button or clickable using the this keyword. It refers the the context of the clicked item.
Works in Java and JavaScript.
I have a listview, from database (category list) and i want to load gridview (product gridview) when i click any button in the category listview, i'm able to make the listview and gridview, but cannot connect them, since im still new at android programming and have no clue to do it.
my main purpose is:
List category
on click on the category button will load gridview of product button
on click product button will add the product id to table cart
ps: if you notice i do SQL query in the mainActivity class, i still new at the code so i tried my best first before convert into better practice.
Any suggestion to do it?
Here i include my snippet:
Category.java (setter and getter)
public class Category {
private int _id;
private String _name;
public Category() {
}
public Category(int id, String name) {
this._id = id;
this._name = name;
}
public Category(String name) {
this._name = name;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String get_name()
{
return this._name;
}
public void set_name(String name)
{
this._name = name;
}
}
CategoryListAdapter.java
public class CategoryListAdapter extends BaseAdapter {
private ArrayList<Category> listData;
private LayoutInflater layoutInflater;
public CategoryListAdapter(Context aContext, ArrayList<Category> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
CategoryListAdapter.ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_category, null);
holder = new CategoryListAdapter.ViewHolder();
holder.btnCategory = (Button) convertView.findViewById(R.id.btnCategory);
convertView.setTag(holder);
} else {
holder = (CategoryListAdapter.ViewHolder) convertView.getTag();
}
holder.btnCategory.setText(listData.get(position).get_name());
return convertView;
}
static class ViewHolder {
Button btnCategory;
}
}
listview_category.xml (layout that will be repeated)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:textSize="16dp"
android:id="#+id/btnCategory"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="#drawable/button_category"/>
</LinearLayout>
listview inside MainActivity
<ListView
android:id="#+id/listviewCategory"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
java inside MainActivity
protected void onCreate(Bundle savedInstanceState) {
...
/* LIST CATEGORY TO LOAD PRODUCT */
ArrayList list_category = getListCategory();
final ListView listview_category = (ListView) findViewById(R.id.listviewCategory);
listview_category.setAdapter(new CategoryListAdapter(this, list_category));
listview_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Log.d(TAG, "click cat");
//Give product list an id from category_id get from here
}
});
...
}
/* List Category for selecting category product */
private ArrayList getListCategory() {
SQLiteDatabase mydatabase = openOrCreateDatabase("posDb",MODE_PRIVATE,null);
Cursor resultSet = mydatabase.rawQuery("Select * from categories",null);
ArrayList<Category> results = new ArrayList<Category>();
if (resultSet.moveToFirst()) {
do {
Category categoriesData = new Category();
categoriesData.set_name(resultSet.getString(1));
results.add(categoriesData);
} while (resultSet.moveToNext());
}
return results;
}
The grid of product mostly the same as category, but load product from database which later i want to onclick the product button to save record to cart table
on Click on button will add the product id in to table_cart
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ArrayList list_category = getListCategory();
int id[]={1, 2, 3, 4, 5};
String categoryName[]={"category1","category2","category3","category4","category5"};
ArrayList<Category> listData=new ArrayList<>();
Category category;
for (int i=0; i<id.length; i++)
{
category=new Category();
category.set_id(id[i]);// setting Id
category.set_name(categoryName[i]);// setting category name
listData.add(category);
}
final ListView listview_category = (ListView) findViewById(R.id.listviewCategory);
listview_category.setAdapter(new CategoryListAdapter(this, listData ));
}
}
CategoryListAdapter.java
public class CategoryListAdapter extends BaseAdapter {
private ArrayList listData;
private LayoutInflater layoutInflater;
ArrayList table_cart = new ArrayList();
private static final String TAG = "CategoryListAdapter";
public CategoryListAdapter(Context aContext, ArrayList<Category> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final CategoryListAdapter.ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_category, null);
holder = new CategoryListAdapter.ViewHolder();
holder.btnCategory = (Button) convertView.findViewById(R.id.btnCategory);
convertView.setTag(holder);
} else {
holder = (CategoryListAdapter.ViewHolder) convertView.getTag();
}
holder.btnCategory.setText(listData.get(position).get_name());
holder.btnCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// getting product id
int product_id = listData.get(position).getID();
Log.e(TAG, "product_id: " + product_id);
// now you can get category name as well
String category_name = listData.get(position).get_name();
Log.e(TAG, "category_name: " + category_name);
// now adding product id in to table_cart
table_cart.add(listData.get(position).getID());
}
});
return convertView;
}
static class ViewHolder {
Button btnCategory;
}
}
Category.java
public class Category {
private int _id;
private String _name;
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String get_name() {
return this._name;
}
public void set_name(String name) {
this._name = name;
}
public void set_id(int _id) {
this._id = _id;
}
}
there is no need to change layouts
Add on click listener for category button, add grid view in fragment and open fragment on button click.
holder.btnCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProductFragment nextFrag= new ProductFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag,TAG_FRAGMENT)
.commit();
}
});
i have 2 arrays productname and price. my each row in listview contains productname,price and an image button remove. when i click remove button,the selected productname and price should be removed from my array as well as from my listview. Please guide me doing this,is very important for me. If there is another way of doing this plz let me know.i m student only!
this is my CartActivity
public class CartActivity extends Activity implements View.OnClickListener {
CartAdapter contactAdapter;
ListView listView;
public String productname[]=new String[10];
public String price[]=new String[10];
int i=0,m;
CartActivity(String scanContent){
this.content = scanContent;
}
public CartActivity() {}
String product,Price;
String pri,prod;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
listView = (ListView) findViewById(R.id.cart_list_view);
contactAdapter = new CartAdapter(this,R.layout.activity_cart_list_view);
listView.setAdapter(contactAdapter);
intent = getIntent();
productname = intent.getStringArrayExtra("productname");
price = intent.getStringArrayExtra("price");
for(int j=0;j<price.length;j++) {
product = productname[j];
Price = price[j];
if (Price != null || product != null) {
Cart contacts = new Cart(product, Price);
contactAdapter.add(contacts);
}
}
}
this is my CartAdapter
public class CartAdapter extends ArrayAdapter {
List list = new ArrayList();
int ind,x=0;
public CartAdapter(CartActivity context, int resource) {
super(context, resource);
}
public void add(Cart object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
ContactHolder contactHolder;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.activity_cart_list_view,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_name =(TextView) row.findViewById(R.id.product_name);
contactHolder.tx_price =(TextView) row.findViewById(R.id.price);
contactHolder.cancelButton = (ImageButton) row.findViewById(R.id.cancel_button);
row.setTag(contactHolder);
}
else
{
contactHolder = (ContactHolder)row.getTag();
}
cancelButton.setTag(position);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer index = (Integer) view.getTag();
list.remove(index.intValue());
notifyDataSetChanged();
}
});
Cart contacts = (Cart) this.getItem(position);
contactHolder.tx_name.setText(contacts.getItemname());
contactHolder.tx_price.setText(contacts.getPrice());
return row;
}
static class ContactHolder
{
TextView tx_name,tx_price;
ImageButton cancelButton;
}
}
this is my Cart
public class Cart {
private String itemname,price;
public Cart(String itemname,String price) {
this.setItemname(itemname);
this.setPrice(price);
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public void setPrice(String price) {
this.price = price;
}
public String getItemname() {
return itemname;
}
public String getPrice() {
return price;
}
}
my each row in a listview contains this
Just do this,
in your adapter class,
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
list.remove(position); //position is getView position
// above line will remove from arraylist
notifyDataSetChanged(); //this line reflects change in listview
}
});
To remove from Activity class,
better create an ArrayList first.
ArrayList<Cart> cardList = new ArrayList<Cart>();
add into arrayList,
if (Price != null || product != null) {
Cart contacts = new Cart(product, Price);
cardList.add(contacts);
contactAdapter.add(contacts); // this line should be removed if you work with arrayList
}
now create a method to remove from ActivityCard arrayList,
//call this method from cancelimage click in adapter class
public void removeFromList(int position){
cardList.remove(position); //this will remove from activity arraylist
}
At the end calculate your bill from existing cartList data.
*** You can also create adapter class object with cardList just changing your constructor. If you do this then just call removeFromList on cancelimage click and put,
adapter.notifyDataSetChanged();
after
cardList.remove(position);
it will refresh your listview also.
I implemented a simple listview with an option to select each time only one item from it, this functionality is working properly.
Also I have a button for sorting the records in Asc or Desc order, when I am selecting a record and after that I am sorting the records the selector of the old record remains in the same old position, even when the selected position is updated different position.
MainActivity:
public class MainActivity extends Activity
{
private ListView _listView;
private PersonAdapter _adapter;
private Button _sortBtn;
private List<Person> _data;
private int _sort;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_listView = (ListView) findViewById(R.id.list);
_sortBtn = (Button) findViewById(R.id.sort_list_btn);
_sort = 1;
_data = new ArrayList<Person>();
_data.add(new Person("abc", "defg", 1));
_data.add(new Person("aaa", "defg", 12));
_data.add(new Person("ccc", "defg", 13));
_data.add(new Person("bb", "defg", 14));
_data.add(new Person("aa", "defg", 144));
_data.add(new Person("fff", "defg", 199));
_adapter = new PersonAdapter(this, _data);
_listView.setAdapter(_adapter);
_sortBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Comparator<Person> sortById = Person.getComperatorByFirstName(_sort);
Collections.sort(_data, sortById);
_adapter.setData(_data);
_sort = -_sort;
}
});
}
public static class Person
{
public String _fName;
public String _lName;
public int _age;
public boolean _selected;
public Person(String fName, String lName, int age)
{
_fName = fName;
_lName = lName;
_age = age;
}
public static Comparator<Person> getComperatorByFirstName(final int ascendingFlag)
{
return new Comparator<Person>()
{
#Override
public int compare(Person patient1, Person patient2)
{
return patient1._fName.compareTo(patient2._fName) * ascendingFlag;
}
};
}
}
}
listView adapter
public class PersonAdapter extends BaseAdapter
{
private Context _con;
private List<Person> _data;
public PersonAdapter(Context context, List<Person> data)
{
_con = context;
_data = data;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return _data.size();
}
#Override
public Person getItem(int position)
{
return _data.get(position);
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
Holder h = null;
if (convertView == null)
{
h = new Holder();
convertView = LayoutInflater.from(_con).inflate(R.layout.item_layout, parent, false);
h._backgroundItem = (LinearLayout) convertView.findViewById(R.id.item_layout);
h._fName = (TextView) convertView.findViewById(R.id.f_name);
h._lName = (TextView) convertView.findViewById(R.id.l_name);
h._age = (TextView) convertView.findViewById(R.id.age);
convertView.setTag(h);
}
else
{
h = (Holder) convertView.getTag();
}
Person p = getItem(position);
h._fName.setText(p._fName);
h._lName.setText(p._lName);
h._age.setText(String.valueOf(p._age));
h._backgroundItem.setActivated(p._selected);
return convertView;
}
public void setData(List<Person> data)
{
_data = data;
notifyDataSetChanged();
}
private static class Holder
{
public LinearLayout _backgroundItem;
public TextView _fName;
public TextView _lName;
public TextView _age;
}
}
item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:background="#drawable/list_selector"
android:weightSum="3" >
<TextView
android:id="#+id/f_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="#+id/l_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="#+id/age"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
I tried already to use clearChoices but after sorting I do not see the selector at all, also I tried to change the choise mode from single mode to none and then again single but without any success.
It is because you are switching the contents of the items inside the list item, but the click is bound to the position withing the ListView, therefore it doesn't "update" to the new position of the item.
I suggest you to programatically click on the new item when you finish the sorting.
Get the ID of the clicked item
public int getItemPosition(long id)
{
for (int position=0; position<mList.size(); position++)
if (mList.get(position).getId() == id)
return position;
return 0;
}
then after the sorting do the click
mList.performItemClick(
mList.getAdapter().getView(mActivePosition, null, null),
mActivePosition,
mList.getAdapter().getItemId(mActivePosition));
hope it helps!
I suggest to store the id of the selected item when it is selected, and after order the dataset, search for that id and re-select it.
I have a custom listView that is empty in first time , from other side i have an activity which contains a product details such as picture, title, model .in this activity there is a button , so when user press this button i want to add the product into my custom listView and shows all the product details there,
my listView is:
public class ListViewShoppingBasketActivity extends Activity {
BaseAdapter adapter;
ArrayList<SingleRow> list;
Context context;
String[] titles;
Bitmap[] images;
String[] descriptions;
TextView title;
TextView description;
ImageView image;
String rowTitles;
String rowDescription;
Bitmap rowImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_shopping_basket_layout);
final ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new MyAdapter(this));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
if (id == 0) {
} else if (id == 1) {
} else if (id == 2) {
} else if (id == 3) {
} else if (id == 4) {
}
}
});
}
class SingleRow {
String rowTitles;
String rowDescription;
Bitmap rowImage;
public SingleRow(String rowTitles, String rowDescription, Bitmap rowImage) {
this.rowTitles = rowTitles;
this.rowDescription = rowDescription;
this.rowImage = rowImage;
}
}
class MyAdapter extends BaseAdapter {
public MyAdapter(Context c) {
context = c;
list = new ArrayList<SingleRow>();
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.shopping_basket_listview_row,
viewGroup, false);
title = (TextView) row.findViewById(R.id.textView1);
description = (TextView) row.findViewById(R.id.textView2);
image = (ImageView) row.findViewById(R.id.imageView1);
SingleRow temp = list.get(i);
title.setText(temp.rowTitles);
description.setText(temp.rowDescription);
image.setImageBitmap(temp.rowImage);
return row;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_view_shopping_basket, menu);
return true;
}
// // **********baraye add kardane item jadid be listview
public void addItems(View v) {
list.add(new SingleRow(rowTitles, rowDescription, rowImage));
adapter.notifyDataSetChanged();
}
}
and this is implementation of my button in other activity
b_add_to_cart = (Button) findViewById(R.id.b_add_to_cart);
b_add_to_cart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(BarcodeDetailsActivity.this,
ListViewShoppingBasketActivity.class);
intent.putExtra("productID", proId);
intent.putExtra("productModel", proModel);
intent.putExtra("productTitle", proTitle);
intent.putExtra("productPictureUrl", mIcon11);
startActivity(intent);
}
});
my problem is , i dont know how and where i can assign my product details in my listview
You can put the next lines to the code of the ListViewShoppingBasketActivity:
Intent i = getIntent();
if (i.getExtras() != null){
int prodId = i.getExtras().getInt("productID");
....
}
You can put theese to an ArrayList, and use an BaseAdapter (http://developer.android.com/reference/android/widget/BaseAdapter.html) to display the data in the list.