Reputation: 1720
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.
Upvotes: 6
Views: 7952
Reputation: 71
In case much better solution is :
Set Tag First-
int position = layout.indexOfChild(view);
layout.setTag(position);
When Clicking time:
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
fileDataList.remove(position);
((LinearLayout) addView.getParent()).removeView(addView);
}
});
Upvotes: 1
Reputation: 17401
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.
Upvotes: 3
Reputation: 9507
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();
}
Upvotes: 9