Arraylist reading the last element as the second last element - java

I have an Adapter Class that inflates items for a gridview. I am using an ArrayList to store certain details of the gridview item that is clicked. The problem that I am facing is that the last element is not recognised separately. If I click on the last element ,it is giving information about the element above it. Rest all other elements are fine.
public class MyAdaptertwo extends BaseAdapter
{
public List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
int j;
public static List<list> rowitems = new ArrayList<list>();
static int size=0;
public MyAdaptertwo(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("S", R.drawable.s,"1",R.drawable.v));
items.add(new Item("F", R.drawable.t,"2",R.drawable.v));
}
#Override
public int getCount()
{
return items.size();
}
#Override
public Object getItem(int i)
{
j=i;
return items.get(i);
}
public void setCost(int i,String a)
{
items.get(i).cost=a;
}
#Override
public long getItemId(int i)
{
return items.get(i).colorId;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
ImageView vn;
TextView name;
TextView costs;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.vnon, v.findViewById(R.id.vnon));
v.setTag(R.id.text, v.findViewById(R.id.text));
v.setTag(R.id.cost,v.findViewById(R.id.cost));
}
picture = (ImageView)v.getTag(R.id.picture);
vn = (ImageView)v.getTag(R.id.vnon);
name = (TextView)v.getTag(R.id.text);
costs = (TextView)v.getTag(R.id.cost);
Item item = (Item)getItem(i);
picture.setImageResource(item.colorId);
name.setText(item.name);
costs.setText(item.cost);
vn.setImageResource(item.vnId);
Button add=(Button)v.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
list item = new list(items.get(j - 1).colorId, items.get(j - 1).name, Integer.toString(co[j-1]), Integer.toString(no[j - 1]));
rowitems.add(item);
//something
}
});
Button sub=(Button)v.findViewById(R.id.sub);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//something
}
});
return v;
}
private class Item
{
final String name;
final int colorId;
String cost;
final int vnId;
Item(String name, int drawableId,String cost,int vnId)
{
this.name = name;
this.colorId = drawableId;
this.cost=cost;
this.vnId=vnId;
}
}
}

Use setTag and getTag method of View class, To get click Item object in onClick method.
Item item = (Item)getItem(i);
Button add=(Button)v.findViewById(R.id.add);
add.setTag(i);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (int)v.getTag();
//do something with item object
Item item = items.get(position);
list listItem = new list(item.colorId, item.name, Integer.toString(co[position]), Integer.toString(no[position]));
rowitems.add(listItem);
}
});

Related

BaseAdapter listview postion while scrolling

