Iterate quantity for items , custom adapter - android

I have a listview where every items have one TextView and 2 Buttons ( + and - ). I want to iterate the quantity from TextView when I press the + Button. After this, I need to sum up all items (price * item quantity) for every item from the list and put the result into new TextView(total cost). That is my adapter and activity main. I appreciate any help.
public class cartItemsAdapter extends BaseAdapter {
private ArrayList<items> itemsList;
private Context context;
public cartItemsAdapter(ArrayList<items> list,Context cont){
this.itemsList = list;
this.context=cont;
}
#Override
public int getCount() {
return this.itemsList.size();
}
#Override
public Object getItem(int position) {
return this.itemsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.list_cart_item_view,null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.icon_item);
holder.nameView = (TextView) convertView.findViewById(R.id.name_item);
holder.priceView=(TextView)convertView.findViewById(R.id.price_item);
holder.plusButton = (Button) convertView.findViewById(R.id.cart_plus_button);
holder.minusButton = (Button) convertView.findViewById(R.id.cart_minus_button);
holder.removeButton = (Button) convertView.findViewById(R.id.cart_remove_button);
holder.quantityView = (TextView) convertView.findViewById(R.id.cart_quantity_view);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
items itm = itemsList.get(position);
holder.imageView.setImageResource(itm.getlImageId());
holder.nameView.setText(itm.getlName());
holder.priceView.setText(itm.getlPrice() + " $");
holder.quantityView.setText("1");
/*holder.plusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pquant=Integer.valueOf(quant.toString());
pquant++;
}
});
holder.minusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int mquant=Integer.valueOf(quant.toString());
mquant--;
}
});*/
return convertView;
}
private static class ViewHolder{
public ImageView imageView;
public TextView nameView;
public TextView priceView;
public Button plusButton;
public Button minusButton;
public Button removeButton;
public TextView quantityView;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
plusBtn = findViewById(R.id.cart_plus_button);
minusBtn = findViewById(R.id.cart_minus_button);
removeBtn = findViewById(R.id.cart_remove_button);
quantity = findViewById(R.id.cart_quantity_view);
listView = (ListView) findViewById(R.id.cart_category_listview);
favoriteDbHelper = new DatabaseHelper(this);
item = favoriteDbHelper.getAllDataCart();
myAdapter = new cartItemsAdapter(item, this);
listView.setAdapter(myAdapter);
}

Actually this code of yours is pretty close:
holder.plusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pquant=Integer.valueOf(quant.toString());
pquant++;
}
});
holder.minusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int mquant=Integer.valueOf(quant.toString());
mquant--;
}
});
All you need to do is to access itemsList inside onClick(View v). To make it simple, try this:
holder.plusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Items item = itemsList.get(position);
//now you can get your quant by accessing this item object
}
});
The key is to get current position using getAdapterPosition().

Related

Add an onclicklistener to every row from a listview (android)

