Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get position of dynamically created child view after clicking on it

Tags:

android

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.

like image 698
Avinash Kumar Pankaj Avatar asked Nov 28 '13 05:11

Avinash Kumar Pankaj


3 Answers

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();


        }
like image 199
Hardik Joshi Avatar answered Oct 21 '22 01:10

Hardik Joshi


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.

like image 30
vipul mittal Avatar answered Oct 20 '22 23:10

vipul mittal


Jsut putting the clicklistner inside the view after setting the tag all thing which you are Done @Avinash kumar Pankaj is right.

like image 41
Ravind Maurya Avatar answered Oct 08 '22 07:10

Ravind Maurya