I have a ListView with many items. Each item has a button and when I click on it, it becomes invisible and another button becomes visible.
The issue is: when I scroll the ListView, many other items buttons are invisible. How can I fix it?
public class homebuyer_fruits_adapter extends BaseAdapter {
private ArrayList<Itemssetget> listData;
private LayoutInflater layoutInflater;
public static int cout=0;
List position_item=new ArrayList();
Context context;
String check;
int lastpostition=-1;
public static List<cartitemslist> cartlist=new ArrayList<>();
public homebuyer_fruits_adapter(Context aContext, ArrayList<Itemssetget> listData,String number) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
check=number;
this.notifyDataSetChanged();
}
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View v, ViewGroup parent) {
final ViewHolder holder;
final cartitemslist homeitemslist = new cartitemslist();
if (v == null) {
v = layoutInflater.inflate(R.layout.food_vegitablews_view, null);
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.name_item);
holder.price = (TextView) v.findViewById(R.id.price_item);
holder.add_q = (ImageView) v.findViewById(R.id.add_quantity);
holder.delete_q = (ImageView) v.findViewById(R.id.delete_quantity);
holder.quantity = (TextView) v.findViewById(R.id.add_quantity_items);
//holder.description = (TextView) v.findViewById(R.id.description_item);
//holder.id = (TextView) v.findViewById(R.id.);
holder.imageView = (ImageView) v.findViewById(R.id.image_items);
holder.discount = (TextView) v.findViewById(R.id.discout_price);
holder.add = (Button) v.findViewById(R.id.addto_cart);
holder.units=(TextView)v.findViewById(R.id.item_units);
holder.percentage=(TextView)v.findViewById(R.id.discount_percentage);
holder.linearLayou=(LinearLayout)v.findViewById(R.id.Quantity_control_linnear_view) ;
holder.phone=check;
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
this.notifyDataSetChanged();
}
int i=Integer.parseInt(listData.get(position).getPrice())-Integer.parseInt(listData.get(position).getDiscountprice());
int d=i*100;
if((d/Integer.parseInt(listData.get(position).getPrice()))==0)
{
holder.percentage.setVisibility(View.GONE);
}
holder.percentage.setText(""+d/Integer.parseInt(listData.get(position).getPrice())+"% OFF");
holder.name.setText(listData.get(position).getName());
holder.price.setText("Rs " + listData.get(position).getPrice());
if(listData.get(position).getPrice().equals(listData.get(position).getDiscountprice()))
{
holder.price.setVisibility(View.GONE);
}
holder.price.setPaintFlags(holder.price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.discount.setText("Rs " + listData.get(position).getDiscountprice());
Picasso.with(context).load(listData.get(position).getImageurl())
.fit().centerCrop().into(holder.imageView);
holder.units.setText(listData.get(position).getUnits());
holder.add_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context,listData.get(position).getName(),Toast.LENGTH_LONG).show();
if(H.containsKey(position))
{
holder.quantity.setText(""+(1+Integer.parseInt(holder.quantity.getText().toString())));
cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());
}
}
});
holder.delete_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(H.containsKey(position)) {
if(holder.quantity.getText().toString().equals("1"))
{
holder.add.setVisibility(View.VISIBLE);
holder.linearLayou.setVisibility(View.GONE);
int i=H.get(position);
cartlist.remove(i);
cout--;
Fruits.numberofitems.setText(cout + "");
}
else {
holder.quantity.setText("" + (Integer.parseInt(holder.quantity.getText().toString()) - 1));
cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());
}
}
}
});
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context,"Add to cart"+holder.name.getText(),Toast.LENGTH_SHORT).show();
holder.add.setVisibility(View.GONE);
holder.linearLayou.setVisibility(View.VISIBLE);
// check=listData.get(position).getPhonenumber_seller();
if(cartlist.isEmpty()) {
cout++;
homeitemslist.setName(holder.name.getText().toString());
homeitemslist.setPrice(holder.price.getText().toString());
homeitemslist.setDiscountp(holder.discount.getText().toString());
homeitemslist.setQuantity(holder.quantity.getText().toString());
homeitemslist.setPhone_id(holder.phone);
homeitemslist.setImage(listData.get(position).getImageurl());
if(Fruits.numberofitems!=null)
{
Fruits.numberofitems.setText(cout + "");
}
shop_items.numberofitems.setText(cout + "");
buyer_home.numberofitems.setText(cout + "");
cartlist.add(homeitemslist);
H.put(position,cartlist.indexOf(homeitemslist));
}
else {
if(check.equals(cartlist.get(0).getPhone_id()))
{
cout++;
cartitemslist homeitemslist = new cartitemslist();
homeitemslist.setName(holder.name.getText().toString());
homeitemslist.setPrice(holder.price.getText().toString());
homeitemslist.setDiscountp(holder.discount.getText().toString());
homeitemslist.setQuantity(holder.quantity.getText().toString());
homeitemslist.setPhone_id(holder.phone);
homeitemslist.setImage(listData.get(position).getImageurl());
if(Fruits.numberofitems!=null)
{
Fruits.numberofitems.setText(cout + "");
}
shop_items.numberofitems.setText(cout + "");
buyer_home.numberofitems.setText(cout + "");
cartlist.add(homeitemslist);
H.put(position,cartlist.indexOf(homeitemslist));
}
else
{
Toast.makeText(context,"Clear the Previous cart First than you can buy from this shop",Toast.LENGTH_LONG).show();
}
}
}
});
// holder.description.setText(listData.get(position).getDiscription());
// holder.id.setText(listData.get(position).getId());
//holder.imageView.setImageAlpha(R.drawable.banana);
setanimation(v,position);
return v;
}
static class ViewHolder {
LinearLayout linearLayou;
TextView name;
TextView price;
TextView discount;
ImageView add_q;
ImageView delete_q;
TextView quantity;
ImageView imageView;
TextView description;
TextView units;
TextView percentage;
String phone;
Button add;
}
public void setanimation(View viewanim,int position)
{
if(position>lastpostition)
{
ScaleAnimation animation=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(1500);
viewanim.setAnimation(animation);
lastpostition=position;
}
}
}
Here is my adapter code.
I tried many ways but still stuck on this issue.
You set the visibility only in the OnClickListener, but you need to set it in getView() too, because your views will get recycled (re-used for a different item) when you scroll.
So in getView() you have to restore the complete state of the view, including button visibility. Store all state information in your list items. Inside your getView() do something like this:
#Override
public View getView(final int position, View v, ViewGroup parent) {
...
// the view (v) could be recycled and showing a state from another item
// e.g. the add button is visible
final Itemssetget listItem = listData.get(position);
// so we have to restore the state of the view for listItem above
// e.g. update the button visibility
holder.add.setVisibility(listItem.isAddButtonVisible() ? View.VISIBLE : View.GONE);
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
holder.add.setVisibility(View.GONE);
// save the state of the button, so it can be restored later
listItem.setAddButtonVisible(true);
...
}
}
...
}
Add the necessary member variables and getter/setter functions in Itemssetget:
public class Itemssetget {
...
private boolean isAddButtonVisible = false;
public boolean isAddButtonVisible() {
return isAddButtonVisible;
}
public void setAddButtonVisible(boolean isVisible) {
this.isAddButtonVisible = isVisible;
}
...
}
PS: Your code looks terrible. Have a look at naming conventions.