I've fetched and parsed a JSON data from an URL and displayed it in ListView but I can't seem to add an onclicklistener on every row I just want to be able to do an action when I click on the row, with the data that the row contains to get all informations of this row in an other activity ?
when i launch code, the app stopped immediatly !!! thanks
public class DemandeAdapter extends BaseAdapter {
private Context context;
private ArrayList<DemandeModel> demandeModelArrayList;
public DemandeAdapter(Context context, ArrayList<DemandeModel> demandeModelArrayList) {
this.context = context;
this.demandeModelArrayList = demandeModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return demandeModelArrayList.size();
}
#Override
public Object getItem(int position) {
return demandeModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.demande, null, true);
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
holder.id = (TextView) convertView.findViewById(R.id.id);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.desc = (TextView) convertView.findViewById(R.id.desc);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.listView = (ListView) convertView.findViewById(R.id.lv);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
Picasso.get().load(demandeModelArrayList.get(position).getImgURL()).into(holder.iv);
holder.id.setText("ID : "+demandeModelArrayList.get(position).getId());
holder.date.setText("Date : "+demandeModelArrayList.get(position).getDate());
holder.name.setText("Name : "+demandeModelArrayList.get(position).getNom());
holder.desc.setText("Description : "+demandeModelArrayList.get(position).getDescription());
holder.listView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, demandeDetails.class);
intent.putExtra("ID",demandeModelArrayList.get(position).getId());
intent.putExtra("Date",demandeModelArrayList.get(position).getDate());
intent.putExtra("Name",demandeModelArrayList.get(position).getNom());
intent.putExtra("Description",demandeModelArrayList.get(position).getDescription());
intent.putExtra("Image",demandeModelArrayList.get(position).getImgURL());
context.startActivity(intent);
}
});
return convertView;
}
private class ViewHolder {
protected TextView id,name,desc,date;
protected ImageView iv;
private ListView listView;
}
}
i do that min my adapter class not in my activity !!!
And this is the other activity where i want to display data
public class demandeDetails extends AppCompatActivity implements Serializable {
protected TextView id,name,desc,date;
protected ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demande_details);
id = (TextView)findViewById(R.id.demande_Id);
name = (TextView)findViewById(R.id.demande_Name);
desc = (TextView)findViewById(R.id.demande_Desc);
date = (TextView)findViewById(R.id.demande_Date);
img = (ImageView)findViewById(R.id.picture_demande);
Intent intent = getIntent();
int imagee = intent.getExtras().getInt("Image");
String idd = intent.getExtras().getString("ID");
String namee = intent.getExtras().getString("Name");
String descc = intent.getExtras().getString("Description");
String datee = intent.getExtras().getString("Date");
img.setImageResource(imagee);
id.setText(idd);
name.setText(namee);
desc.setText(descc);
date.setText(datee);
}
}
You can add onItemClickListener just like below
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(this,"You selected : " + position,Toast.LENGTH_SHORT).show();
}
});
You need to set an item listener on the listview
Listview listView = findViewById(R.id.listview);
listView.setOnItemClickListener(new
OnItemClickListener() {
#Override
protected void onListItemClick(ListView l, View v, int
position, long id) {
Log.i("Hello!", "Clicked! YAY!");
});
setOnClickListener on convertView.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, demandeDetails.class);
intent.putExtra("ID",demandeModelArrayList.get(position).getId());
intent.putExtra("Date",demandeModelArrayList.get(position).getDate());
intent.putExtra("Name",demandeModelArrayList.get(position).getNom());
intent.putExtra("Description",demandeModelArrayList.get(position).getDescription());
intent.putExtra("Image",demandeModelArrayList.get(position).getImgURL());
context.startActivity(intent);
}
});

Values out of ScrollView not get accessed to the Fragment from RecyclerAdapter

I have two buttons and one textview in each child row of Recyclerview. Those two buttons are used as triggers to count number and then write the number in textview.
Issue: Text-view value is not Updated while scrolling and the item-count only give the count of items in the list which is visible.
public class RecyclerAdapterItem extends RecyclerView.Adapter<RecyclerAdapterItem.ItemViewHolder> {
private LayoutInflater inflater;
private Context ctx;
public RecyclerAdapterItem(Context ctx) {
inflater = LayoutInflater.from(ctx);
this.ctx = ctx;
}
#Override
public RecyclerAdapterItem.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.recyclerviewitem_item_tab, parent, false);
ItemViewHolder holder = new ItemViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final RecyclerAdapterItem.ItemViewHolder holder, int position) {
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
holder.Itemname.setText(ItemFragment.itemArrayList.get(position).getItem_name().toString());
Float rt=Float.valueOf(ItemFragment.itemArrayList.get(position).getRate());
holder.Rate.setText(String.valueOf(df.format(rt)));
holder.qty.setText(String.valueOf(ItemFragment.itemArrayList.get(position).getQty()));
}
#Override
public int getItemCount() {
return ItemFragment.itemArrayList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected Button btn_plus, btn_minus;
private TextView Itemname,Rate, qty;
public ItemViewHolder(View itemView) {
super(itemView);
Itemname = (TextView) itemView.findViewById(R.id.Itemname);
Rate=(TextView) itemView.findViewById(R.id.Rate);
qty = (TextView) itemView.findViewById(R.id.Qty);
btn_plus = (Button) itemView.findViewById(R.id.plus);
btn_minus = (Button) itemView.findViewById(R.id.minus);
btn_plus.setTag(R.integer.btn_plus_view, itemView);
btn_minus.setTag(R.integer.btn_minus_view, itemView);
btn_plus.setOnClickListener(this);
btn_minus.setOnClickListener(this);
}
// onClick Listener for view
#Override
public void onClick(View v) {
if (v.getId() == btn_plus.getId()){
View tempview = (View) btn_plus.getTag(R.integer.btn_plus_view);
TextView tv = (TextView) tempview.findViewById(R.id.Qty);
int number = Integer.parseInt(tv.getText().toString()) + 1;
tv.setText(String.valueOf(number));
ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
} else if(v.getId() == btn_minus.getId()) {
View tempview = (View) btn_minus.getTag( R.integer.btn_minus_view);
TextView tv = (TextView) tempview.findViewById(R.id.Qty);
int number = Integer.parseInt(tv.getText().toString()) - 1;
if(number<0)
number =0;
tv.setText(String.valueOf(number));
ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
}
}
}
}
This is the recycleradapter code.The mentioned textview is qty.
If your code is working then only try this method and try to check update..
notifyDataSetChanged();
other wise make interfaces to puls and mins value update like below code ..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(Item str);//pass your object types.
}
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
Item item=mDataset.get(position);
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.setPlus(true);
onItemClickListner.onClick(data);
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.setMinus(true);
onItemClickListner.onClick(data);
}
});
}
then after define adapter object and bind into recylcer view then call below code ..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
#Override
public void onClick(Item str) {
if (str.getPlus()){
str.setQty();// set your logic
recyclerViewAdpater.notifyDataSetChanged();
}
if (str.getMinus()){
str.setQty();// set your logic
recyclerViewAdpater.notifyDataSetChanged();
}
}
});
Move the click listeners for the buttons to BindView method
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDataset.get(position).setQty(mDataset.get(position).getQty() + 1);
holder.qty.setText(String.valueOf(mDataset.get(position).getQty()));
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDataset.get(position).getQty()==0)
{
holder.qty.setText(String.valueOf(0));
}
else
{
mDataset.get(position).setQty(mDataset.get(position).getQty() - 1);
holder.qty.setText(String.valueOf(mDataset.get(position).getQty()));
}
}
});
}

