I am inflating layout dynamically according size of my list. And now whenever i click on that dynamic view i have to get position of clicked child view. I am trying this code but my bad luck...
LinearLayout layoutViewDealsList = (LinearLayout) view.findViewById(R.id.baseLinearLayoutSiteHistory);
txtViewNoDeals = (TextView) view.findViewById(R.id.txtViewNoDeals);
if(dealsList != null && dealsList.size() > 0) {
for(int index = 0; index < dealsList.size(); index++) {
txtViewNoDeals.setVisibility(View.GONE);
LayoutInflater inflater = (LayoutInflater) profileActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.deal_row, null);
txtViewDealsPrice = (TextView) layoutView.findViewById(R.id.txtViewDealPrice);
txtViewDealsTitle = (TextView) layoutView.findViewById(R.id.txtViewDealTitle);
txtViewDealsDescription = (TextView) layoutView.findViewById(R.id.txtViewDealDescription);
txtViewDealsBuyer = (TextView) layoutView.findViewById(R.id.txtViewDealBuyer);
txtViewDealsDate = (TextView) layoutView.findViewById(R.id.txtViewDealDate);
txtViewDealsPrice.setText("Price: $" + dealsList.get(index).getDeal_amount());
txtViewDealsTitle.setText(dealsList.get(index).getTitle());
txtViewDealsDescription.setText(dealsList.get(index).getDesc());
layoutViewDealsList.addView(layoutView);
int position = layoutViewDealsList.indexOfChild(layoutView);
layoutViewDealsList.setTag(position);
appDelegate.setDealsList(dealsList);
}
}
layoutViewDealsList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();//layoutViewDealsList.indexOfChild(layoutView);
Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();
}
});
I am unable to know that where i am doin wrong.
You need to apply onClickListener in your layoutView inside loop.
LinearLayout layoutViewDealsList = (LinearLayout) view.findViewById(R.id.baseLinearLayoutSiteHistory);
txtViewNoDeals = (TextView) view.findViewById(R.id.txtViewNoDeals);
if(dealsList != null && dealsList.size() > 0) {
for(int index = 0; index < dealsList.size(); index++) {
txtViewNoDeals.setVisibility(View.GONE);
LayoutInflater inflater = (LayoutInflater) profileActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.deal_row, null);
txtViewDealsPrice = (TextView) layoutView.findViewById(R.id.txtViewDealPrice);
txtViewDealsTitle = (TextView) layoutView.findViewById(R.id.txtViewDealTitle);
txtViewDealsDescription = (TextView) layoutView.findViewById(R.id.txtViewDealDescription);
txtViewDealsBuyer = (TextView) layoutView.findViewById(R.id.txtViewDealBuyer);
txtViewDealsDate = (TextView) layoutView.findViewById(R.id.txtViewDealDate);
txtViewDealsPrice.setText("Price: $" + dealsList.get(index).getDeal_amount());
txtViewDealsTitle.setText(dealsList.get(index).getTitle());
txtViewDealsDescription.setText(dealsList.get(index).getDesc());
layoutViewDealsList.addView(layoutView);
int position = layoutViewDealsList.indexOfChild(layoutView);
layoutViewDealsList.setTag(position);
appDelegate.setDealsList(dealsList);
layoutView.setOnClickListener(this); <--------------- setOnClickListener
}
}
And your onClick()
looks like..
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();
}
You need to set "OnClickListener" to each inflated view.
OnClickListener onClickListener=new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();//layoutViewDealsList.indexOfChild(layoutView);
Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();
}
};
LinearLayout layoutViewDealsList = (LinearLayout) view.findViewById(R.id.baseLinearLayoutSiteHistory);
txtViewNoDeals = (TextView) view.findViewById(R.id.txtViewNoDeals);
if(dealsList != null && dealsList.size() > 0) {
for(int index = 0; index < dealsList.size(); index++) {
txtViewNoDeals.setVisibility(View.GONE);
LayoutInflater inflater = (LayoutInflater) profileActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.deal_row, null);
txtViewDealsPrice = (TextView) layoutView.findViewById(R.id.txtViewDealPrice);
txtViewDealsTitle = (TextView) layoutView.findViewById(R.id.txtViewDealTitle);
txtViewDealsDescription = (TextView) layoutView.findViewById(R.id.txtViewDealDescription);
txtViewDealsBuyer = (TextView) layoutView.findViewById(R.id.txtViewDealBuyer);
txtViewDealsDate = (TextView) layoutView.findViewById(R.id.txtViewDealDate);
txtViewDealsPrice.setText("Price: $" + dealsList.get(index).getDeal_amount());
txtViewDealsTitle.setText(dealsList.get(index).getTitle());
txtViewDealsDescription.setText(dealsList.get(index).getDesc());
layoutViewDealsList.addView(layoutView);
int position = layoutViewDealsList.indexOfChild(layoutView);
appDelegate.setDealsList(dealsList);
layoutView.setTag(position);
layoutView.setOnClickListener(onClickListener);
}
}
Also notice that position is set to each layout view instead of it'a parent.
Jsut putting the clicklistner inside the view after setting the tag all thing which you are Done @Avinash kumar Pankaj is right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With