Add and remove custom item to listview

Currently I'm adding a custom row (contains and edit text and a plus and minus for adding/removing a row) to a list view. However, each time a row is added the contents from all edit texts are wiped.
public class ingredientView extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public ingredientView(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.ingredient_item, null);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, loadedFood);
//Handle TextView and display string from your list
InstantAutoComplete listItemText = (InstantAutoComplete) view.findViewById(R.id.recipe_ingredients);
listItemText.setAdapter(adapter);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
ImageButton deleteBtn = (ImageButton)view.findViewById(R.id.ingredient_minus_button);
ImageButton addBtn = (ImageButton)view.findViewById(R.id.ingredient_plus_button);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
if (list.size()>1){
list.remove(position); //or some other task
notifyDataSetChanged();}
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.add(position+1,"");
notifyDataSetChanged();
}
});
return view;
}
private static final String[] loadedFood = new String[]{
"eggs","milk","butter","flour"
};
}

Hide or Show checkboxes inside Custom grid adapter for all Positions

I have to hide all the checkboxes for every [Position] product until User click button. When ever user clicks button check boxes will be show to select items for delete. Only check box will appear to change not whole grid view.
public class CartAdapter extends BaseAdapter {
Context context;
ArrayList<ProductCount> productCounts;
private LayoutInflater inflater;
private ImageButton plusButton;
private ImageButton minusButton;
private CheckBox selectToDelete;
private onDeleteCartItem onDeleteCartItem = null;
public CartAdapter(Context context, ArrayList<ProductCount> productCounts, onDeleteCartItem selectChangeListener) {
this.context = context;
this.productCounts = productCounts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.onDeleteCartItem = selectChangeListener;
}
#Override
public int getCount() {
if(productCounts!=null)
return productCounts.size();
return 0;
}
#Override
public Object getItem(int position) {
if(productCounts!=null && position >=0 && position<getCount())
return productCounts.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(productCounts!=null && position >=0 && position<getCount()){
ProductCount temp = productCounts.get(position);
return productCounts.indexOf(temp);
}
return 0;
}
public class ProductsListHolder{
public ImageView cart_item_img;
public TextView cart_item_desc;
public TextView cart_item_count;
public TextView cart_item_price_tag;
public TextView cart_item_price;
public ImageButton cart_item_minus;
public ImageButton cart_item_plus;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ProductsListHolder productsListHolder;
if(view == null){
view = inflater.inflate(R.layout.cart_adapter, parent, false);
productsListHolder = new ProductsListHolder();
productsListHolder.cart_item_img = (ImageView) view.findViewById(R.id.cart_item_img);
productsListHolder.cart_item_desc = (TextView) view.findViewById(R.id.cart_item_desc);
productsListHolder.cart_item_count = (TextView) view.findViewById(R.id.cart_item_count);
productsListHolder.cart_item_price_tag = (TextView) view.findViewById(R.id.cart_item_price_tag);
productsListHolder.cart_item_price = (TextView) view.findViewById(R.id.cart_item_price);
plusButton = (ImageButton) view.findViewById(R.id.cart_item_plus);
minusButton = (ImageButton) view.findViewById(R.id.cart_item_minus);
selectToDelete = (CheckBox) view.findViewById(R.id.select_to_delete);
selectToDelete.setTag(position);
view.setTag(productsListHolder);
}
else{
productsListHolder = (ProductsListHolder) view.getTag();
}
final ProductCount cat = productCounts.get(position);
selectToDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(onDeleteCartItem != null){
onDeleteCartItem.onSelectToDelete((Integer)buttonView.getTag(),isChecked);
}
}
}
});
minusButton.setOnClickListener(new View.OnClickListener() {
int itemcount = 0;
#Override
public void onClick(View v) {
itemcount = productCounts.get(position).getCount();
productCounts.get(position).setCount(itemcount-1);
setProduct(position,productsListHolder,cat);
}
});
plusButton.setOnClickListener(new View.OnClickListener() {
int itemcount = 0;
#Override
public void onClick(View v) {
itemcount = productCounts.get(position).getCount();
productCounts.get(position).setCount(itemcount+1);
setProduct(position,productsListHolder,cat);
}
});
setProduct(position,productsListHolder,cat);
return view;
}
private void setProduct(int position, final ProductsListHolder productsListHolder, ProductCount pCount) {
Picasso.with(context).load(pCount.products.getImageResours()).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
productsListHolder.cart_item_img.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
}
});
productsListHolder.cart_item_desc.setText(pCount.getProducts().getDescription());
productsListHolder.cart_item_price_tag.setText((String.valueOf(pCount.getCount()).concat(" x Rs. ").concat(String.valueOf((pCount.products.getPrice())))));
productsListHolder.cart_item_price.setText("Rs. ".concat(String.valueOf(pCount.getCount()* pCount.products.getPrice())));
productsListHolder.cart_item_count.setText(String.valueOf(pCount.getCount()));
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
Just handling your checkBox in your adapter because you got the position then can hide/show, OnClickListener, OnCheckedChangeListener however you want.
Reference to my answer here: Is there a simple way to check all CheckBox items in custimize BaseAdapter in android?