how to set value for each item button increase and decrease click and set that value in text view

public class MyCustomAdapter extends BaseAdapter {
Context context;
String[] result;
int[] imageId;
String[] Cost;
static int counter = 0;
private static LayoutInflater inflater = null;
public MyCustomAdapter(Context mainActivity, String[] result, int[] imageId, String[] Cost
) {
// TODO Auto-generated constructor stub
this.result = result;
this.imageId = imageId;
this.Cost = Cost;
this.context = mainActivity;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
TextView cost;
ImageView img;
ImageView plusitem;
ImageView minusitem;
TextView itemnumber;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.listitem, null);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
holder.cost = (TextView) rowView.findViewById(R.id.textView2);
holder.plusitem = (ImageView) rowView.findViewById(R.id.imageButton2);
holder.minusitem = (ImageView) rowView.findViewById(R.id.imageButton);
holder.itemnumber = (TextView) rowView.findViewById(R.id.textView4);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
holder.plusitem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.itemnumber.setText(String.valueOf( _counter));
//holder.itemnumber.setText(result[position]);
}
});
holder.minusitem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.itemnumber.setText(String.valueOf( _counter));
// holder.itemnumber.setText(result[position]);
}
});
return rowView;
}}
This is my adapter we have +button for increase item and - button
for decrease item i have to get item number like in first item
if we have 2 item and second item we can 3 ,4 .. so on..
actuality i want to add item on cart actually i
have tried with static variable its not working Properly
please tell me logic . look my screen below
Create the Buttons and the TextView as shown in your design.Write onclick methods for the Buttons.Initialize an Integer variable for saving the count.In the click of + button,increment the count by one and set the text in Textview.Similarly,when you click the - button,decrement the count by one and set the text in your TextView.
int count=0;
TextView txtCount =(TextView) findViewById(R.id.txt);
Button buttonInc= (Button) findViewById(R.id.button1);
Button buttonDec= (Button) findViewById(R.id.button2);
buttonInc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
txtCount.setText(String.valueOf(count));
}
});
holder.cart_minus_img.setOnClickListener(new View.OnClickListener() {
//1. List item
#Override
public void onClick(View v) {
int count= Integer.parseInt(String.valueOf(holder.cart_product_quantity_tv.getText()));
if(count>0){
//2. enter code here
count--;
holder.cart_product_quantity_tv.setText(String.valueOf(count));
}
}
});
holder.cart_plus_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int count= Integer.parseInt(String.valueOf(holder.cart_product_quantity_tv.getText()));
count++;
holder.cart_product_quantity_tv.setText(String.valueOf(count));
}
});
Use a hash map.For every item clicked store the item id as the key and the clicked count as the value.
For eg:<Item_id:item_count>
This way you can easily manage the user's selection and also its more efficient than using static variables as you need to have only a single hashmap .
First of all you should maintain one arraylist of custom class having fields like ItemId, ItemName, Price, Quantity, Image and so on. So that you can easily maintain quantity and other details in shopping cart.
Now in adapter you just need to pass this arrayList, no need of passing different Arrays.
int quantity = arraylist.get(position).getItemQuantity();
btnIncrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
quantity++;
tvQuantity.setText(quantity+"");
}
});
btnDecrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
quantity--;
tvQuantity.setText(quantity+"");
}
});
This works perfectly...
This is the Adapter add to cart in this you can see on btnIncrease and btnDecrease in that i change the value of cart and i use notifyDataSetChanged(); it will change the data automatically
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, cart, genre;
public Button btnIncrease,btnDecrease;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
cart = (TextView) view.findViewById(R.id.cart);
btnIncrease =view.findViewById(R.id.plus);
btnDecrease = view.findViewById(R.id.minus);
}
}
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Movie movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.cart.setText(String.valueOf(movie.getcart()));
holder.btnIncrease.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
movie.cart++;
notifyDataSetChanged();
}
});
holder.btnDecrease.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (movie.cart>0)
movie.cart--;
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return moviesList.size();
}
}