Delete checked item from list view and data from database, using adapter class

public class delete extends BaseAdapter {
SQLiteDatabase db;
String am;
private final Context c;
private final ArrayList<String> str;
private final String[] number;
ArrayList<Integer> checks=new ArrayList<Integer>();
public delete(Context c, ArrayList<String> str, String[] number, ArrayList<Integer> checks) {
this.c = c;
this.str = str;
this.number = number;
this.checks=checks;
}
#Override
public int getCount() {
return str.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View ListView ;
if(convertView==null)
{
ListView=new View(c);
ListView=inflater.inflate(R.layout.button,null);
TextView textview=(TextView) ListView.findViewById(R.id.textView6);
textview.setText(str.get(position));
}
else
{
ListView=(View)convertView;
}
Button btnupdate=(Button)ListView.findViewById(R.id.button4);
//checkbox declaration
CheckBox chk=(CheckBox)ListView.findViewById(R.id.chk1);
btnupdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db=c.openOrCreateDatabase("Student",Context.MODE_PRIVATE, null);
String num = number[position];
Cursor cc=db.rawQuery("select mobile from detail where mobile='"+num+"'", null);
if (cc.moveToFirst()) {
do {
am = cc.getString(cc.getColumnIndex("mobile"));
} while (cc.moveToNext());
}
Intent i=new Intent(c,Update.class);
i.putExtra("value",am);
c.startActivity(i);
}
});
return ListView;
}
}
//It show all the data in list view with update button and check box in list view. There is delete button which is suppose to delete all the item which is checked.
//Delete button is on main activity where list view is added.
You can create a method in your DataBase class and pass the table name and entry to be deleted along with id if any.Call the method at onClick of ListView item.
public void deleteEntry(String tableName,String columnName,String id)
{
openDatabase();
database.delete(tableName, columnName + " = ?",new String[] { id });
closeDatabase();
}

Onclick listner to expandable listview

I have created an expandable listview, but the onclick listener to the child list items could not be attached.
The activity code:
public class MyActivity extends Activity {
private ExpandableListView mExpandableList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
for (int i = 0; i < 2; i++){
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
for (int j = 0; j < 3; j++) {
arrayChildren.add("Child " + j);
}
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
}
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
}
}
The custom adapter for the lists:
public class MyCustomAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
public MyCustomAdapter(Context context, ArrayList<Parent> parent){
mParent = parent;
inflater = LayoutInflater.from(context);
}
#Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
#Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
#Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
#Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
//return the entire view
return view;
}
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
//return the entire view
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
}
The parent class:
public class Parent {
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public ArrayList<String> getArrayChildren() {
return mArrayChildren;
}
public void setArrayChildren(ArrayList<String> mArrayChildren) {
this.mArrayChildren = mArrayChildren;
}
}
What should I do to add onclick listener to the child list items?
Add this after you setAdapter of the Expandable list
mExpandableList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
/* You must make use of the View v, find the view by id and extract the text as below*/
TextView tv= (TextView) v.findViewById(R.id.childTextView);
String data= tv.getText().toString();
return true; // i missed this
}
});
You need to add
ChildClickListener
like this : mExpandableList.setOnChildClickListener
add this line to onCreate method
read here
also this is a good example
Declare your listView with an overridden onChildClick
ExpandableListView listView = getExpandableListView();
listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition,long id) {
Log.d(TAG,"I got clicked childPosition:["+childPosition+"] groupPosition:["+groupPosition+"] id:["+id+"]");
return true;
}
});

Categories

Resources