android implement multiple click event in ListView item

I wanna to implement multiple button in my ListView items. But the problem is that i couldn't handle click event when button is clicked, i mean they are Play button and Image button like this
Here is my Adapter:
public class ArtistsAdapter extends BaseAdapter implements SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private LayoutInflater inflater;
private int resId;
private Context context;
private ArrayList<Artist> artistList;
public ArtistsAdapter(Context context, int resId, ArrayList<Artist> artistList) {
this.resId = resId;
this.context = context;
this.artistList = artistList;
this.inflater = LayoutInflater.from(context);
}
#Override
public Object[] getSections() {
return sections;
}
#Override
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
#Override
public int getSectionForPosition(int position) {
return 1;
}
private static class ViewHolder {
ImageView artistImgItem;
ImageButton artistPlayItem;
TextView artistNameItem;
TextView artistAgeItem;
LinearLayout artistItem;
}
#Override
public int getCount() {
return artistList.size();
}
#Override
public Object getItem(int position) {
return artistList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Artist artist = (Artist) getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = this.inflater.inflate(resId, parent, false);
viewHolder = new ViewHolder();
viewHolder.artistImgItem = (ImageView) convertView.findViewById(R.id.artistImg);
viewHolder.artistPlayItem = (ImageButton) convertView.findViewById(R.id.artistPlay);
viewHolder.artistNameItem = (TextView) convertView.findViewById(R.id.artistName);
viewHolder.artistAgeItem = (TextView) convertView.findViewById(R.id.artistAge);
viewHolder.artistItem = (LinearLayout) convertView.findViewById(R.id.artistItem);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
viewHolder.artistNameItem.setText(artist.getName());
viewHolder.artistPlayItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("aaaaa", position+"ssss");
Toast.makeText(context, "Music playin " + position+1 +"", Toast.LENGTH_SHORT).show();
}
});
viewHolder.artistItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ArtistsActivity) context).showArtistView();
}
});
viewHolder.artistImgItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ArtistsActivity) context).showArtistView();
}
});
return convertView;
}
}
And my activity looks like that
public class ArtistsActivity extends Activity {
private IndexableListView artistListUI;
private ImageView maleArtist, femaleArtist, groupArtist, artistImg;
private ArtistsAdapter artistsAdapter;
private ArrayList<Artist> maleArtistList, femaleArtistList, groupArtistList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_list);
artistListUI = (IndexableListView) findViewById(R.id.artistListView);
maleArtist = (ImageView) findViewById(R.id.maleArtist);
femaleArtist = (ImageView) findViewById(R.id.femaleArtist);
groupArtist = (ImageView) findViewById(R.id.groupArtist);
artistImg = (ImageView) findViewById(R.id.artistImg);
artistListUI.setFastScrollEnabled(true);
artistsAdapter = new ArtistsAdapter(ArtistsActivity.this, R.layout.artist_item, artistList);
artistListUI.setAdapter(artistsAdapter);
public void showArtistView() {
startActivity(new Intent(ArtistsActivity.this, AlbumsActivity.class));
}
}
you have to set clickable property of listview to false
because of entire listview click is fire
This is resolved in the adapter. Look at this example:
https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/tracks/TrackAdapter.java
In the bindView or getView method you have to define your clickhandler linked to your view:
holder.hLayout.setOnClickListener(new MentionClickhandler());
And do sour stuff in this function:
public class MentionClickhandler implements View.OnClickListener
{
public void onClick( View view ){
Track trackTag = new Track();
trackTag = (Track) view.getTag();
Log.i(TAG, "item: "+trackTag.getSearchTrack());
Bundle bundle = new Bundle();
bundle.putInt("idTrack", trackTag.getIdTrack());
bundle.putString("searchTrack", trackTag.getSearchTrack());
bundle.putInt("topTrack", trackTag.getTopTrack());
bundle.putLong("lastId", trackTag.getLastId());
bundle.putInt("count", trackTag.getCount());
bundle.putString("type", "LastMentions");
Intent i = new Intent(context, GDMentionsList.class);
i.putExtras(bundle);
context.startActivity(i);
}
}
Hope it helps...

Custom NumberPicker buttons.onClickListener

One of my activities requires a list view with custom number pickers in each row.
I created the number picker as suggested here. When I click on the buttons, however, the counter doesn't increment or decrement at all.
The following is the code found at my Custom ListView Adapter.
public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
private ArrayList<MaterialClass> data;
public static LayoutInflater inflater = null;
public static Dialog dialog;
String materialName;
public MaterialListViewAdapter(Context applicationContext,
int materialdialogcontent, ArrayList<MaterialClass> materials)
{
this.data = materials;
inflater = (LayoutInflater)applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return data.size();
}
#Override
public Object getItem(int position)
{
return position;
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
//ViewHolder holder = new ViewHolder();
if(vi == null)
{
holder = new ViewHolder();
vi = inflater.inflate(R.layout.materialdialogcontent, null);
//Initialize Buttons and TextViews.
holder.num.setText("0");
holder.add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
holder.num.setText("" + counter);
}
});
holder.sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
holder.sub.setText("" + counter);
}
});
//holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
//holder.pk.setMaxValue(20);
//holder.pk.setMinValue(0);
return vi;
}
private class ViewHolder
{
TextView txt;
Button add;
Button sub;
TextView num;
//NumberPicker pk;
}
}
When I try debugging to see what's going on, counter doesn't even initialize with '0'.
I tried initializing the counter in the getView() method, however in order to do so I need to set the counter to final, and then another error shows up saying
The final local variable counter cannot be assigned, since it is defined in an enclosing type.
Any suggestions?
Updated for additional code:
public class Material extends Activity
{
ArrayList<String> materialList;
ListView lv;
Button btnConfirm;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_material);
lv = (ListView)findViewById(R.id.list);
btnConfirm = (Button)findViewById(R.id.btnConfirm);
materialList = new ArrayList<String>();
Intent i = getIntent();
materialList = i.getStringArrayListExtra("materialList");
if(materialList != null)
{
Toast t = Toast.makeText(getApplicationContext(), materialList.get(0).toString(), Toast.LENGTH_LONG);
t.show();
}
else
{
//FILL HERE LATER
}
ArrayList<MaterialClass> materials = new ArrayList<MaterialClass>();
for(String temp : materialList)
{
MaterialClass m = new MaterialClass();
m.setName(temp);
materials.add(m);
}
MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
lv.setAdapter(adapter);
}
}
You should implement the onItemClickListener for your listView . You are using listener in adapter class but practically those numbers are nothing but items of that listView, so that to operate on those buttons (listView's items) you've not defined onItemClickListener. So after setting adapter write
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View item, int position, long id) {
// here you are getting position of clicked item
}
}
UPDATE:
I've gone through your code again and I found these:
1.you are writing this line:
MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
but I didn't see any constructor in your Adapter.
2.As you are extending BaseAdapter, you must override getCount() , getItem(int position) and getItemId(int position) method as like getView() . There you are getting issue.
Try this :
public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
//ViewHolder holder = new ViewHolder();
if(vi == null)
{
holder = new ViewHolder();
vi = inflater.inflate(R.layout.materialdialogcontent, null);
//Initialize Buttons and TextViews.
//holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
//holder.pk.setMaxValue(20);
//holder.pk.setMinValue(0);
holder.add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
holder.num.setText("" + counter);
}
});
holder.sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
holder.num.setText("" + counter);
}
});
return vi;
}
private class ViewHolder
{
TextView txt;
Button add;
Button sub;
TextView num;
//NumberPicker pk;
}
}

Categories

Resources